[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
FizzBuzz code golf
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 106
Thread images: 7
File: fizz-buzz.png (21 KB, 480x350) Image search: [Google]
fizz-buzz.png
21 KB, 480x350
Welcome to FizzBuzz code golf!

What it means?

The "FizzBuzz" part:
You have to write a program that prints the numbers from 1 to 100. But for multiples of three print the word “Fizz” instead of the number and for the multiples of five print the word “Buzz”. For numbers which are multiples of both three and five print word “FizzBuzz”. Each word and number you print must be in a new line.

The "code golf" part:
Code producing said output with the least number of characters (including whitespace characters) wins. Sadly there are no prizes.

Each language has it's own category and there is a special category consisting of all languages.
Code has to be compilable on ideone online compiler https://ideone.com/
Your submission must have the following format (it will be helpful for others to read it):

Reply(optional)
Language
Chars
Code
Comment(optional)

Where:
Language - the language you have written the code in
Chars - the total number of charactes in your code (including whitespace characters)
Code - Actual code of your submission in code tags

You are encouraged to use the code already posted in this thread by yourself or other people, but please reply to the submission you have modified. Try to use all the hacks you know and feel free to learn more on the internet, but don't spoil yourself fun by reading FizzBuzzes from other competitions.

Good luck!
>>
 
bool fizz;
bool buzz;
for (int i =1; i <=100; i++) {
if (i % 3 == 0)
fizz = true;
if (i % 5 == 0)
buzz = true;
if (fizz || buzz) {
if(fizz)
puts("fizz");
if(buzz)
puts("buzz");
} else {
printf("%d\n", i);
}
}
>>
>>53735806
What does || do?
>>
>>53735950
or
>>
>>53735806
for(int i = 0; i < 101;++i)
{
if (!(i%3))
std::cout << "fizz";
if (!(i%5))
std::cout << "buzz";
if (i%3 && i%5)
std::cout << i;
std::cout << '\n';
}
>>
print('\n'.join(['', 'fizz', 'buzz', 'fizzbuzz'][i%3==0+(i%5==0)*2] or str(i) for i in range(1, 101)))
>>
import fizzbuzz
fizzbuzz()
>>
>Inb4 Python best language
>>
 
def g(c):
while c<101:
print(c)
if c%3==0:
print('fizz')
if c%5==0:
print('buzz')
c=c+1
g(0)
[\code]
>>
>>53736653
fuck me.
Python
107 Characters
>>
>>53736306
106 characters
>>
File: 1432323884812-0.png (26 KB, 431x499) Image search: [Google]
1432323884812-0.png
26 KB, 431x499
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

void _start()
{
for (register uint_fast8_t i = 1; i <= 100; ++i)
printf("%" PRIuFAST8 " %s", i, (i % 15 == 0 ? "FizzBuzz\n" : (i % 5 == 0 ? "Buzz\n" : (i % 3 == 0 ? "Fizz\n" : "\n"))));
exit(0);
}


Compile with -nostartfiles -O3 -std=c11 -march=native for sanic speed.
>>
>>53736306
This doesn't work. it just prints fizz for everything.
>>
File: fizzbuzz of the christ.png (402 KB, 1024x768) Image search: [Google]
fizzbuzz of the christ.png
402 KB, 1024x768
>>53735649
>>
>>53736736
>sanic speed
>uses printf
Pls

#include <unistd.h>
#include <stdint.h>
#include <string.h>

#ifndef TARGET
#define TARGET 100
#endif

#ifndef BUFLEN
#define BUFLEN 1024
#endif


static char buf[BUFLEN * 16];


static const char* fizzbuzz[] = {
"FizzBuzz", NULL, NULL, "Fizz", NULL, "Buzz", "Fizz",
NULL, NULL, "Fizz", "Buzz", NULL, "Fizz", NULL, NULL
};


static void* digitize(char* pos, uint32_t num)
{
if (num > 0)
{
pos = digitize(pos, num / 10);
*pos++ = '0' + num % 10;
}

return pos;
}


static inline char* format(char* pos, uint32_t mod, uint32_t num)
{
const char* ptr = fizzbuzz[mod];

if (ptr != NULL)
{
while (*ptr != '\0')
{
*pos++ = *ptr++;
}
}
else
{
pos = digitize(pos, num);
}

*pos++ = '\n';
return pos;
}


int main()
{
char* pos = buf;
char* end = buf + (BUFLEN - 1) * 16;

uint32_t num __attribute__((aligned(16)));
uint32_t mod __attribute__((aligned(16)));

for (num = mod = 1; num <= TARGET; ++num, ++mod)
{
if (mod == 15)
{
mod = 0;
}

if (pos >= end)
{
write(1, buf, pos - buf);
pos = buf;
}

pos = format(pos, mod, num);
}

write(1, buf, pos - buf);

return 0;
}
>>
>>53736746
Not him, I just ran it, and it did print a successful fizzbuzz for numbers 1 to 100
>>
f="fizzbuzz"
for i in range(101):
if i%15==0: print f
elif i%3==0: print f[:4]
elif i%5==0: print f[4:]
else: print i
>>
File: fizzbuzz.webm (3 MB, 1280x720) Image search: [Google]
fizzbuzz.webm
3 MB, 1280x720
>>53735649
>old webm I did some time ago related
>>
>>53736695
why even make a function. you could have saved a whole bunch of chars.
>>
File: fucking-font.webm (320 KB, 1138x640) Image search: [Google]
fucking-font.webm
320 KB, 1138x640
>>53736790
another one
>>
C++
126 chars
#include<cstdio>
main(int i){for(;i<101;i++){bool a=i%5,b=i%3;printf(a&b?"%d\n":&"FizzBuzz\n\0Fizz\n"[a&!b?10:b&!a?4:0],i);}}


C++ used for bools, compiles on ideone. Also, printf is silly with it's arguments.
>>
>>53736752
Damn, that's clever
>>
>>53736829
It isn't called 'FizzBuzz of the Christ' for nothing
>>
main(i){for(;i<101;puts(i++%5?"":"Buzz"))printf(i%3?i%5?"%d":"":"Fizz",i);}
>>
>>53736306
>>53736653
These don't even work correctly, faggots.

My submission:
Python, 96 chars.
print '\n'.join((('Fizz'if x%3==0else'')+('Buzz'if x%5==0else''))or str(x)for x in range(1,101))
>>
>>53736759
You should really protect those attribute specifiers baka desu senpai.
>>
>>53736917
Not sure if new to Python or doesn't realize that 2 and 3 have different print conventions

Those 2 you quoted DO work
>>
>>53736961
I never said they don't work (as in "fail to execute without errors"). I just said they do not work correctly. Which is true.
>>
>>53737020
Anyways, >>53736752 makes this a moot point. That is a stroke of genius.
>>
>>53735649
https://github.com/haasn/fizzbuzz/tree/master
>>
>>53737020
>>53737039
You're right. My bad
>>
>>53735649
> include "fizzbuzz.h"
> start.fizbuzz(1, 100);

Done.
>>
: P MOD 0= IF TYPE 1+ ELSE 2DROP THEN ;
: F ?DO CR 0 S" FIZZ" I 3 P S" BUZZ" I 5 P 0= IF I . THEN LOOP ;
>>
>>53736736
>not going -Ofast -funroll-loops
Do you even sanic?
>>
for (int i = 0; i < 101; i++)
{
i % 3 ? i : (cout << "fizz\n"; continue;);
i % 5 ? i : (cout << "Buzz\n"; continue;);
i % 15 ? i : "FizzBuzz\n";
}


This should satisfy. 141 characters. It can fit in a tweet
>>
>>53737176
prints "fizz" instead of "Fizz"
>>
>>53737176
I should change each ternary statement to be inside of a cout statement. The numbers don't output, only fizz, buzz and FizzBuzz output. But close enough. Add 18 more characters for a total of 159 characters.

>>53737205
Yeah fuck off m8
>>
Don't really care about the "contest," but yeah.

for(var i=1;i<=100;i++) console.log(i%15==0?"fizzbuzz":i%5==0?"buzz":i%3==0?"fizz":i);
>>
>>53736917
5 prints out buzzfizz
>>
I'm going to create a language and compiler that generates fizzbuzz machine code when supplied with an empty file. Gimme a sec.
>>
>>53737072
No problem m8. We all are too quick to judge sometimes. :^)
>>
>>53735806
what about a newline after fizz and or buzz? faggot.
>>
>>53735649
PHP
84 bytes
for($i=1;$i<101;$i++){$s=($i%3==0?'Fizz':'').($i%5==0?'Buzz':'');echo$s?$s:$i,"\n";}
>>
>>53735649
>Language
Haskell

