 # FizzBuzz Solution Dumping Ground

To the perl 1 liner author:
for (1…100) {print \$_ unless ((\$_ % 3 ? 0 : print “Bizz”) || (\$_ % 5 ? 0 : print “Fuzz”));}

When you hit multiples of 3 and 5, it doesn’t print BizzFuzz. Nice try though.

Here’s my favorite implementation. It is better then everyone else’s because it does not require a single modulo calculation! It can also fit on one line (a really long line!).

printf(“1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\n10\n11\n … Fizz\n100”);

1 Like

Thought I’d post a version in bash, just for kicks:

#!/bin/bash

TEST_DIVISOR_1=3
TEST_DIVISOR_2=5
UPPER_LIMIT=100

i=1
while [ \$i -lt \$((\$UPPER_LIMIT+1)) ];
do
printed_text=0
if [ \$((\$i%\$TEST_DIVISOR_1)) -eq 0 ]; then
echo -n "Fizz"
printed_text=1
fi
if [ \$((\$i%\$TEST_DIVISOR_2)) -eq 0 ]; then
echo -n "Buzz"
printed_text=1
fi
if [ \$printed_text -eq 0 ]; then
echo -n \$i
fi
echo ""
i=\$((\$i+1))
done

Plain C:

int i;
for (i = 0; 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);
}
I think that one word/number per line is more readable for such a useful piece of software… no one has done this one either ( I like the ‘?’ operator). Its wonderfully confusing and only two statements:

for(int i=1;i=100;i++)
printf( !(i%3) || !(i%5) ? “%s\n”: “%d\n”,
!(i%3) !(i%5) ? “FizzBuzz”:
!(i%3) ? “Fizz”:
!(i%5) ? “Buzz”:
i);

If you’re system does not have modulo (oldschool!) or you’re afraid that doing modulo may be a big performance problem (1khz clockspeed) here’s a solution that requires only additions:

int m3 = 1;
int m5 = 1;
for(int i=1;i=100;i++) {
printf( !m3 || !m5 ? “%s\n”: “%d\n”,
!m3 !m5 ? “FizzBuzz”:
!m3 ? “Fizz”:
!m5 ? “Buzz”:
i);
if(++m3 == 3)
m3 = 0;
if(++m5 == 5)
m5 = 0;
}

Or if you wanted to make the second half of the loop more complicated and wanted to get rid of all branching (only smart compilers would get rid of the branching), then you could do this:
m3 += (m3 == 2) ? -2 : 1;
m5 += (m3 == 4) ? -4 : 1;

78 chars:

perl -le’print\$%15==0?“FizzBuff” %3==0?“Fizz” %5==0?“Buzz” for 1…100’

Using naughty syntax we can get it down to 72 chars:

perl -le’print\$%15==0?FizzBuff:\$%3==0?Fizz:\$%5==0?Buzz:\$ for 1…100’

Here’s the same program with sane indenting: http://rafb.net/p/QLtUvx47.html

example output:

1
2
Fizz
4
Buzz
Fizz

FizzBuff
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz
END

"90% of people can’t touch one’s elbow with one’s tongue."
Hmm, can i?

for(int i=1;i=100;i++)
{
printf(i%3==0i%5==0?“fizzbuzz\n”:(i%3==0?“fizz\n”:(i%5==0?“buzz\n”:"%d\n")),i);
}

Have a nice day!

Love my old VB. Do it all backwards.

``````List1.Clear
Dim n As Integer, out As String
For n = 1 To 100
out = vbNullString
out = IIf(n Mod 3 = 0, "Fizz", vbNullString) + IIf(n Mod 5 = 0, "Buzz", vbNullString)
If out = vbNullString Then out = n
Next
``````

Blazing fast LUT implementation by a coworker:

#include stdio.h

#define max 1000000

int main(void)
{
char X = “%d \n%d \nfizz \n%d \nbuzz \nfizz \n%d \n%d \nfizz \nbuzz \n%d \nfizz \n%d \n%d \nfizzbuzz\n\0”;
int modu = max % 15;
int full = max - modu;
unsigned int a;
for(a = 1; a full; a += 15)
printf(X, a, a + 1, a + 3, a + 6, a + 7, a + 10, a + 12, a + 13);
X[modu * 9] = 0;
printf(X, a, a + 1, a + 3, a + 6, a + 7, a + 10, a + 12, a + 13);
}

on second thoughts

``````For n = 1 To 100
out = IIf(n Mod 3 = 0, "Fizz", vbNullString)
out = IIf(n Mod 5 = 0, out + "Buzz", out)
out = IIf(out = vbNullString, n, out)
Next
``````

is more consistent

since you guys coded the solution to the FizzBuzz problem in most of the language and most of the language i know is C based so maybe i thought make an algorithm/pseudo code maybe that’s where most new graduates failed to do or atleast imagine in their heads on how to come up with a solution.

1. start
2. declare variable named i
3. declare variables named x and y
4. set x to the remainder of i devided by 3
5. set y to the remainder of i devided by 5
9. print the value of i
10. if i is less than or equal to 100 jump increment i by 1 and and jump to step 4 else jump to step 11
11. print the word “Done”
12. end

or at least master the flowchart in their head.

Saw many implementation but no BASH script version, figured I’d include that:

#!/bin/bash

declare CNT=0

while [ \$CNT -le 100 ]; do
[ \$((CNT % 3)) -eq 0 ] echo -n “Fizz”
[ \$((CNT % 5)) -eq 0 ] echo -n “Buzz”
[ \$((CNT % 3)) -ne 0 -a \$((CNT % 5)) -ne 0 ] echo -n \$CNT
echo ""
CNT=\$((CNT+1))
done

