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 …
//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
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
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 ):
%
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
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");
}
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.
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!).
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
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…
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:
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;