>Chars
118

>Code
f=zipWith3 (\f b n->if null (f++b) then show n else f++b) (cycle ["","","fizz"]) (cycle ["","","","","buzz"]) [1..100]
>>
>>53737378
but that's easy, just write a program that reads in data from a file and closes it, executes a fizzbuzz function if it's empty, otherwise passes it to a compiler of your choice with original cli arguments. Unless it's just something you want to do for fun
>>
>>53736752
What's with that -- operator? It works like bit shift, but only when written in that solution. For esample:
8--1 == 9
8--1**4%5 == 4
(8--1)**4%5 == 1
8--(1**4%5) == 9

What the hell is going on here? This is insane.
>>
Thought to give it a try but ended up a mess.
#include<iostream>
main(){std::string c[]{"Fizz","Buzz"};for(int i=1;i<101;i++)std::cout<<(i%5?i%3?std::to_string(i):c[0]:i%3?c[1]:c[0]+c[1])<<'\n';}
>>
>>53737736
It's not 1 operator, it's 2

First is subtract
Second is sign-change

Precedence makes it:
8 - ( (-(i**4)) % 5)
>>
R:
a=b=1:100;b[c<-!a%%5]="";b[!a%%3]="Fizz";b[c]=paste("Buzz",b[c]);b

67 characters.
>>
>>53737786
The modulo of negative number threw me off somehow. Thank you.
>>
>>53737795
oops, swap the arguments of paste to get the correct result. also use paste0 if "Fizz Buzz" is illegal.
>>
>>53737814
No problem, it throws off most people in /dpt/ due to how it uses one of fermat's theorem, lack of decrement operator, and the result of negative modulo
>>
>>53737795
R, actually following the rules, 83 chars:
a=b=1:100;b[c<-!a%%5]="";b[!a%%3]="Fizz";b[c]<-paste0(b[c],"Buzz");lapply(b,print)
>>
Rust
141 chars
fn main(){for n in 1..101{match (n%3,n%5){(0,0)=>println!("FizzBuzz"),(0,_)=>println!("Fizz"),(_,0)=>println!("Buzz"),_=>println!("{}",n)}}}
>>
>>53737922
The fact that negation takes higher precedence than modulo (but lower than power) also doesn't make too much sense, I would say.
>>
>>53737489
If we're allowed to cheat by not actually printing:

