I am an affront to society for (sorta) lying

This commit is contained in:
Ren Kararou 2023-10-14 01:43:47 -05:00
parent be94e873b0
commit 44d2d245cd
Signed by: spicywolf
GPG key ID: B0BA4EEC0714F8E6
2 changed files with 33 additions and 41 deletions

View file

@ -5,45 +5,28 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <signal.h>
#include <stdint.h>
#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 */) { int main(/* int argc, char** argv */) {
//setup signal handling for (int i = 1; i < 999999999; i++) {
signal(SIGINT, siginthandl); char buf[20] = "\0";
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; int pos = 0;
if (i % 3 == 0) { if (i % 3 == 0) {
pos = snprintf(buf+pos, MAXBUFFERYO-pos, "fizz"); pos = snprintf(buf+pos, 20-pos, "fizz");
if (pos < 0) return ENOMEM; if (pos < 0) return ENOMEM;
} }
if (i % 5 == 0) { if (i % 5 == 0) {
pos = snprintf(buf+pos, MAXBUFFERYO-pos, "buzz"); pos = snprintf(buf+pos, 20-pos, "buzz");
if (pos < 0) return ENOMEM; if (pos < 0) return ENOMEM;
} }
if (pos == 0) { if (pos == 0) {
pos = snprintf(buf+pos, MAXBUFFERYO-pos, "%d", i); pos = snprintf(buf+pos, 20-pos, "%d", i);
if (pos < 0) return ENOMEM; if (pos < 0) return ENOMEM;
} }
puts(buf); if (puts(buf) < 1) {
} else { return EIO;
break;
} }
} }
return 0; // SUCCESS! return 0;
} }

View file

@ -3,13 +3,22 @@
* All rights reserved * * 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() { fn main() {
for i in 1..999999999 { for i in 1..999999999 {
if i % 3 == 0 && i % 5 == 0 { println!("fizzbuzz"); } let mut buf = String::with_capacity(20);
else if i % 3 == 0 { println!("fizz"); } if i % 3 == 0 {
else if i % 5 == 0 { println!("buzz"); } write!(&mut buf, "fizz").expect("cannot write to buffer");
else { println!("{i}"); } }
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}");
} }
} }