Fizz Buzz In Many Programming Languages

The FizzBuzzTest inspiring many people to code up quick and dirty solutions.

...many quick. And quite a few dirty. Many "dirty" to the point of not actually working correctly!!!


See:


http://tickletux.wordpress.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/

contains examples in


My ridiculously over-generalized solution in ScalaLanguage:

 def replaceMultiples(x: Int, rs: (Int, String)*) =
rs map {case (n, s) => Either cond (x % n == 0, s, x)} reduceLeft ((a, b) =>
a fold ((_ => b), (s => b fold ((_ => a), (t => Right(s + t))))))

def fizzbuzz(n: Int) = replaceMultiples(n, 3 -> "Fizz", 5 -> "Buzz") fold ((_ toString), identity)

1 to 100 map fizzbuzz foreach println
but at least it's DRY..."Fizz" and "Buzz" appear only once each. :)


My not so geneneralized ScalaLanguage solution that does not use the modulo operator:

object FizzBuzz extends App {

  val three = (3 to 100 by 3) toSet
  val five = (5 to 100 by 5) toSet

case class M(nums: Set[Int], phrase: String)

val t = List(M(three intersect five, "FizzBuzz"), M(three, "Fizz"), M(five, "Buzz"))

1 to 100 map { n => t.find(m => m.nums contains n) match { case Some(m) => m.phrase case default => n.toString } } foreach println
}

- JanekBogucki


Obvious in VBA

 Public Sub FizzBuzz()
a = Array("", "Fizz", "", "Buzz", "")
For n = 1 To 100
f = a(1 + Sgn(n Mod 3)) & a(3 + Sgn(n Mod 5))
Debug.Print IIf("" = f, n, f)
Next n
 End Sub

And R
 fzbz <- function(n){c(n,"fizz","buzz","fizzbuzz")[1+(!n%%3)+2*(!n%%5)]}
 for (a in 1:100) print(fzbz(a))
-- MarcThibault


Here is the one in TeX:

 \newcount\x
 \newcount\y
 \newcount\z
 \loop
\advance\x by 1
\advance\y by 1
\advance\z by 1
\ifnum\y=3 Fizz\y=0 \fi
\ifnum\z=5 Buzz\z=0 \fi
\ifvmode \the\x \fi\endgraf
 \ifnum\x<100 \repeat
 \bye
Golf:
 \let~\advance\time0\day0\loop~\time1~\day1~\mit\ifnum\time=3\time0Fizz\fi\ifnum\fam=5Buzz\rm\fi\ifvmode\the\day\fi\endgraf\ifnum\day<100\repeat\bye


Readable perl version

  my $i = 1;
  while ($i <= 100) {
if ($i % 3 == 0 && $i % 5 == 0) {
print "FizzBuzz\n";
} elsif ($i % 3 == 0) {
print "Fizz\n";
} elsif ($i % 5 == 0) {
print "Buzz\n";
} else {
print $i . "\n";
}
$i++
  }


Lua:

  for i = 1,100 do
local n = false
if i % 3 == 0 then
io.write("Fizz")
n = true
end
if i % 5 == 0 then
io.write("Buzz")
n = true
end

if not n then io.write(tostring(i)) end

io.write("\n") end


ColdFusion:

  <cfset index = 0 />
  <cfloop index="index" from="1" to="100">
  <cfset output = "#index#" />
  <cfif index MOD 3 EQ 0 OR index MOD 5 EQ 0><cfset output = "" /></cfif>
  <cfif index MOD 3 EQ 0><cfset output &= "Fizz" /></cfif>
  <cfif index MOD 5 EQ 0><cfset output &= "Buzz" /></cfif>
  <cfoutput>#output# <br /></cfoutput>
  </cfloop>


Python: for i in ("fizzbuzz" if i % 15 == 0 else ("fizz" if i % 3 == 0 else ("buzz" if i % 5 == 0 else i)) for i in range(1, 101)): print i


Lua (another): for i=1,100 do

-- Set fizzBuzz to "", "Fizz", "Buzz", or "FizzBuzz" based on mod 3 and mod 5 tests.
local fizzBuzz = (( i % 3 == 0 ) and "Fizz" or "") .. (( i % 5 == 0 ) and "Buzz" or "")
-- Print non-"" fizzBuzz or print i. 
print( (fizzBuzz ~= "" and fizzBuzz) or i )
end


C++ (with one abuse of control characters to avoid excess bools or ifs):

  #include <iostream>
  int main()
  {
    for(int i = 1; i <= 100; ++i)
    {
      std::cout << i << '\r'; // return to beginning of line for overwriting with "Fizz" or "Buzz"
      if(i % 3 == 0) std::cout << "Fizz";
      if(i % 5 == 0) std::cout << "Buzz";
      std::cout << '\n';
    }
    return 0;
  }


CategoryInManyProgrammingLanguages


EditText of this page (last edited November 25, 2014) or FindPage with title or text search