let f x=fromMaybe(show x)$["fizz"|x`rem`3<1]<>["buzz"|x`rem`5<1]


64 chars, compile with ghc -XMonadComprehensions
>>
Never tried any kind of code golf before. My first attempt, in C:

#include <stdio.h>
s(s,e){if(e==0)printf(s);}main(){for(int i=1;i<101;i++){int t=i%3;int f=i%5;if(t&&f){printf("%i",i);puts("");continue;}s("fizz",t);s("buzz",f);puts("");}}
>>
>>53738095
Make that 60, forgot to strip the "let " from testing.
>>
what am i doing wrong desu
#include <stdio.h>
#include <stdlib.h>

typedef enum {false, true} bool;
int main()
{
bool fizz;
bool buzz;
int i;

for (i = 1; i <=100; i++)

{
if (i % 3 == 0)
fizz = true;
if (i % 5 == 0)
buzz = true;
if (fizz || buzz)
{ // || == "or"
if(fizz == true)
puts("fizz");
if(buzz == true)
puts("buzz");
}
else
{
printf("%d\n", i);
}
}
}
>>
>>53737957
a=b=1:100;b[c<-!a%%5]="";b[!a%%3]="Fizz";b[c]=paste0(b[c],"Buzz");cat(b,sep="\n")

81.
>>
>>53738222
You are putting the braces in the wrong place.
>>
The best Matlab FizzBuzz code possible:

