Make the code shorter for solving Fizz buzz

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?

• 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

676356 55 (47?) characters

Better:

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


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

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


• Very nice solution! – chyanog Jan 31 '13 at 8:45
• @chyanog Thanks :D – Mr.Wizard Jan 31 '13 at 8:48
• @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:

{#,,fizz,fizzbuzz,buzz}[[#~GCD~15~Mod~11]]&~Array~100

• 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]

• 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:

Switch[#~Mod~{3,5},{0,0},FizzBuzz,{0,_},Fizz,{_,0},Buzz,_,#]&~Array~100

• +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:

#/.Flatten[{15#->FizzBuzz,3#->Fizz,5#->Buzz}&/@#]&@Range@100


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

• 59 characters: #/.Join@@({15#->FizzBuzz,3#->Fizz,5#->Buzz}&/@#)&@Range@100 – chyanog 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! – chyanog Aug 6 '13 at 14:05
• @Akater Could you explain how your solution works? I'm confused by the multiple uses of #. – Mitchell Kaplan Apr 18 '18 at 21:38
• @MitchellKaplan 15#, 3#, 5# is interpreted as multiplication, Times[15, #] and so on. I generate rules that replace exact numbers with words and apply them to the list {1, ..., 100} generated in the enveloping function. If you use FrontEnd, you may select the expression you're uncertain about, expand selection with multiple clicks or a keyboard shortcut (don't remember which one already) and convert the selected expressions into FullForm. – akater Jul 24 '18 at 23: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"}

• 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