ITT: We critique each others fizzbuzzes

ITT: We critique each others fizzbuzzes

#include #include #ifdef __GNUC__ #define HOT_INLINED __attribute__ ((always_inline)) inline \ __attribute__ ((hot))#else #define HOT_INLINED inline#endifstatic const char *LOOKUP_TABLE[] = { "FIZZBUZZ", NULL, NULL, "FIZZ", NULL, "BUZZ", "FIZZ", NULL, NULL, "FIZZ", "BUZZ", NULL, "FIZZ", NULL, NULL };typedef struct s_div10_t{ const uint32_t quot; const uint32_t rem;}div10_t;static HOT_INLINED div10_t div10(uint32_t value){ uint32_t quot = (value >> 1) + (value >> 2); quot = quot + (quot >> 4); quot = quot + (quot >> 8); quot = quot + (quot >> 16); quot = quot >> 3; uint32_t rem = value - ((quot 4); if (9 < rem) rem = rem - 10; return (div10_t) { .quot = quot, .rem = rem };}static HOT_INLINED uint32_t mod15(uint32_t value){ value = (value >> 16) + (value & 0xFFFF); value = (value >> 8) + (value & 0xFF); value = (value >> 4) + (value & 0xF); if (value < 15) return value; if (value < 30) return value - 15; if (value < 45) return value - 30; return value - 45;}static HOT_INLINED void printu(uint32_t value){ if (0 == value) { putchar_unlocked('0'); putchar_unlocked('\n'); } else { char buffer[11] = { 0 }; char *iter = &buffer[9]; buffer[10] = '\n'; while (value) { const div10_t div10_r = div10(value); value = div10_r.quot; *(iter--) = div10_r.rem + '0'; } while (*iter != '\n') putchar_unlocked(*(++iter)); }}static HOT_INLINED void fastbuzz(uint32_t n){ const char *result = LOOKUP_TABLE[mod15(n)]; if (NULL != result) { puts(result); } else { printu(n); }}int main(){ for (uint32_t i = 1; i < 100; i++) { fastbuzz(i); } return 0;}

Other urls found in this thread:

github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition
joelgrus.com/2016/05/23/fizz-buzz-in-tensorflow/
twitter.com/AnonBabble

is the assembly to this fewer lines than the source?
I looked at the assembly to this with -Os, and the answer is no, barely, the straight assembly is 134 lines while this is 112

reposting my fizzbuzz from another thread: 
use std::cmp::min;const FNS: [fn(usize) -> FizzBuzz; 15] = [ FizzBuzz::fizzbuzz, FizzBuzz::number, FizzBuzz::number, FizzBuzz::fizz, FizzBuzz::number, FizzBuzz::buzz, FizzBuzz::fizz, FizzBuzz::number, FizzBuzz::number, FizzBuzz::fizz, FizzBuzz::buzz, FizzBuzz::number, FizzBuzz::fizz, FizzBuzz::number, FizzBuzz::number];#[derive(Debug, Eq, PartialEq)]enum FizzBuzz { FizzBuzz, Fizz, Buzz, Number(usize)}impl FizzBuzz { fn fizzbuzz(n: usize) -> Self { debug_assert!(n % 15 == 0); FizzBuzz::FizzBuzz } fn fizz(n: usize) -> Self { debug_assert!(n % 3 == 0 && n % 5 != 0); FizzBuzz::Fizz } fn buzz(n: usize) -> Self { debug_assert!(n % 3 != 0 && n % 5 == 0); FizzBuzz::Buzz } fn number(n: usize) -> Self { debug_assert!(n % 3 != 0 && n % 5 != 0); FizzBuzz::Number(n) }}fn main() { fizzbuzz(|v| println!("{:?}", v), 1, 100);}fn fizzbuzz(mut f: F, mut start: usize, end: usize) { assert!(end >= start); let rem = start % 15; if rem != 0 { for i in rem..rem + min(15 - rem, end - start) { f(FNS[i](start - rem + i)); } start += 15 - rem; } debug_assert!(start % 15 == 0); debug_assert!(end >= start); while (end - start) >= 15 { f(FizzBuzz::fizzbuzz(start)); f(FizzBuzz::number(start + 1)); f(FizzBuzz::number(start + 2)); f(FizzBuzz::fizz(start + 3)); f(FizzBuzz::number(start + 4)); f(FizzBuzz::buzz(start + 5)); f(FizzBuzz::fizz(start + 6)); f(FizzBuzz::number(start + 7)); f(FizzBuzz::number(start + 8)); f(FizzBuzz::fizz(start + 9)); f(FizzBuzz::buzz(start + 10)); f(FizzBuzz::number(start + 11)); f(FizzBuzz::fizz(start + 12)); f(FizzBuzz::number(start + 13)); f(FizzBuzz::number(start + 14)); start += 15; } debug_assert!(start % 15 == 0); debug_assert!(end >= start); for i in 0..end - start { f(FNS[i](start + i)); }}

retard

