Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Here is Fizz buzz

Write a program that prints the integers from 1 to 100. But for multiples of 3 print "Fizz" instead of the number and for the multiples of 5 print "Buzz". For numbers which are multiples of both 3 and 5 print "FizzBuzz".

I found the python version is much shorter, so I decided to write a short one, I have wrote several version

Table[If[# != {}, Row@#, n] &@({Fizz}[[Sign[n~Mod~3] + 1 ;;]]~Join~{Buzz}[[Sign[n~Mod~5] + 1 ;;]]), {n, 100}]

StringJoin@{If[#~Mod~3 == 0, "Fizz", ""], If[#~Mod~5 == 0, "Buzz", ""]} /. "" -> # & /@ Range@100

d = Divisible; Range@100 /. {_?(#~d~15 &) -> FizzBuzz, _?(#~d~3 &) -> Fizz, _?(#~d~5 &) -> Buzz}

Can you show a more shorter one?

share|improve this question
Related meta: meta.mathematica.stackexchange.com/q/136/5 –  rm -rf Jan 30 '13 at 16:56
@chyanog As rm -rf linked, code-golf challenges are generally frowned upon in this forum. I do however think you could make a slight edit of your question and instead of asking for specific versions of fizz-buzz ask for general techniques that can be applied within Mathematica in code-golf settings. I personally would like to see such a question answered. –  jVincent Jan 30 '13 at 17:33
I understand "shorter" in terms of lexemes rather than characters. This would eliminate trivial moves like d=Divisible (which merely replaces one symbol by another) designed solely to shrink the character count without changing how the code actually works, and helps focus the mind on the computation itself. –  whuber Jan 30 '13 at 18:51
It's still code-golf and a very tired, stale example at that. –  m_goldberg Jan 30 '13 at 18:53
@m_goldberg oh well, I had fun with it anyway. –  Mr.Wizard Jan 31 '13 at 10:41

6 Answers 6

up vote 18 down vote accepted

67 63 56 55 (47?) characters



In the rule-bending spirit of Code Golf, 47 characters:

Pick[Fizz^Buzz,#~Mod~{3,5},0]/. 1->#&~Array~100

Mathematica graphics

share|improve this answer
Very nice solution! –  chyaong Jan 31 '13 at 8:45
@chyanog Thanks :D –  Mr.Wizard Jan 31 '13 at 8:48
+1 Very nice :-) –  Simon Woods Jan 31 '13 at 11:27
@Simon I had to post it before you did! ;-) –  Mr.Wizard Jan 31 '13 at 11:29

53 characters

I can't beat Mr Wizard, but anyway:

share|improve this answer
It's just there to convert 15 to 4, without the Mod I would need "fizzbuzz" at position 15 in the list, which is a lot of commas! I could also use modulo 13, or even 15 for the rather nice fizzbuzz[#,,fizz,,buzz][[#~GCD~15~Mod~15]]&~Array~100 –  Simon Woods Jan 31 '13 at 11:44
Yes, I figured it out after walking through the code. Again I really like this approach and I wish I could claim that I thought of something similar but I surely did not. –  Mr.Wizard Jan 31 '13 at 11:45

One method of shortening (and obfuscating!) code with repetitive specifications uses a function that writes functions:

w[i_, s_] := f[x_] /; Divisible[x, i] := s

It's now quick work to use w to write the solution, beginning with the default of just echoing the input:

f[x_] := x
LCM[3,5]~w~FizzBuzz; 3~w~Fizz; 5~w~Buzz

(LCM is used to avoid doing any precomputation. Because this case is intended to override the other cases, it must appear first in the list of definitions.) Apply it:

Array[f, 100]
share|improve this answer
This is still longer than the OP's third method, even with 15~w~FizzBuzz and f@x_:=x etc. –  Mr.Wizard Jan 31 '13 at 2:04
@Mr.Wizard That is correct: I am taking a (now deleted) comment to the question seriously; namely, to respond in terms of general techniques to address such questions. Otherwise, I agree with that comment: there is no place on our site for endless "code golf" solutions. We should also be interested in solutions that generalize well, as in the solution by jVincent--and especially in your elegant solution! –  whuber Jan 31 '13 at 3:06
Okay, I see your perspective. Sorry for being harsh. I guess it would sit better with me if you had demonstrated this method actually making the code shorter. Though I know I am in the minority I actually learn from code-golf, both from the process of working things out and from seeing other answers; since I am a greater fan of the process let me recommend this addition to your answer as a demonstration of efficacy of your method: (f@x_ /; x~Mod~# == 0 = #2) & @@@ {15 | FizzBuzz, 3 | Fizz, 5 | Buzz, 1 | x}. (continued) –  Mr.Wizard Jan 31 '13 at 7:50
Though this uses x dangerously, a common Golf failing, it also shows something useful in the use of |: many users often forget that Apply works with heads besides List. –  Mr.Wizard Jan 31 '13 at 7:51

The problem is essentially to apply a test and provide output according to the test. Often the most straight forward construct in Mathematica is also among the most concise. Though in the name of cutting characters you can substitute call syntax for prefix and infix when it saves characters:

share|improve this answer
+1 That is an instructive use of Switch. This solution generalizes readily to more moduli, as in With[{moduli = {2, 3, 5, 7}, names = {Huh, Fizz, Buzz, Whap}}, Switch[Mod[#, moduli], Evaluate[ Sequence @@ Riffle[Tuples[{0, _}, n = Length[moduli]], Tuples[{1, 0}, n] . names /. {Plus -> Alternatives, 0 -> #}]]] ] &~Array~210 –  whuber Jan 30 '13 at 21:15

60 characters, a simple rule-driven solution:


(Nice to see it being interpreted correctly by MMA kernel.)

share|improve this answer
59 characters: #/.Join@@({15#->FizzBuzz,3#->Fizz,5#->Buzz}&/@#)&@Range@100 –  chyaong Aug 5 '13 at 15:22
@chyanog 56: #/.(##&[15#->FizzBuzz,3#->Fizz,5#->Buzz]&)/@#&@Range@100 –  Mr.Wizard Aug 5 '13 at 18:14
@Mr.Wizard, Good job! –  chyaong Aug 6 '13 at 14:05

Constrained patterns can often be used to find simple literal solutions to some problems.

Range@100 /. {(x_ /; IntegerQ[x/3] && IntegerQ[x/5]) -> "FizzBuzz", 
              (x_ /; IntegerQ[x/3]) -> "Fizz", 
              (x_ /; IntegerQ[x/5]) -> "Buzz"}
share|improve this answer
This is almost the same as the OP's third solution with #~Divisible~..& replaced by IntegerQ. I like how the reference to 15 has been replaced by a conjunction of criteria, which is closer to the problem statement. –  whuber Jan 30 '13 at 19:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.