a companion discussion area for blog.codinghorror.com

FizzBuzz Solution Dumping Ground


#1

This topic is to catalog all your solutions to FizzBuzz, and is a companion to Why Can’t Programmers… Program?


Why Can't Programmers.. Program?
Why Can't Programmers.. Program?
Why Can't Programmers.. Program?
#2

In case any readers don’t know how to complete that test, here’s a solution in VBScript. To try it - save the code into “c:\test.vbs” and run “cscript c:\test.vbs” from a command prompt.

Dim i

For i = 1 to 100
    If (i Mod 3 = 0) And (i Mod 5 = 0) Then
        WScript.Echo "FizzBuzz"
    ElseIf (i Mod 3 = 0) Then
        WScript.Echo "Fizz"
    ElseIf (i Mod 5 = 0) Then
        WScript.Echo "Buzz"
    Else
        Wscript.Echo i
    End If
Next

#3

I think the above solution is not elegant.

I think

If (i Mod 3 = 0) Then
  WScript.Echo "Fizz"
End If
If (i Mod 5 = 0) Then 
   WScript.Echo "Buzz"
End If

because it covers the “both” case automatically. OK you have to care about line breaks which I don’t know in VBS (never used that crap, thank goodness).


#4
for (int i = 1; i  101; i++)
{
	if ((i % 3) == 0) Console.Write("Fizz");
	if ((i % 5) == 0) Console.Write("Buzz");
	Console.WriteLine();
}

#5
 bool printint;
            for (int i = 1; i = 100; i++)
            {
                printint = true;
                if (i % 3 == 0) { Console.Write("Fizz"); printint = false; }
                if (i % 5 == 0) { Console.Write("Buzz"); printint = false; }
                if (printint) Console.Write(i.ToString());
                Console.WriteLine();
            }

#6

Heres another solution

int i = 0;

for(i = 1; i = 100; i++)
{
  if(i % 15 == 0)
  {
    count  "FizzBuzz"  endl;
  }    
  elseif(i % 5 == 0)
  {
    count  "Buzz"  endl;
  }    
  elseif(i % 3 == 0)
  {
    count  "Fizz"  endl;
  }    
  else
  {
    cout  i  endl;
  }
 
}

#7

My weapon of choice is Ruby

1.upto(100) do |i|
  out = nil
  out = out.to_s + 'Fizz' if i % 3 == 0
  out = out.to_s + 'Buzz' if i % 5 == 0
  puts out || i
end

And did you really think you could say “only 0.5% of programmers can write FizzBuzz” without people showing you how they do it? :stuck_out_tongue:


#8

Neah… Jeff just wants us to fell all fuzzy and warm… No way I’m in like the top .5% of the programmers.
Anyway… I can do the FizzBuzz thingy… hi hi hi

for i in range(1,101): print {0:"FizzBuzz", 3:"Fizz", 6:"Fizz", 9:"Fizz", 12:"Fizz" , 5:"Buzz", 10:"Buzz"}.get(i%15, i)

#9

I just couldn’t resist:

for(int i=0;i100;printf(i%3==0?i%5==0?"Fizzbuzz":"FIZZ":i%5==0?"BUZZ":"%i",i++));

82 Characters
And yes, it’s as dirty as it would ever get … :wink:
I’ve tested it with .NET 2.0 C++ and it works just fine …


#10
public class FizzBuzz
{

    public static void main(String [] args)
    {

        int k;

        for(int i=1; i=100; i++)
        {
            k = 0;

            if(i%3==0)
            {
                k = 1;
            }

            if(i%5==0)
            {
                k = k + 2;
            }

            switch(k)
            {
                case 1:
                    System.out.println("Fizz");
                    break;
                case 2:
                    System.out.println("Buzz");
                    break;
                case 3:
                    System.out.println("FizzBuzz");
                    break;
                default:
                    System.out.println(i);
            }
        }
    }
}

#11

Forgive me, but I can’t resist…recursive fizzbuzz:
(Python)

def fizzbuzz(n):
    if n:
        if   n % 15 == 0: return fizzbuzz(n-1) + 'fizzbuzz ';
        elif n %  5 == 0: return fizzbuzz(n-1) + 'buzz ';
        elif n %  3 == 0: return fizzbuzz(n-1) + 'fizz ';
        else            : return fizzbuzz(n-1) + ('%d ' % n)
    return ''

 fizzbuzz(100)

#12

Here’s my simple C program that implements the fuzzbuzz program.

#include stdio.h

int main(void)
{
   int i,j=0;
   for(i=0;i=100;i++)
     {
        if((i%3)==0)
          {
             printf("Fizz");
             j=1;
          }
        if((i%5)==0)
          {
             printf("Buzz");
             j=1;
          }
        if(!j)
          {
             printf("%d",i);
          }
        printf("\n");
        j=0;
     }
}

#13

well … someone had to write it in C right?

#include "stdio.h"

int main ()
{
    for(int i = 1; i  101; i++)
    {
        if (i%3==0) printf("Fizz");
        if (i%5==0) printf("Buzz");
        if (i%3  i%5) printf("%d", i);
        printf("\n");
    }
    return (0);
}

#14

CF (SCRIPT) version, for your viewing pleasure…

for (i = 1; i LTE 100; i = i + 1) {
	if ((j MOD 3) AND (j MOD 5)) {
		WriteOutput(i);
	} else {
		if (NOT j MOD 3) {WriteOutput('FIZZ');}		
		if (NOT j MOD 5) {WriteOutput('BUZZ');}
	}
	WriteOutput(chr(13)  chr(10));
}