Has anyone determined the least bloated FizzBuzz possible?

Attached: rms.png (750x1000, 573.14K)

pen and a pad of fuckin paper

tl;dr
bloat
0/10

also this task is too trivial to discuss, there's really nothing interesting, unless you are brainlet or are trying to be funny and need ideas for a pull request to github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition

joelgrus.com/2016/05/23/fizz-buzz-in-tensorflow/

for i in range(100): if i == 1: print 1 elif i == 2: print 2 elif i == 3: print "fizz" elif i == 4: print 4 elif i == 5: print "buzz" elif i == 6 print "fizz" elif i == 7 print 7...

butt ugly GAS syntax
.section .datafizz: .ascii "fizz\n"buzz: .ascii "buzz\n"fizzbuzz: .ascii "fizzbuzz\n"buf: .skip 20nl: .ascii "\n".section .text.global _start_start: mov $1, %r151: /* mod 15 */ mov %r15, %rax mov $15, %rbx xor %rdx, %rdx div %rbx cmp $0, %rdx jne 2f mov $4, %rax mov $1, %rbx mov $9, %rdx mov $fizzbuzz, %rcx int $0x80 jmp 6f2: /* mod 3 */ mov %r15, %rax mov $3, %rbx xor %rdx, %rdx div %rbx cmp $0, %rdx jne 3f mov $4, %rax mov $1, %rbx mov $5, %rdx mov $fizz, %rcx int $0x80 jmp 6f3: /* mod 5 */ mov %r15, %rax mov $5, %rbx xor %rdx, %rdx div %rbx cmp $0, %rdx jne 4f mov $4, %rax mov $1, %rbx mov $5, %rdx mov $buzz, %rcx int $0x80 jmp 6f4: /* number to buffer */ mov %r15, %rax mov $buf, %r14 add $20, %r145: dec %r14 mov $10, %rbx xor %rdx, %rdx div %rbx add $'0', %rdx mov %dl, (%r14) cmp $0, %rax jne 5b mov $4, %rax mov $1, %rbx mov $nl, %rdx sub %r14, %rdx inc %rdx mov %r14, %rcx int $0x80 6: /* jump to 1 if counter != 101 */ inc %r15 cmp $101, %r15 jne 1b /* exit */ mov $1, %rax mov $0, %rbx int $0x80
Save as fizzbuzz.S and compile with `as -o fizzbuzz.o fizzbuzz.S; ld -o fizzbuzz fizzbuzz.o`

Your fizz-buzz violates KISS principle.

KISS principle is for Java pajeets who need interfaces and inheritance

