From 44d2d245cd4ffb21007c6dc1d9fcb36b023547f7 Mon Sep 17 00:00:00 2001 From: Ren Kararou Date: Sat, 14 Oct 2023 01:43:47 -0500 Subject: [PATCH] I am an affront to society for (sorta) lying --- c/fizzbuzz/src/main.c | 55 ++++++++++++++------------------------- rust/fizzbuzz/src/main.rs | 19 ++++++++++---- 2 files changed, 33 insertions(+), 41 deletions(-) diff --git a/c/fizzbuzz/src/main.c b/c/fizzbuzz/src/main.c index e7aced1..bfc227f 100644 --- a/c/fizzbuzz/src/main.c +++ b/c/fizzbuzz/src/main.c @@ -5,45 +5,28 @@ #include #include -#include -#include - -#define RUNTO 999999999 -#define MAXDIGITSYO 10 -#define MAXBUFFERYO (MAXDIGITSYO*2) -static volatile uint8_t run = 1; - -void siginthandl(int _notused) { - run = 0; -} +// I hope you're happy with yourself barrow <3 int main(/* int argc, char** argv */) { - //setup signal handling - signal(SIGINT, siginthandl); - - for (int i = 1; i < RUNTO; i++) { // if we ever get this high we've won computing. - if (run) { - // setup a buffer, 10 digits is all we need, since we're only going up to 999999999 (plus a null terminator) - char buf[MAXBUFFERYO] = "\0"; - // gotta get that return value. - int pos = 0; - if (i % 3 == 0) { - pos = snprintf(buf+pos, MAXBUFFERYO-pos, "fizz"); - if (pos < 0) return ENOMEM; - } - if (i % 5 == 0) { - pos = snprintf(buf+pos, MAXBUFFERYO-pos, "buzz"); - if (pos < 0) return ENOMEM; - } - if (pos == 0) { - pos = snprintf(buf+pos, MAXBUFFERYO-pos, "%d", i); - if (pos < 0) return ENOMEM; - } - puts(buf); - } else { - break; + for (int i = 1; i < 999999999; i++) { + char buf[20] = "\0"; + int pos = 0; + if (i % 3 == 0) { + pos = snprintf(buf+pos, 20-pos, "fizz"); + if (pos < 0) return ENOMEM; + } + if (i % 5 == 0) { + pos = snprintf(buf+pos, 20-pos, "buzz"); + if (pos < 0) return ENOMEM; + } + if (pos == 0) { + pos = snprintf(buf+pos, 20-pos, "%d", i); + if (pos < 0) return ENOMEM; + } + if (puts(buf) < 1) { + return EIO; } } - return 0; // SUCCESS! + return 0; } diff --git a/rust/fizzbuzz/src/main.rs b/rust/fizzbuzz/src/main.rs index f72c0b3..d9b55a8 100644 --- a/rust/fizzbuzz/src/main.rs +++ b/rust/fizzbuzz/src/main.rs @@ -3,13 +3,22 @@ * All rights reserved * ************************/ -// A memory safe fizzbuzz in rust is much smaller than the same in C, turns out. +use std::fmt::Write; + +// still much smaller, hope you're happy barrow fn main() { for i in 1..999999999 { - if i % 3 == 0 && i % 5 == 0 { println!("fizzbuzz"); } - else if i % 3 == 0 { println!("fizz"); } - else if i % 5 == 0 { println!("buzz"); } - else { println!("{i}"); } + let mut buf = String::with_capacity(20); + if i % 3 == 0 { + write!(&mut buf, "fizz").expect("cannot write to buffer"); + } + if i % 5 == 0 { + write!(&mut buf, "buzz").expect("cannot write to buffer"); + } + if buf.is_empty() { + write!(&mut buf, "{i}").expect("cannot write to buffer"); + } + println!("{buf}"); } }