#15

most people are in it for the money. most people, like me, actually enjoy coding. What do I do before heading to bed? No, I dont read a book, I code. I get butteryflies in my stomach whenever my professors anounce a new C++ programming project; I start on it that same day and do not stop until complete–and perfect. I added a feature to that fizz-buzz program: it prints out the number which is divisible as well. Took me all of 30 seconds to do.

for(int loop = 1; loop = 100; loop++)
{
	if ((loop % 3 == 0)  (loop % 5 == 0))
	{
		cout  loop  ":\tFizzBuzz"  endl ;
	}else if (loop % 3 == 0)
	{
		cout  loop  ":\tFizz"  endl ;
	}
	else if (loop % 5 == 0)
	{
		cout  loop  ":\tBuzz"  endl ;
	}
}

btw, my fiance gets mad when i code early in the morning; i actually had to alt-tab out of the compiler so she wouldnt see it. :stuck_out_tongue:


#16

And the obligatory Haskeller chimes in with:

fizzbuzz n | n `mod` 15 == 0 = "FizzBuzz"
           | n `mod` 5  == 0 = "Buzz"
           | n `mod` 3  == 0 = "Fizz"
fizzbuzz n = show n 

main = mapM_ (putStrLn . fizzbuzz) [1..100]

That probably won’t format properly because HTML is disallowed and there’s no preview button. What’s up with that?


#17

And now for something completely different. In Lisp. Recursive. And in backwards recursion. For no reason.

(defun fizzbuzz (n)
    (when ( n 0)         
        (fizzbuzz (- n 1))
        (format t "~a~%" 
            (if (= (mod n 3) 0)
                (if (= (mod n 5) 0) "FizzBuzz" "Fizz")
                (if (= (mod n 5) 0) "Buzz" n)))))

(progn (fizzbuzz 100) (values))

#18

piece of cake. took me about a minute to write it in php
then again, i started my days hacking Z80 assembly on a ZX Spectrum…

maybe this lack of “real programmers” is because of the growth of object oriented languages + visual GUIs - and move away from being at the coalface?

here’s “fizzbuzz” in php:

for ($x=1;$x=100;$x++)
{
if ($x%3==0 and $x%5==0)
{
print "$x\tfizzbuzz\n";
}
elseif ($x%3==0)
{
print "$x\tfizz\n";
}
elseif ($x%5==0)
{
print "$x\tbuzz"."\n";
}
else
{
print "$x\n";
}


}

#19

An APL one-liner where INDEX ORIGIN is 0 :

(L,‘Fizz’ ‘Buzz’ ‘FizzBuzz’)[1+(LW=0)+W{assign}(100~0=W)+W{assign}{declose}+/1 20=3 5|{enclose}L{assign}1+{index}100]

“{index}100” creates a numeric vector from 0 to 99, “1+” brings it up to 1…100, which we {assign} to L.

“0=3 5|{enclose}L” compute the modulo of 3 and 5 for L. Enclosing vector L makes it a scalar, which can then be taken as argument of a modulo (primitives in APL requires vectors of same length on each side, or a vector and a scalar, the latter being applied to each item of said vector). This gives us a vector of two vectors (one for modulo 3, the other for modulo 5). “0=” gives us two binary vector where 1s point every solution where the number is a multiple of either 3 or 5.

“W{assign}{declose}+/1 2” multiplies these vectors by 1 and two respectively (now 1s represent modulo 3, and 2s represent modulo 5), and then we sum the two vectors. “/” is an operator which introduce the function between each item of a vector : +/1 2 3 = 1 + 2 + 3
In this case : +/(0 0 1 0 0 1 …)(0 0 0 0 2 0 …) = (0 0 1 0 0 1 …) + (0 0 0 0 2 0 …). Since the result is still a vector of vectors (with only one item), we {declose} it, which makes it a simple vector, and {assign} it to W.

“W{assign}(100~W=0)+” : we then add this vector to another where every number not 0 is replaced by 100. “~W=0” (~ means not) creates a binary vector with 1s everywhere that’s not 0, which we multiply by 100. So now we have (0 0 101 0 102 0 …). We {assign} it to W.

“1+(LW=0)+” : we use the reverse operation “=0” to create a binary vector with 1s everywhere that’s not a multiple of 3 or 5 and “L” to replace every 1 with the original number. We then add this to W, and “voil !”, every number a multiple of 3 or 5 or both is replaced by 101, 102 or 103. Since indexing starts from 0, we substract 1 from the vector “1+” (- is reserved for substraction, indicates negative numbers : 3-1 = 4).

“(L,‘Fizz’ ‘Buzz’ ‘FizzBuzz’)[ …the rest… ]” creates a vector where ‘Fizz’, ‘Buzz’ and ‘FizzBuzz’ are concatenated at the end of L, and [] indicates that we use what’s inside the brackets to index the vector to the left : ‘abc’[1 0 2] = ‘bac’

And there you go : Fizz, Buzz and FizzBuzz replace multiples of 3, 5 or both.

It is possible to create more readable code, but not as fun !

This code, which can be the body of a function, does the same thing in easy to read steps :

List{assign}1+{index}100
Fizz{assign}0=3|List
Buzz{assign}20=5|L
W{assign}Fizz+Buzz
W{assign}W+100~0=W
(L,‘Fizz’ ‘Buzz’ ‘FizzBuzz’)[1+(ListW=0)+W]


#20

My simple C version:

#include stdio.h

int
main (void)
{
int i;

    for (i=1; i101; 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);
            }
    }

    return 0;

}