/** * Plays FizzBuzz up to an arbitrary high given integer. * FizzBuzz is a game where the goal is to count up to an integer * n while replacing the multiples of 3 with Fizz, the mutliples * of 5 with Buzz and the multiples of both 3 and 5 with * Fizzbuzz. It is a common entry-level problem for gaujing a software * engineer's expertise and methodological rigor. * @author Dinesh Ratrikasha * @version 1.0.0 * @since 2018-04-07 */public class FizzBuzz { /** * Checks whether an integer is a multiple of another. * @param n The integer to be tested * @param q The divisor to be tested * @return {@code true} if {@code n} is a multiple of {@code q}, {@code false} otherwise */ private static boolean isMultiple(final int n, final int q) { // The integer n is a multiple of q if and only if n = 0 (mod q) return n % q == 0; } /** * Prints the appropriate FizzBuzz message for a given integer. * @param n The number of the message */ private static void printFizzBuzzMessage(final int n) { // Since 3 and 5 are co-prime, (3 | n and 5 | n) iff 15 | n if (isMultiple(n, 15)) System.out.println("Fizzbuzz"); else if (isMultiple(n, 3)) System.out.println("Fizz"); else if (isMultiple(n, 5)) System.out.println("Buzz"); else System.out.println(n); } /** * Program starting point. * @param args Command-line arguments, expected to be of length 1 * @throws SecurityException if a call to {@link System#exit} fails (unlikely) */ public static void main(String[] args) { if (args.length == 1) { try { final int max = Integer.parseInt(args[0]); for (int i = 1; i

...

Attached: hurr_fizzbuzz.png (621x942, 34.22K)

Yes we have.
: fizz 3 mod 0= if ." fizz" -1 else 0 then ;: buzz 5 mod 0= if ." buzz" -1 else 0 then ;: else -rot or 0= if . then ;: fb 100 0 do i fizz i buzz i else cr loop ;

Alt:
: fizz ( n -- ) drop ." Fizz" ;: buzz ( n -- ) drop ." Buzz" ;: fb ( n -- ) drop ." FizzBuzz" ;: vector create does> ( n -- ) over 15 mod cells + @ execute ;vector .fizzbuzz ' fb , ' . , ' . , ' fizz , ' . , ' buzz , ' fizz , ' . , ' . , ' fizz , ' buzz , ' . , ' fizz , ' . , ' . ,

2/10 would not hire

AWK:seq 100 | awk '!($1%3){printf"Fizz"}!($1%5){printf"Buzz"}{if(!(!($1%3)||!($1%5)))print;else printf"\n"}'
sed:seq 100 | sed '3~3s/.*/Fizz/;5~5s/.*/Buzz/;15~15s/.*/FizzBuzz/'

Can anyone make it even shorter?

Invalid solution. You are supposed to determine conditions for fizz and buzz as you go, and not just regurgitate a premade table every 15 steps.

Yes. Given that the universe is doomed to perish eventually anyway, 0x90 (NOP) is equivalent to any program in the long run.

generator () { echo ${1:-1} generator $((${1:-1} + 1))}divides() { [ $(($1 % $2)) = 0 ]}enhancer () { echo -n $1 divides $1 3 && echo -n ' 'fizz divides $1 5 && echo -n ' 'buzz echo}printer () { echo -n $1'\r' echo -n $2 echo $3}per_line () { while read line do $1 $line done}generator | per_line enhancer | per_line printer

yelling "nigger" at the wall and observing the output of the wall is equivalent to any program in the long run

#include struct potential { int divisor; char *word;};struct potential potentials[] = { (struct potential){3, "fizz"}, (struct potential){5, "buzz"}, (struct potential){15, "fizzbuzz"}};void u(int num){ int length; char *output = 0; length = sizeof potentials / sizeof (struct potential); for (int i = 0; i < length; i++) if (!(num % potentials[i].divisor)) output = potentials[i].word; if (output) puts(output); else printf("%d\n", num); u(num + 1);}main [-1u]= { 1};

?????????????

Just compile it. It's C.

Attached: index.jpeg (300x168, 4.81K)

Looks like code "refactored" by a disgruntled employee one day before he left

Compile it and find out.

for 1..50 { if !it%3 printf("fizz\n"); else if !it%5 printf("buzz\n"); else if !it%15 printf("fizzbuzz\n"); else printf("%\n", it);}

Attached: 15655180.jpg (1280x720, 136.21K)

Even if it somehow compiles and works, that still won't explain what that ayyyylmao-tier piece of syntax is supposed to represent

fucking bloat

It's declaring an array with the name main and a size of -1u. -1u is unsigned -1, or 4294967295. It's implicitly typed int, which means the array is 16 GB large on most architectures. The first element is set to 1, all the others are set to 0. All of that is dumped in the binary, so the file will be 16 GB+ and the compiler won't be very responsive. There's no main function, but because the symbol is called "main" the linker will accept it. The executable will be created but won't work.

I'd rather take a bloated program than a slow program.

So I was in the right staying sceptical of actually attempting to compile it.

Attached: notcheatedthistime.png (149x102, 44.68K)

seq 100|sed 5~5cBuzz|sed 3~3s/[^B]*/Fizz/

Attached: 1520512904039.jpg (1130x1194, 359.19K)

...

GNU isn't perfect but it sure is better than traditional inefficient arbitrarily-limited Unix.

while($counter++,$num++ -lt 100){ $mod3 = $num % 3 $mod5 = $num % 5 if($mod3 -eq 0 -and $mod5 -eq 0){ Write-Output FizzBuzz }elseif($mod5 -eq 0){ Write-Output Buzz }elseif($mod3 -eq 0){ Write-Output Fizz }else{ Write-Output $num }}

sauce? are there n00dez?

Care to explain this bit? It really confuses me.

The suffix "u" makes the compiler read the integer as an unsigned integer. Because it is almost guaranteed that the computer uses two-complement to represent integers, signed -1 is equivalent to the maximum permitted unsigned integer, i.e. 1111 1111 1111 1111...
So, he's creating an array of UINT_MAX which contains the number 1 everywhere.

Attached: Screenshot at 2018-04-12 21-42-59.png (661x421, 37.48K)

Uhm that's not the screencap I wanted to post.

Attached: Screenshot at 2018-04-17 21-29-49.png (661x421, 25.68K)

Now, that's not the real trick he's doing. By creating an array called main, he tricks the linker into thinking the contained data is in fact the main() function. I saw this hack implemented by simply declaring a const char array that contained the bytes of a classic "hello, world!" program. Because they were declared as const, they were also executable (since memory cells are either writable or executable in modern computers). That way, it was effectively possible to write a C program without ever declaring a main() function.

Why it would work without declaring the type of the array, or the array as constant I do not know.

FizzBuzz hardware edition:library IEEE;use IEEE.std_logic_1164.all;use IEEE.numeric_std.all;entity fizzbuzz is port (clk : in std_logic; fizz, buzz : out std_logic);end fizzbuzz;architecture behaviour of fizzbuzz is signal cnt : unsigned(6 downto 0) := "0000001"; signal cnt3, cnt5 : unsigned(2 downto 0) := "001";begin p1: process(clk) begin if rising_edge(clk) then cnt

Attached: Screenshot at 2018-04-17 22-22-07.png (881x126, 9.52K)