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

<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:** [March 1, 2023, 5:25pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/41 "2023-03-01T17:25:23Z")

</div>

Those two solutions are neat.

Searching around for tips for code golfing in Julia, on the Code Golf StackExchange came across discussion for this problem. From the discussion it appears there is a 60char/bye solution using `repr` and there is a 59char/byte solution using `"$x"`.

---

<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, 7:14pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/42 "2023-03-01T19:14:15Z")

</div>

another whacky one (65)

```julia
1:100 .|>x->println(max("$x",join(("Fizz","Buzz")[x.%[3,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:** [March 1, 2023, 8:23pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/43 "2023-03-01T20:23:56Z")

</div>

Well for what its worth I freed up one char/byte on

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

---

<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:** [March 1, 2023, 10:21pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/44 "2023-03-01T22:21:21Z")

</div>

Does it have to be short, or just weird:

```julia
t=1:100;t.|>println∘x->["$x","Fizz","Buzz","FizzBuzz"][Dict([1 3 5 0].*t.=>[1 2 3 4])[x%15]]

```

---

<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:** [March 1, 2023, 11:42pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/45 "2023-03-01T23:42:00Z")

</div>

Weird examples are certainly welcome. I’m sure they could get pretty weird, I really want to crack that 59 char/byte solution though.

This one is cool

---

<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:** [March 2, 2023, 12:29am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/46 "2023-03-02T00:29:39Z")

</div>

This one is both weird and breaks all barriers (but of dubious correctness):

```julia
t=1:100;@.println(t,"\r","Fizz"^(t%3<1)*"Buzz"^(t%5<1))

```

---

<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 2, 2023, 7:31am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/47 "2023-03-02T07:31:46Z")

</div>

This is very cool

Replacing `\r` with `\b` gives something visually almost correct

---

<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 2, 2023, 12:17pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/48 "2023-03-02T12:17:27Z")

</div>

another hint from the leaderboard is that the smallest solution is 59bytes/59chars, so no unicode trickeries.  
But there’s a shorter solution with 61bytes/57chars which may imply the use of `∘` (3bytes/1char)

---

<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:** [March 4, 2023, 9:53pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/49 "2023-03-04T21:53:08Z")

</div>

It has me pretty stumped, I’ve been working on it in-between writing stuff for my thesis.

---

<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 5, 2023, 10:34am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/50 "2023-03-05T10:34:37Z")

</div>

I feel you xD  
I have so many solutions 1byte from best on that website…it drives me crazy

---

<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:** [March 5, 2023, 3:26pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/51 "2023-03-05T15:26:36Z")

</div>

The most frustrating is when an idea seems potentially it then realize you’re at 61+ char/bytes haha.

---

<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 5, 2023, 5:50pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/52 "2023-03-05T17:50:31Z")

</div>

If the approach is different share it even though is not the shortest one. Someone else could find a way to make it work

---

<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:** [March 5, 2023, 5:55pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/53 "2023-03-05T17:55:42Z")

</div>

The primary different approach I have tried is to use something like  
`"$x FizzBuzz"[start condition:end condition]` or another variation because besides using `max` to compare strings `"$x"` and `"Fizz^(x%x<1)*"Buzz"^(x%5<1)` not sure how using `"$x"` would help–given that at least one of the 59char/byte solutions involves using that interpolation.

---

<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 5, 2023, 7:06pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/54 "2023-03-05T19:06:51Z")

</div>

the problem is that “$x” length will change, so a unique start condition is tricky…

on that line I have a 64

```julia
/ =gcd;1:100 .|>n->println(max("FizzBuzz"[7-2(3/n):3+5/n],"$n"))

```

---

<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:** [March 5, 2023, 8:06pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/55 "2023-03-05T20:06:35Z")

</div>

Yeah that’s why I abandoned that approach.

That is a neat solution

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [March 5, 2023, 10:40pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/56 "2023-03-05T22:40:23Z")

</div>

putting together various ideas that appeared in previous posts …

```julia
 1:100 .|>i->println("$i\r","fizz"^(i%3<1),"buzz"^(i%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:** [March 5, 2023, 11:21pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/57 "2023-03-05T23:21:04Z")

</div>

oddly this type of solution works correctly in the Julia REPL but if plugging in here [Fizz Buzz](https://code.golf/fizz-buzz#julia) it doesn’t.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [March 5, 2023, 11:30pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/58 "2023-03-05T23:30:54Z")

</div>

in fact, it seemed too within the reach of previous posts to be overlooked

---

<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 6, 2023, 8:39am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/59 "2023-03-06T08:39:19Z")

</div>

[this](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/56) is printing **both** digits and “fizz”/“buzz” instead of skipping either

also, code.golf checks the solution verbatim, thus `\r` != `\n`

it would have been nice to beat the record by 2 tho! 😃

---

<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:** [March 6, 2023, 5:29pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/60 "2023-03-06T17:29:54Z")

</div>

So I know there is a 60 char/byte solution that uses `repr` and I got a 61byte/char so maybe analyzing it can give a hint how they got the 60 byte/char.  
`x=1:100;@.println(max("Fizz"^(1>x%3)*"Buzz"^(1>x%5),repr(x)))`

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

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