# 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:** 2

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [November 18, 2019, 9:43pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/21 "2019-11-18T21:43:58Z")

</div>

I think you need the trailing semi-colon in professional FizzBuzz competitions.

---

<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 19, 2019, 1:22am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/22 "2019-11-19T01:22:17Z")

</div>

got one down to 78 bytes…

```plaintext
println.(1:100 .|>x->(x.%[3,5].<1)|>g->any(g) ? join(["Fizz","Buzz"][g]) : x)

```

Think this is where the rubber meets the road for me!

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [November 19, 2019, 1:39am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/23 "2019-11-19T01:39:03Z")

</div>

I don’t know about you folks, but I’m sticking to 249 bytes:

```julia
function fizzbuzz()
    for i ∈ 1:100
        if i%15==0
            println("fizzbuzz")
        elseif i%3==0
            println("fizz")
        elseif i%5==0
            println("buzz")
        else
            println(i)
        end
    end
end

```

---

<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 19, 2019, 2:20am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/24 "2019-11-19T02:20:46Z")

</div>

Seeing that right now… Is like being handed a glass of water after being left for dead in the desert. I spent a lot of my freetime code golfing. Don’t regret it, but - don’t think I’ll be going back anytime soon.

---

<div class="post-metadata">

**Author:** ![alemelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alemelis/32/208433_2.png) [@alemelis](https://discourse.julialang.org/u/alemelis)\
**Post date:** [March 9, 2022, 4:08pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/25 "2022-03-09T16:08:56Z")

</div>

Apparently, 59 is possible!

> **[Rankings: Holes](https://code.golf/rankings/holes/fizz-buzz/julia/bytes)**
>
> Fizz Buzz in Julia in bytes.

---

<div class="post-metadata">

**Author:** ![Max\_Beez](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Beez](https://discourse.julialang.org/u/Max_Beez)\
**Post date:** [February 18, 2023, 6:52pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/26 "2023-02-18T18:52:09Z")

</div>

`(x->(t="Fizz"^(x%3<1)*"Buzz"^(x%5<1);t>"" ? t : x)).(1:100)` Runs in the REPL

---

<div class="post-metadata">

**Author:** ![Max\_Beez](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Beez](https://discourse.julialang.org/u/Max_Beez)\
**Post date:** [February 24, 2023, 8:19pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/27 "2023-02-24T20:19:22Z")

</div>

I’ve been trying to crack this solution but I don’t know how to work in a print statement. 59 in the REPL took some work but doable.

---

<div class="post-metadata">

**Author:** ![Max\_Beez](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Beez](https://discourse.julialang.org/u/Max_Beez)\
**Post date:** [February 25, 2023, 4:19am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/28 "2023-02-25T04:19:25Z")

</div>

Discovered another 59 char/59 byte REPL solution  
`(x->(s=x%3<1;t=x%5<1;(x,"Fizz"^s*"Buzz"^t)[s|t+1])).(1:100)`

---

<div class="post-metadata">

**Author:** ![alemelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alemelis/32/208433_2.png) [@alemelis](https://discourse.julialang.org/u/alemelis)\
**Post date:** [February 27, 2023, 10:58am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/29 "2023-02-27T10:58:23Z")

</div>

> [@Max\_Beez](#):
>
> (x-\>(s=x%3\<1;t=x%5\<1;(x,“Fizz”^s\*“Buzz”^t)[s|t+1])).(1:100)

best I can do with this idea + print is 65

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

```

but my personal best is 64 ([writeup here](https://alemelis.gitlab.io/b/fbg))

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

```

the \<64 solutions may use a completely different idea(s)…something with neither ternary operator or array indexing 🤷

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [February 27, 2023, 2:23pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/30 "2023-02-27T14:23:10Z")

</div>

This may count as a 62 length solution:

```julia
(x->max("$x","Fizz"^(x%3<1)*"Buzz"^(x%5<1))).(1:100).|>println

```

and 52 on the REPL without `.|>println`

---

<div class="post-metadata">

**Author:** ![Max\_Beez](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Beez](https://discourse.julialang.org/u/Max_Beez)\
**Post date:** [February 27, 2023, 3:23pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/31 "2023-02-27T15:23:43Z")

</div>

Can improve to 60 length  
`1:100 .|>x->max("$x","Fizz"^(x%3<1)*"Buzz"^(x%5<1))|>println` or  
`1:100 .|>x->println(max("$x","Fizz"^(x%3<1)*"Buzz"^(x%5<1)))`

Or 51 in the REPL

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [February 27, 2023, 6:15pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/32 "2023-02-27T18:15:38Z")

</div>

And yet another improvement:

```julia
1:100 .|>println∘x->max("$x","Fizz"^(x%3<1)*"Buzz"^(x%5<1))

```

but it is an improvement in characters (byte-wise there is some unicode extra byte for `∘`.

---

<div class="post-metadata">

**Author:** ![Max\_Beez](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Beez](https://discourse.julialang.org/u/Max_Beez)\
**Post date:** [February 27, 2023, 6:36pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/33 "2023-02-27T18:36:20Z")

</div>

clever, I had tried \circ with println and max but then required another pair (,)

---

<div class="post-metadata">

**Author:** ![alemelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alemelis/32/208433_2.png) [@alemelis](https://discourse.julialang.org/u/alemelis)\
**Post date:** [February 27, 2023, 9:26pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/34 "2023-02-27T21:26:12Z")

</div>

Getting closer!  
60 bytes/chars

```julia
1:100 .|>x->println(max("$x","Fizz"^(x%3<1)*"Buzz"^(x%5<1)))

```

Edit: sorry haven’t seen the post above with this same solution

---

<div class="post-metadata">

**Author:** ![alemelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alemelis/32/208433_2.png) [@alemelis](https://discourse.julialang.org/u/alemelis)\
**Post date:** [February 27, 2023, 9:31pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/35 "2023-02-27T21:31:29Z")

</div>

Is there some hidden Unicode character to replace the `max` function call?

---

<div class="post-metadata">

**Author:** ![Max\_Beez](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Beez](https://discourse.julialang.org/u/Max_Beez)\
**Post date:** [February 27, 2023, 9:44pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/36 "2023-02-27T21:44:04Z")

</div>

Not that I am able to find, interestingly learning a lot about Julia trying to solve this though so its not all for naught

---

<div class="post-metadata">

**Author:** ![alemelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alemelis/32/208433_2.png) [@alemelis](https://discourse.julialang.org/u/alemelis)\
**Post date:** [February 28, 2023, 10:13am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/37 "2023-02-28T10:13:13Z")

</div>

this could be the one if only we could find a way of broadcasting `"$x"` without `string`

for now is 63 bytes/chars

```julia
N=1:100;@.println(max(string(N),"Fizz"^(N%3<1)*"Buzz"^(N%5<1)))

```

---

<div class="post-metadata">

**Author:** ![Max\_Beez](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Beez](https://discourse.julialang.org/u/Max_Beez)\
**Post date:** [February 28, 2023, 2:16pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/38 "2023-02-28T14:16:14Z")

</div>

Can shorten this one down to 61  
`x=1:100;@.println(max(repr(x),"Fizz"^(x%3<1)*"Buzz"^(x%5<1)))`

---

<div class="post-metadata">

**Author:** ![Max\_Beez](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Beez](https://discourse.julialang.org/u/Max_Beez)\
**Post date:** [February 28, 2023, 4:50pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/39 "2023-02-28T16:50:52Z")

</div>

Some remarks/observations

My suspicion is `"$x"` is used.

Defining the exponents for the strings outside the primary call saves parentheses but then adds characters to insert: i.e. `f=x%3<1` is the same length as `(x%3<1)` but then need to use`;`.

Using the ternary operator `a ? b : c` certainly takes up too much space

Trying to put it into one string doesn’t work because need to concatenate `$x` with `FizzBuzz` and there is no efficient way to then call exactly which indices are wanted, would have been nice given strings behave as arrays

Would be nice if `show()` works but it much like `print` displays on a single line. `dump()` was an option I tried, it prints on new lines but also includes type of the item being printed.

I have found no alternative to using `max()`

I had wondered if there was a clever way to use `0:99` but can’t seem to find a workaround for this.

---

<div class="post-metadata">

**Author:** ![alemelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alemelis/32/208433_2.png) [@alemelis](https://discourse.julialang.org/u/alemelis)\
**Post date:** [March 1, 2023, 9:13am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/40 "2023-03-01T09:13:14Z")

</div>

some other observations:

- `$x` needs `max` and we probably already have the [best solution with that idea](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/34)
- `0:99` is appealing but I don’t see a way of using it neither. This could be a false friend though, as we’re looking to remove just one byte/char and that blank space after `100` is the obvious target
- broadcasting over `N=1:100` usually yields the best solution, but in this case [we’re stuck at 61](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/38)
- another sometimes winning approach is to write a recursive solution, but I cannot do better than 68

```julia
i<n=102>i+1<println((f=1>i%3)|(b=1>i%5) ? "Fizz"^f*"Buzz"^b : i);1<0

```

- we can use `gcd` for a single check instead of two (66)

```julia
f="Fizz";b="Buzz";1:100 .|>i->println([i i f f*b b][gcd(i,15)%11])

```

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

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