I’m just leaning java.
more familiar with vb.vet
took 3 minutes - with all of that spent on
sorting out (still unfamiliar) if else {} syntax.

for (int i = 0; i = 100; i++) {
if (i % 3 == 0 i % 5 == 0)
System.out.println(“fizzBuzz_3_5”);
else if (i % 3 == 0)
System.out.println(“fizz_3”);
else if ( i % 5 == 0 )
System.out.println(“buzz_5”);
else
System.out.println(i);
}

But do they have COMMUNICATION/TEAMWORK skills? Another MS SQL version - not elegant, but it works:
note: Begins and Ends not required but included for readability

declare @loopcount int

set @loopcount = 100

while (@loopcount 2)
begin
if @loopcount % 3 = 0
Begin
print ‘Fizz’ + cast(@loopcount as char)
End
else
if @loopcount % 5 = 0
Begin
print ‘Bang’ + cast(@loopcount as char)
End

``````	if (@loopcount % 3 = 0 and @loopcount % 5 = 0)
Begin
print 'FizzBang'   + cast(@loopcount as char)
End

set @loopcount = @loopcount -1
``````

end

// Here’s my attempt at an elegant C(++) solution.
// I’ll assume the appropriate headers are included.
// This snippet can be put into main or elsewhere.
// I’m a bit rusty with the details, as I haven’t
// C++'ed in years.

#DEFINE FACTOR0 3 // Using macros for easier portability to other
#DEFINE STRING0 “Fizz” // uses, without increasing code size, memory
#DEFINE FACTOR1 5 // needs, or decreasing performance.
#DEFINE STRING1 “Buzz”

for(int i=1; i=100; i++;)
{if(!(i%FACTOR0)) printf(STRING0);
elseif(!(i%FACTOR1)) printf(STRING1);
else printf("%i", i);
printf("\n");}

I cann’t resist… some people forget to print the number and another prints the number and the word… The requirements say: “Instead of the number print the word”

for (int i=0; i101; i++)
{
if (i%3==0)//Print if is multiple of 3 print Buzz and not the #
{
cout “Fizz”;
}
else//not multiple of 3
{
if (i%5==0)//Print if is multiple of 5 print Buzz and not the #
{
cout “Buzz”;
}
else//not multiple of 5
{
if (i%5==0 i%3==0)//Print if is multiple of 3 AND 5 print
//Fizzbuzz and not the #
{
cout “Fizzbuzz”;
}
else//not multiple of 3 or 5… then print the number
{
cout i;
}
}
}
cout"\n"; //just endline for every case
}

Thanks… it works for C++, Java or C# just change the COUT lines for the output.

My PHP version:
for ( \$i = 1; \$i = 100; \$i++){
if ( \$i % 3 == 0 ) \$print = ‘Fizz’;
if ( \$i % 5 == 0 ) \$print .= ‘Buzz’;
if ( !isset(\$print) ) \$print = \$i;
echo \$print.‘br /’;
unset(\$print);
}
Is clear and easy

pft…its really easy although time consuming. Thats why I can’t see this as a way to test programmers skills… it took me like 30 minutes to solve this problem. each candidate * 30 minutes…figure it out!

anyway, here is my solution. (I had to use calculator after 25)

print 1
print 2
print "Fizz"
print 4
print "Buzz"
print "Fizz"
print 7
print 8
print "Fizz"
print "Buzz"
print 11
print "Fizz"
print 13
print 14
print "FizzBuzz"
print 16
print 17
print "Fizz"
print 19
print "Buzz"
print "Fizz"
print 22
print 23
print "Fizz"
print "Buzz"
print 26
print "Fizz"
print 28
print 29
print "FizzBuzz"
print 31
print 32
print "Fizz"
print 34
print "Buzz"
print "Fizz"
print 37
print 38
print "Fizz"
print "Buzz"
print 41
print "Fizz"
print 43
print 44
print "FizzBuzz"
print 46
print 47
print "Fizz"
print 49
print "Buzz"
print "Fizz"
print 52
print 53
print "Fizz"
print "Buzz"
print 56
print "Fizz"
print 58
print 59
print "FizzBuzz"
print 61
print 62
print "Fizz"
print 64
print "Buzz"
print "Fizz"
print 67
print 68
print "Fizz"
print "Buzz"
print 71
print "Fizz"
print 73
print 74
print "FizzBuzz"
print 76
print 77
print "Fizz"
print 79
print "Buzz"
print "Fizz"
print 82
print 83
print "Fizz"
print "Buzz"
print 86
print "Fizz"
print 88
print 89
print "FizzBuzz"
print 91
print 92
print "Fizz"
print 94
print "Buzz"
print "Fizz"
print 97
print 98
print "Fizz"
print “Buzz”

Method with one less if statement since 3 and 5 aren’t mutually exclusive and variable names.

void main()
{
for (int i = 1; i = 100; i++)
{
bool isdiv3 = !(i % 3);
bool isdiv5 = !(i % 5);
if (isdiv3) cout “Fizz”;
if (isdiv5) cout “Buzz”;
if (!(isdiv3 || isdiv5)) cout i;
cout “\n”;
}
}

Ignoring the language for now, the simplest way to do this is IMO:

for (int i = 0; i 101; ++i)
{
if (i % 3 == 0) then
{
printf(“Fizz”);
}
else
{
if (i % 5 == 0) then
{
printf(“Buzz”);
}
else
{
printf("%d", i);
}
}
}