x=1:100;
y(x)=cellstr(int2str(x'));
y(~(mod(x,3))) = cellstr('Fizz');
y(~(mod(x,5))) = cellstr('Buzz');
y(~(mod(x,15))) = cellstr('FizzBuzz');
for i=x
fprintf(strcat(y{i},'\n'));
end
>>
>>53738222
I appear to have fixed it
#include <stdio.h>
#include <stdlib.h>

typedef enum {false, true} bool;
int main()
{
/*
bool fizz;
bool buzz;
*/
int i;

for (i = 1; i <=100; i++)

{
int fizz = 0;
int buzz = 0;
if (i % 3 == 0)
fizz = 1;
if (i % 5 == 0)
buzz = 1;
if (fizz || buzz)
{ // || == "or"
if(fizz == 1)
puts("fizz");
if(buzz == 1)
puts("buzz");
}
else
{
printf("%d\n", i);
}
}
}
>>
>>53735806
Not reseting the boolean variables. Algorithm will break.
>>
>>53735649

#!/bin/bash
for i in {1..100}; do
if [ $(($i % 3)) -eq 0 ] && [ $(($i % 5)) -eq 0 ]; then
echo fizzbuzz
elif [ $(($i % 3)) -eq 0 ]; then
echo fizz
elif [ $(($i % 5)) -eq 0 ]; then
echo buzz
fi
done
>>
>>53737482
PHP
81
for($i=1;$i<101;$i++)echo($s=($i%3==0?'Fizz':'').($i%5==0?'Buzz':''))?$s:$i,"\n";
>>
>>53738292
I guess the last loop can be removed:

x=1:100;
y(x)=cellstr(int2str(x'));
y(~(mod(x,3))) = cellstr('Fizz');
y(~(mod(x,5))) = cellstr('Buzz');
y(~(mod(x,15))) = cellstr('FizzBuzz');
fprintf('%s \n',y{:});
>>
>>53738430
This works in octave, not sure about matlab and it's slightly shorter:
x=1:100;
y=cellstr(int2str(x'));
y(~mod(x,3))='Fizz';
y(~mod(x,5))='Buzz';
y(~mod(x,15))='FizzBuzz';
fprintf('%s\n',y{:});

What a fucking horrible language.
>>
>>53738430
Code further improved

x=cellstr(int2str((1:100)'));
x(3:3:end)=cellstr('Fizz');
x(5:5:end)=cellstr('Buzz');
x(15:15:end)=cellstr('FizzBuzz');
fprintf('%s\n',x{:});
>>
py
print(['fizz'*(not x%3)+'buzz'*(not x%5) or x for x in range(1,101)])
>>
>>53738781
> Each word and number you print must be in a new line.
>>
 for(int i=1;i<101;i++){if(!(i%3))std::cout<<"Fizz\n";if(!(i%5))std::cout<<"Buzz\n";if(!(i%15))std::cout<<"FizzBuzz\n";else std::cout<<i<<std::endl;} 

149 lines, cool way to learn some C++
>>
>>53738937
Why would you use C++ over C for FizzBuzz?
>>
I haven't seen a proper non-hacky one in this thread, so I'll post a reference implementation.

#include <stdio.h>

int main(int i) {
for (int i = 1; i <= 100; ++i) {
if (i % 3 == 0 && i % 5 == 0) {
printf("FizzBuzz\n");
} else if (i % 3 == 0) {
printf("Fizz\n");
} else if (i % 5 == 0) {
printf("Buzz\n");
} else {
printf("%d\n", i);
}
}
}
>>
>>53738837
We both know that's an artificial requirement.
print('\n'.join(['fizz'*(not x%3)+'buzz'*(not x%5) or str(x)for x in range(1,101)]))

Wow, the algorithm is so much more interesting now
>>
>>53738969
Object oriented programming
>>
>>53738995
I need to read questions :S

No idea ;)
>>
>>53738983
shaved a few chars:
print'\n'.join(['fizz'*(1-x%3)+'buzz'*(1-x%5) or str(x)for x in range(1,101)])
>>
rate

while (x=<100)
{
x++;
if (x%3 && x%5 == 0) {
console.writeline("fizzbuzz"); }
else if (x%3==0) {
console.writeline("fizz"); }
else if (x%5==0) {
console.writeline("buzz") }
}
>>
>tfw Java is the most maintainable language
>hacky algorithms are bad code

[/code]
class HelloWorld {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (i % 3 == 0 && i % 5 == 0) {
System.out.println("FizzBuzz");
} else if (i % 3 == 0) {
System.out.println("Fizz");
} else if (i % 5 == 0) {
System.out.println("Buzz");
} else {
System.out.println(i);
}
}
}
}
[/code]
>>
>>53735649

Groovy:

1.upto(100) {
ans =""
if (it%3 == 0) {ans ="Fizz"}
if (it%5 == 0) {ans += "Buzz"}
if (ans == "") {println it} else println ans
}
>>
>>53738983
It didn't get more interesting, but it got 15 bytes longer. This is code golf; arbitrarily omitting "uninteresting" parts is not how it's played, you retard.
>>
>>53739120
>Not writing the number when nothing applies
>Not initializing x
>Using a while loop where a for loop is proper
>Inconsistent spacing
>>
dadada
>>
>>53739120
>>53739138
codetags/10
>>
>>53739120
What fucking language are you trying to write this in?
C#/VB needs to be written as
"Console.WriteLine()"
>>
>>53739120
>>53739229
You know what, your code literally doesn't even remotely work in C# which it's most likely aimed at (Even after the Console fix)
>>
>>53739158

OK, since this is a dick contest: I got it down to 106 chars (with whitespaces), yay!

1.upto(100){ans=""
if(it%3==0){ans="Fizz"}
if(it%5==0){ans+="Buzz"}
if(ans==""){println it} else println ans}


And it's still runnable:
>https://groovyconsole.appspot.com/
>>
>>53739328

Sorry, next try:
I had a stupid variable name..

Now 99 chars (with whitespaces)!

1.upto(100){a=""
if(it%3==0){a="Fizz"}
if(it%5==0){a+="Buzz"}
if(a==""){println it} else println a}
>>
cout?
>>
File: 1444608818009.gif (2 MB, 750x750) Image search: [Google]
1444608818009.gif
2 MB, 750x750
89 characters:

FOR i TO 100 DO print(((i%*15=0|"FizzBuzz"|:i%*3=0|"Fizz"|:i%*5=0|"Buzz"|i),new line)) OD


Algol 68, biatch!
>>
File: 1458519629473.jpg (13 KB, 540x489) Image search: [Google]
1458519629473.jpg
13 KB, 540x489
>>53735649
 
string[] messages = new string[]{null, "Fizz", "Buzz", "FizzBuzz"};
int acc = 810092048;
int c = 0;
for (int i=1; i < = N; ++i) {
c = acc & 3;
result += (c > 0 ? messages[c] : i.ToString()) + ", ";
acc = acc >> 2 | c < < 28;
}
>>
>>53735649
for $i = 0; $i <= 100; $i++
if $i % 3 = 0
print "fizz"
else if " " 5 " "
print "buzz"
else
print $i
$i++

ironically part of my current project involves a fizz buzz

>>53735806
>if (fizz || buzz) {
> if(fizz)
> puts("fizz");
> if(buzz)
> puts("buzz");

setting a var to true might be a good solution thanks
>>
>>53735649
    std::string f[2]={"Fizz",""};
std::string b[2]={"Buzz",""};
for(int i=1;i<101;i++){
//printf("%d\n",!!(i%3));
if(i%3*i%5){
std::cout << i;
}
std::cout << f[!!(i%3)]+b[!!(i%5)]+"\n";
}

[\code]
>>
>>53735649

C++
183 :(


std::string f[2]={"Fizz",""};
std::string b[2]={"Buzz",""};
for(int i=1;i<101;i++){
if(i%3*i%5){
std::cout << i;
}
std::cout << {"Fizz",""}[!!(i%3)]+b[!!(i%5)]+"\n";
}
>>
import moe.worldclassshitposting.FizzBuzzFactory

public static void main (String [] args) {
fbf = new FizzBuzzFactory (100);
FizzBuzzer fizzBuzz = fbf.generate();
System.out.println(fizzBuzz);
}
>>
>>53736790
>>53736814
Neat. Pi or Arduino?
>>
Haskell
123
a=cycle.c;b x y z|x++y==""=z|True=x++y;c=(++)["",""];main=mapM putStrLn$zipWith3 b(a["Fizz"])(a$c["Buzz"])$map show[1..100]
>>
>>53736653
How I run this? I've tried python FizzBuzz.py but nothing happened.
>>
for i in range(1,101):
print("FizzBuzz"[i*i%3*4:8--i**4%5] or i)
>>
>>53742865
Clever. Getting rid of the linebreak/indent would shave few more bytes, though.
>>
>>53742941
it's required in python
>>
>>53742971
It's not for a "block" consisting of a single statement as in this case.
>>
>>53743027
oh, cool. sorry
>>
for x in range(1,101):
if x//3 == x/3 and x//5 == x/5:
print('FizzBuzz')
elif x//3 == x/3:
print('Fizz')
elif x//5 == x/5:
print('Buzz')
else:
print(x)


Python
191char
>>
>>53742865
>>53743027
for i in range(1,101):print("FizzBuzz"[i*i%3*4:8--i**4%5] or i)

63 characters in python
Thread replies: 106
Thread images: 7

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.