From be94e873b06e2e0b97d2d5c51c7298da5ac34295 Mon Sep 17 00:00:00 2001 From: Ren Kararou Date: Fri, 13 Oct 2023 12:32:44 -0500 Subject: [PATCH] initial commit; fizzbuzz complete --- .gitignore | 4 ++++ c/fizzbuzz/.gitignore | 1 + c/fizzbuzz/makefile | 36 ++++++++++++++++++++++++++++ c/fizzbuzz/src/main.c | 49 +++++++++++++++++++++++++++++++++++++++ rust/fizzbuzz/Cargo.lock | 7 ++++++ rust/fizzbuzz/Cargo.toml | 8 +++++++ rust/fizzbuzz/src/main.rs | 15 ++++++++++++ 7 files changed, 120 insertions(+) create mode 100644 .gitignore create mode 100644 c/fizzbuzz/.gitignore create mode 100644 c/fizzbuzz/makefile create mode 100644 c/fizzbuzz/src/main.c create mode 100644 rust/fizzbuzz/Cargo.lock create mode 100644 rust/fizzbuzz/Cargo.toml create mode 100644 rust/fizzbuzz/src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c73965b --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +bld/ +dbg/ +obj/ +target/ diff --git a/c/fizzbuzz/.gitignore b/c/fizzbuzz/.gitignore new file mode 100644 index 0000000..b97647c --- /dev/null +++ b/c/fizzbuzz/.gitignore @@ -0,0 +1 @@ +bld diff --git a/c/fizzbuzz/makefile b/c/fizzbuzz/makefile new file mode 100644 index 0000000..7180b3f --- /dev/null +++ b/c/fizzbuzz/makefile @@ -0,0 +1,36 @@ +######################## +# (c) Ren Kararou 2023 # +# All rights reserved # +######################## + +# For use with GNU Make + +CFLAGS ?= -Wall -Wextra -march=native -O3 -flto=thin -funroll-loops + +.PHONY : all +all : bld/fizzbuzz + +.PHONY : debug +debug : bld/fizzbuzz-dbg + +obj/%.o : src/%.c + @mkdir -p obj + @cc $(CFLAGS) -c -o $@ $< + +bld/fizzbuzz : obj/main.o + @mkdir -p bld + @cc $(CFLAGS) -o $@ $< + @llvm-strip $@ + +dbg/%-dbg.o : src/%.c + @mkdir -p dbg + @cc --debug $(CFLAGS) -c -o $@ $< + +bld/fizzbuzz-dbg : dbg/main-dbg.o + @mkdir -p bld + @cc --debug $(CFLAGS) -o $@ $< + +.PHONY : clean +clean : + @rm -rf obj bld dbg + diff --git a/c/fizzbuzz/src/main.c b/c/fizzbuzz/src/main.c new file mode 100644 index 0000000..e7aced1 --- /dev/null +++ b/c/fizzbuzz/src/main.c @@ -0,0 +1,49 @@ +/************************ + * (c) Ren Kararou 2023 * + * All rights reserved * + ************************/ + +#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; +} + +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; + } + } + return 0; // SUCCESS! +} + diff --git a/rust/fizzbuzz/Cargo.lock b/rust/fizzbuzz/Cargo.lock new file mode 100644 index 0000000..a091d7f --- /dev/null +++ b/rust/fizzbuzz/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "fizzbuzz" +version = "0.1.0" diff --git a/rust/fizzbuzz/Cargo.toml b/rust/fizzbuzz/Cargo.toml new file mode 100644 index 0000000..f7a4018 --- /dev/null +++ b/rust/fizzbuzz/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "fizzbuzz" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/fizzbuzz/src/main.rs b/rust/fizzbuzz/src/main.rs new file mode 100644 index 0000000..f72c0b3 --- /dev/null +++ b/rust/fizzbuzz/src/main.rs @@ -0,0 +1,15 @@ +/************************ + * (c) Ren Kararou 2023 * + * All rights reserved * + ************************/ + +// A memory safe fizzbuzz in rust is much smaller than the same in C, turns out. + +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}"); } + } +}