# Code-golfing FizzBuzz

**URL:** <https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354>\
**Category:** General Usage\
**Created:** [November 16, 2019, 3:00am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354 "2019-11-16T03:00:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [November 16, 2019, 3:00am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/1 "2019-11-16T03:00:44Z")

</div>

Just realised there is no Julia soluton to this on Code golf [code golf - 1, 2, Fizz, 4, Buzz - Code Golf Stack Exchange](https://codegolf.stackexchange.com/questions/58615/1-2-fizz-4-buzz)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 16, 2019, 6:07am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/2 "2019-11-16T06:07:18Z")

</div>

That’s a very weird discussion, dominated by esoteric programming languages.

Incidentally, do people _really_ ask for this in an interview? I would imagine that simply looking at code on someone’s public FOSS contributions would be a much quicker and more specific test. If a significant number of interviewees are filtered out by this in a process, then the interviewers are not using their own time efficiently.

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [November 16, 2019, 1:35pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/3 "2019-11-16T13:35:34Z")

</div>

I’ll bite. Here’s a moderately respectable 69 chars:

```julia
(i->(f=i%3≈0;b=i%5≈0;println("Fizz"^f*"Buzz"^b*"$i"^!(f|b)))).(1:100);

```

---

<div class="post-metadata">

**Author:** ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Post date:** [November 16, 2019, 2:08pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/4 "2019-11-16T14:08:33Z")

</div>

I just submitted some results to code golf for laughs:

> <https://codegolf.stackexchange.com/questions/58615/1-2-fizz-4-buzz/195962#195962>

Julia 87 bytes

```julia
z(i)=(f=i .%[3,5] .==0;sum(f)>0 ? foldl(*,["Fizz","Buzz"][f]) : i)
println.(z.(1:100))

```

Or we could kind of cheat to drop 3 bytes and use show instead of println.  
Julia 83 bytes

```julia
z(i)=(f=i .%[3,5] .==0;sum(f)>0 ? foldl(*,["Fizz","Buzz"][f]) : i)
show(z.(1:100))

```

or a more legible FP style, poor performing, 91 bytes

```julia
F(x)=foldl(*,["Fizz","Buzz"][x .%[3,5] .==0])
N(y)=F(y)=="" ? y : F(y)
println.(N.(1:100))

```

---

<div class="post-metadata">

**Author:** ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Post date:** [November 16, 2019, 2:10pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/5 "2019-11-16T14:10:08Z")

</div>

Looks like you beat me with 75 bytes! Nice one! Maybe swap out println with show?

maybe this counts 😛  
71 bytes

```julia
(i->(f=i%3≈0;b=i%5≈0;show("Fizz"^f*"Buzz"^b*"$i"^!(f|b)))).(1:100)

```

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [November 16, 2019, 6:42pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/6 "2019-11-16T18:42:50Z")

</div>

Isn’t a FizzBuzz code supposed to give its output in precisely this form?

> <https://github.com/Keith-S-Thompson/fizzbuzz-polyglot/blob/master/expected-output.txt>

---

<div class="post-metadata">

**Author:** ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Post date:** [November 16, 2019, 8:52pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/7 "2019-11-16T20:52:48Z")

</div>

Think so. But, I dunno if they care if some other things like " 's are in there or not.

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [November 17, 2019, 3:35am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/8 "2019-11-17T03:35:05Z")

</div>

My reading of their rules were that we needed to print to stdout in the standard format so I think `show` is out 🙂

I missed that they were scoring in bytes rather than characters. That’s a bit of a disappointment, it reduces the golfing opportunities a bit. In that case we may as well use `==` rather than `≈` for a total of 71 bytes (also dropping the trailing `;` if not using it at the REPL.)

```julia
(i->(f=i%3==0;b=i%5==0;println("Fizz"^f*"Buzz"^b*"$i"^!(f|b)))).(1:100)

```

There may be other options. An observation related to one of the python solutions is that `"FizzBuzz$i"[n:m]` would do the trick for the right `n` and `m`.

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [November 17, 2019, 6:24am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/9 "2019-11-17T06:24:56Z")

</div>

You can do ‘i%3\<1’ for 2 bytes.

---

<div class="post-metadata">

**Author:** ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Post date:** [November 17, 2019, 1:12pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/10 "2019-11-17T13:12:35Z")

</div>

Awe well I tried! Awesome solution though!

I also submitted some code golfs to a few other simple ones. I know Julia isn’t made to do this sort of thing, and it’s really an obtuse use of a programming language - but it’s kind of fun and could give some exposure.

> <https://codegolf.stackexchange.com/questions/289/shortest-floor-function/195981#195981>

> <https://codegolf.stackexchange.com/questions/195964/n-dimensional-cartesian-product/195975#195975>

I also made a code golf:

> <https://codegolf.stackexchange.com/questions/196000/euclidean-distance-between-two-matrices>

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 17, 2019, 2:20pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/11 "2019-11-17T14:20:51Z")

</div>

I find it interesting that the languages that “win” by the code length criterion range from the somewhat obscure to the very esoteric.

OTOH, the “verbose” languages are actually those that most people prefer to program in, especially for nontrivial projects.

There may be a lesson here 😉

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [November 17, 2019, 2:23pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/12 "2019-11-17T14:23:37Z")

</div>

Not sure if people prefer Java or they just learnt it at uni or forced to use it at work.

---

<div class="post-metadata">

**Author:** ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Post date:** [November 17, 2019, 2:27pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/13 "2019-11-17T14:27:27Z")

</div>

I couldn’t agree more. Most of those languages make me sick to my stomach! It is interesting to see some of the solutions in the sane languages with usable syntax though.

If I were ever hiring someone and they actually wrote something in brainfuck, hexagony, labrynth, or whatever the hell I would have serious concerns about their ability to color within the lines. I had a running gag at a previous job that if I were ever interviewing someone and they said the word “Mortran” I would immediately close the interview and leave the room.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [November 18, 2019, 12:34am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/14 "2019-11-18T00:34:49Z")

</div>

> [@anon92994695](#):
>
> were ever hiring someone and they actually wrote something in brainfuck, hexagony, labrynth, or whatever the hell I would have serious concerns about their ability to color within the lines.

I have the opposite feeling. I thikk they are pretty smart. But jot sure if they have the patience to do boring work like colouring within the lines.

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [November 18, 2019, 1:18am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/15 "2019-11-18T01:18:00Z")

</div>

Oh yes of course! So we have the following for 69 bytes:

```julia
(i->(f=i%3<1;b=i%5<1;println("Fizz"^f*"Buzz"^b*"$i"^!(f|b)))).(1:100)

```

It seems likely a different approach would be needed to make much more progress.

> [@anon92994695](#):
>
> I know Julia isn’t made to do this sort of thing, and it’s really an obtuse use of a programming language

Abusing the tools is the fun of it! I really wanted to use more broadcast in the solution like your nice solution where you have `foldl` and `i .%[3,5] .==0` but the reduction consumes a lot of characters. Here’s a related 78 byte solution:

```julia
(i->(f=i.%[3,5].<1;println(join(["Fizz","Buzz","$i"][[f;sum(f)<1]])))).(1:100)

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 18, 2019, 7:14am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/16 "2019-11-18T07:14:16Z")

</div>

I don’t think that any of those esoteric languages (eg Keg, Mouse 2002, etc) are used to write anything nontrivial (I am not talking about production, just something one would do in 100 LOC in Julia). They may be fun to play with, but I am not sure even their inventors would argue that they would be an improvement over even the dullest mainstream languages.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [November 18, 2019, 12:29pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/17 "2019-11-18T12:29:31Z")

</div>

I can only manage one letter shorter than yours, for 68 (letters, but not bytes) not including the semicolon needed for the REPL:

```julia-auto
julia -e '(i->(x="Fizz"^(i%3≈0)*"Buzz"^(i%5≈0);println(x>"" ? x : i))).(1:100)'

```

> [@c42f](#):
>
> I missed that they were scoring in bytes rather than characters. That’s a bit of a disappointment, it reduces the golfing opportunities a bit. In that case we may as well use `==` rather than `≈`

Right (I did too) but you can avoid == and use \<, and with ≈, this cryptic code doesn’t make up for it:

```julia-auto
for i = 1:100 f,b=.≈((i%3,i%5), 0);println("Fizz"^f*"Buzz"^b*"$i"^!(f|b))end

```

I can’t take full credit for my best solution, as I combined bits from yours and what was actually there in this answer from 2015 from Alex A (which no longer works in current Julia, I had to add spaces, then actually getting 68 bytes, not just letters):

# Julia, 64 bytes

```julia-auto
for i=1:100 x="Fizz"^(i%3<1)*"Buzz"^(i%5<1);println(x>""?x:i)end

```

**EDIT:** Off-topic below, just cool solutions for Julia to beat (but I doubt possible, still some solutions use gcd or square root, so possibly √ in Julia typed \sqrt helps):

Even PHP is hard to beat and I like how they use ö from my Icelandic (or e.g. German) alphabet:

# PHP, 54 bytes

```julia-auto
<?for(;$i++<100;)echo[Fizz][$i%3].[Buzz][$i%5]?:$i,~õ;

```

Valid for v5.5 onwards. The `õ` is character 245, a bit inverted `\n` .

and also:

# Python 2, 56

```julia-auto
i=0;exec"print i%3/2*'Fizz'+i%5/4*'Buzz'or-~i;i+=1;"*100

```

Let alone Perl at 45 bytes, or (other) made for golfing languages:

# [MathGolf](https://github.com/maxbergmark/mathgolf), ~~24~~ 22 bytes

```julia-auto
♀{î╕Σ╠δ╕┌╠δ`+Γî35α÷ä§p

```

or those that cheat, here by language targeting this problem:

# [gs2](https://github.com/nooodl/gs2/), 1

```julia-auto
f

```

> **Update** : Added 27-byte [answer](https://codegolf.stackexchange.com/a/58664/2537) that doesn’t use `f`

My favorite newly discovered (after Piet, and Julia) language/solution is though:

# [Hexagony](https://github.com/m-ender/hexagony), ~~77~~ 76 bytes

```julia-auto
=?3})"F\>%'5?\"\B;u;"}/4;d'%<>\g/"!{{\}.%<@>'...<>,}/${;;z;i;z;;$/<>?{$/"./_

```

[Try it online!](https://tio.run/##y0itSEzPz6v8/9/W3rhWU8ktxk5V3dQ@RinGybrUWqlW38Q6RV3Vxi4mXV9Jsbo6plZP1cbBTl1PT8/GTqdWX6Xa2rrKOhOIrVX0bezsq1X0lfT04///BwA)

Same side length as [M L’s solution](https://codegolf.stackexchange.com/a/74906/76162), but a bit smaller.

answered Mar 24 '18 at 11:40

[  
  ![](https://global.discourse-cdn.com/julialang/original/3X/7/2/72c3f286d8e89ab695dee72b41929bfb03d7f418.png)  
 ](https://codegolf.stackexchange.com/users/76162/jo-king)

[Jo King](https://codegolf.stackexchange.com/users/76162/jo-king)

# [Hexagony](https://github.com/mbuettner/hexagony), 91 bytes

[..] Fizzbuzz in a size 6 hexagon:

```julia-auto
3}1"$.!$>)}g4_.{$'))\<$\.\.@\}F\$/;z;u;<%<_>_..$>B/<>}))'%<>{>;e"-</_%;\/{}/>.\;.z;i;..>(('

```

[..] So, here is a 110 seconds long GIF animation at 2 fps, showing the program flow during the first 6 numbers `1, 2, Fizz, 4, Buzz, Fizz` , the first 220 ticks of the program (click on the image for the full size):

[![enter image description here](https://global.discourse-cdn.com/julialang/original/3X/d/c/dc830313b3a9114ec6c405079890ed3103f13dcd.gif)](https://i.stack.imgur.com/Ciu3j.gif)

[..] Hexagony is not that hard to work with. But golfing it can be a pain in the butt. 😉

It was fun!

answered Mar 5 '16 at 21:56

[  
  ![](https://global.discourse-cdn.com/julialang/original/3X/9/b/9bcac5a14f17c09cca08a045fb9bb74f535efe10.jpeg)  
 ](https://codegolf.stackexchange.com/users/39407/m-l)

[M L](https://codegolf.stackexchange.com/users/39407/m-l)

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [November 18, 2019, 1:01pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/18 "2019-11-18T13:01:11Z")

</div>

I can read non-Julia solutions on stackoverflow…

---

<div class="post-metadata">

**Author:** ![non-Jedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-jedi/32/3645_2.png) [@non-Jedi](https://discourse.julialang.org/u/non-Jedi)\
**Post date:** [November 18, 2019, 2:10pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/19 "2019-11-18T14:10:44Z")

</div>

I got it down to 67 bytes with both the for-loop implementation and the iterator implementation, but I’m not seeing a way forward from here either:

```julia
for i=1:100;f=i%3<1;b=i%5<1;println(f|b ? "Fizz"^f*"Buzz"^b : i)end

```

```julia
(i->(f=i%3<1;b=i%5<1;println(f|b ? "Fizz"^f*"Buzz"^b : i))).(1:100)

```

Note that doing `f,b=i%3<1,i%5<1` is the same number of characters as `f=i%3<1;b=i%5<1`.

Lots of variations on the same theme that don’t actually shorten it at all:

```julia
g(i,f=i%3<1,b=i%5<1)=println(b|f ? "Fizz"^f*"Buzz"^b : i);g.(1:100)
(g(i,f=i%3<1,b=i%5<1)=println(b|f ? "Fizz"^f*"Buzz"^b : i)).(1:100)

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [November 18, 2019, 3:11pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/20 "2019-11-18T15:11:08Z")

</div>

66 is possible:

```julia
1:100 .|>i->(f=i%3<1;b=i%5<1;println(f|b ? "Fizz"^f*"Buzz"^b : i))

```

[Next page](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354.md?page=2)
