a companion discussion area for blog.codinghorror.com

FizzBuzz Solution Dumping Ground


#68

Very easy in C#:

private void FizzBuzz() {
string fizz;
string buzz;

for (int i = 1; i = 100; i++) {
    fizz = null; buzz = null;

    if (i % 3 == 0) {
        fizz = "Fizz";
    }
    if (i % 5 == 0) {
        buzz = "Buzz";
    }
    if (fizz != null || buzz != null) {
        Console.WriteLine(fizz + buzz);
    }
    else {
        Console.WriteLine(i.ToString());
    }
}

}


#69

Surely this is a test about reading the question (i.e. outputting the numbers)
and efficiency, minimising the number of conditional tests.

So the mod3 mod5 elegant solution misses the non replaced numbers and does 2 conditional checks per loop

This is better:

while(i++ i=100)
{
if(i%3)//most likely so first
write(“Fizz”)
if((i%5))//next likely
write(“Fizz”)
else if (i%5))//next likely
write(“Fizz”)
else
write(i)
}

even with this for a non replaced number all 3 conditions are checked first


#70

Writing a program that satisfies your FizzBuzz specification is trivial in any language. The solve the specification you can simply write out the precomputed solution:

println(“1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz …”);

This gives you also the best runtime performance …


#71

//define your own output method with TRACE
void fizzbuzz(int i = 1)
{
if (i 100) return;
(i%15)?(i%3)?(i%5)?TRACE("%d",i):TRACE(“Buzz”):TRACE(“Fizz”):TRACE(“FizzBuzz”);
TRACE("\n");
fizzbuzz(++i);
}
void main()
{
fizzbuzz();
}
//yep, I know this kinf of code is a nightmare for thos who will mantain this code later :wink:


#72

Dim i As Integer = 1
Dim newline = False

    For i = 1 To 100
        Response.Write("br/")
        If (i Mod 3 = 0) Then
            Response.Write("Fizz")
        End If
        
        If (i Mod 5 = 0) Then
            Response.Write("Buzz")
        ElseIf (i Mod 3  0 And i Mod 5  0) Then
            Response.Write(i)
        End If            
    Next
End Sub

#73

for (int i=0;i101 ;i++)
{
System.out.println((i%3==0)?((i%5==0)?“fizzbuzz”:“fizz”):(i%5==0)?“buzz”:i+"");
}

programmers are idiots.


#74

PHP:

while(++$i101) {
if(!($i%15)) echo “FizzBuzz\n”;
else if(!($i%3)) echo “Fizz\n”;
else if(!($i%5)) echo “Buzz\n”;
else echo “$i\n”
}


#75

Welcome to school :slight_smile:

For the VBScripters that were arguing over carriage returns, a CSscript solution in about 30 seconds:
Dim i
For i = 1 to 100
If i mod 3 = 0 Then WScript.StdOut.Write "Fizz"
If i mod 5 = 0 Then WScript.StdOut.Write "Buzz"
If CBool(i mod 3) And CBool(i Mod 5) Then WScript.StdOut.Write i
WScript.Echo ""
Next

Next we use VBScript to do it in ASP (ok, this is cheating :stuck_out_tongue: ):
%
Option Explicit

Dim i
For i = 1 to 100
If i mod 3 = 0 Then Response.Write "Fizz"
If i mod 5 = 0 Then Response.Write "Buzz"
If CBool(i mod 3) And CBool(i Mod 5) Then Response.Write i
Response.Write "br/"
Next
%

Then something else…python?
a = []
for i in range(1,101):
a.append((i%15==0) and “FizzBuzz” or ((i%5==0) and “Buzz” or ((i%3==0) and “Fizz” or str(i))))
print a

Damn, time to take a shower and go to work. I think I just wasted most of my morning reading the other replies before I spent a couple minutes writing those three :stuck_out_tongue:


#76

Another C# solution:

for (int i = 1; i = 100; i++) {
if (i % 3 == 0 || i % 5 == 0) {
if (i % 3 == 0) {
Console.Write(“Fizz”);
}
if (i % 5 == 0) {
Console.Write(“Buzz”);
}
}
else {
Console.Write(i.ToString());
}
Console.Write("\n");
}


#77

i’m sick of seeing C/C++ and other {} enclosed solutions so here’s my VB6 version. spent less than 5 minutes on it, which is terrible going by other’s standards.

dim num as byte, printstr as string
for num=1 to 100
printstr=""
if num mod 3 = 0 then printstr=printstr “Fizz"
if num mod 5 = 0 then printstr=printstr “Buzz"
if printstr”” then
debug.print printstr
else
debug.print num
end if
next

btw, i’m not proving myself. i’m 18, and i’ve already tried making a 2d game in VB6. oddly enough, it failed.

all this degrading of other applicants has made me want to apply for a programming job now! still at school for 3 months though.
God, i’m so arrogant.


#78

Just for the sake of perversity, here’s a solution written as a Windows .BAT file:

@echo off

for /L %%I in (1,1,100) do call :FizzBuzz %%I
goto :EOF

:FizzBuzz
set I=%1
set /a T3=I %% 3
set /a T5=I %% 5
set /a T35=T3 + T5

if not %T35%==0 goto FizzBuzz5
echo FizzBuzz
goto :EOF

:FizzBuzz5
if not %T5%==0 goto FizzBuzz3
echo Buzz
goto :EOF

:FizzBuzz3
if not %T3%==0 goto FizzBuzzEcho
echo Fizz
goto :EOF

:FizzBuzzEcho
echo %I%
goto :EOF


#79

In action Script ^^

for (var a:Number = 0; a100; a++) {
if (a%15 == 0) {
trace(“Fizz-Buzz”);
} else if (a%5 == 0) {
trace(“Buzz”);
} else if (a%3 == 0) {
trace(“Fizz”);
} else {
trace(a);
}
}

or

for (var a:Number = 0; a100; a++) {
trace(a%15 == 0 ? “Fizz-Buzz” : a%5 == 0 ? “Buzz” : a%3 == 0 ? “Fizz” : a);
}

nice lol …


#80

It’s so BASIC:

for i = 1 to 100
a$ = ""
IF i mod 3 = 0 THEN a$ = a$+“Fizz"
IF i mod 5 = 0 THEN a$ = a$+“Buzz"
IF a$=”” THEN print i ELSE print a$
next i

30 seconds (but no PhD)


#81

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.


#82

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”);


#83

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


#84

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… :wink:


#85

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;


#86

78 chars:

perl -le’print$%15==0?“FizzBuff”:blush:%3==0?“Fizz”:blush:%5==0?“Buzz”:blush: 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


#87

"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!