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

<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, 6:19pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/61 "2023-03-06T18:19:55Z")

</div>

how do you know that such solution exists?

---

<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, 6:37pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/62 "2023-03-06T18:37:49Z")

</div>

> [@Max\_Beez](#):
>
> 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"`.

See this previous post I made

---

<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 7, 2023, 1:38am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/63 "2023-03-07T01:38:58Z")

</div>

Not less, more infact, 67 char/byte but one I like  
`1:100 .|>x->max(("Fizz","Buzz").^(x.%(3,5).<1)|>prod,"$x")|>println`

---

<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 7, 2023, 9:31am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/64 "2023-03-07T09:31:27Z")

</div>

Found a way to use `99`, 65/63

```julia
0:99 .|>i->println(max("$(i+1)","Fizz"^(i%3÷2)*"Buzz"^(i%5÷4)))

```

and 64/62

```julia
i=0:99;@.println(max(repr(i+1),"Fizz"^(i%3÷2)*"Buzz"^(i%5÷4)))

```

---

<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 8, 2023, 2:26am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/65 "2023-03-08T02:26:48Z")

</div>

Couple fun solutions  
73Byte/Char  
`\ =(x,y)->x%y<1;x=1:100;@.max("Fizz"^(x\3)*"Buzz"^(x\5),repr(x))|>println`  
72Byte/Char  
`\ =(x,y)->x%y<1;1:100 .|>x->max("Fizz"^(x\3)*"Buzz"^(x\5),"$x")|>println`

And got rid of `max` but not for a shorter solution  
68Byte/Char  
`1:100 .|>x->"Fizz"^(x%3<1)*"Buzz"^(1>x%5)*"$x"^(x%3>0<x%5)|>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:** [March 8, 2023, 4:14am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/67 "2023-03-08T04:14:13Z")

</div>

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

Improved this to 64Byte/Char  
`i<n=102>i+1<println(max("Fizz"^(1>i%3)*"Buzz"^(1>i%5),"$i"));1<0`

---

<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 8, 2023, 9:00am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/68 "2023-03-08T09:00:30Z")

</div>

can you explain step by step how this function works?

```julia
i<n=102>i+1<println(max("Fizz"^(1>i%3)*"Buzz"^(1>i%5),"$i"));1<0

```

Not for the `println(...)` part which is “simple”, but for the `i<n=102>i+1< ... ;1<0` part

---

<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 8, 2023, 9:30am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/69 "2023-03-08T09:30:55Z")

</div>

```julia
i<n=...

```

overloads `<` operator but it keeps its precedence. We could have written `f(i,n)=...`

Here is where the thing gets interesting

```julia
i<n=102>i+1<...

```

The first part, `102>i+1`, is a comparison, the second is a function call! Let’s rewrite it as

```julia
function f(i, n)
    if 102>i+1
        f(i+1, println(max("Fizz"^(1>i%3)*"Buzz"^(1>i%5),"$i")))
    end
end

```

as soon as `102>i+1 == false` the execution stops. In all the other cases, we call again our function with an increased `i` and we give the `println(...)` as the `n` argument. The `println` statement is ran at `f` call time, so it doesn’t really matter what type that argument is.

The magic happens thanks to the chaining properties of `<` and `>`. It’s perfectly legit julia code to compare the same integer with two boundaries as `10 < x > 3`, as this translates into `10 < x && x > 3`.

Finally

```julia
...;1<0

```

launches the recursive function. The first argument is important as it’s your index `I`, the second one is not that interesting but you need something so `0` it is.

hope it makes sense

---

<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 8, 2023, 9:56am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/70 "2023-03-08T09:56:42Z")

</div>

thank you very much.  
this was the part i was missing…

> [@alemelis](#):
>
> The magic happens thanks to the chaining properties of `<` and `>`. It’s perfectly legit julia code to compare the same integer with two boundaries as `10 < x > 3`, as this translates into `10 < x && x > 3`.
> 
> Finally
> 
> `...;1<0`

PS  
also need a “;” at the end to avoid having a “false” at the end of the output!?

---

<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 8, 2023, 11:28am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/71 "2023-03-08T11:28:31Z")

</div>

the `;` is used to indicate the end of the function. That’s simply to inline the solution rather than having

```julia
i<n=...
1<0

```

both solutions have the same number of bytes, but I find easier to keep everything on a single line.

If instead you are referring to a `;` after `1<0`, that is not needed as that’s only to avoid the REPL to print the function return argument. In code.golf they use the Julia interpreter and read from stdout. This behaviour can sometimes be abused (e.g. some solutions may stop execution with an error, but that doesn’t show in stdout 😛 )

---

<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 8, 2023, 11:47am UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/72 "2023-03-08T11:47:08Z")

</div>

> [@alemelis](#):
>
> If instead you are referring to a `;` after `1<0`, that is not needed as that’s only to avoid the REPL to print the function return argument. In code.golf they use the Julia interpreter and read from stdout.

this is exactly what I was referring to.  
Thanks again for your clear and precise explanations!

To complete, does “false” come from the moment in which `i=101` and `102>102` is the last calculated value and is it the one implicitly “returned” by `<(i,n)`?

PS  
What happens to the “nothing” returned by the `println(...)` function?

PPS  
does it make the idea better?

```julia
i<n::Nothing=102>i+1<println(max("Fizz"^(1>i%3)*"Buzz"^(1>i%5),"$i"));1<nothing

```

---

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

</div>

> To complete, does “false” come from the moment in which `i=101` and `102>102` is the last calculated value and is it the one implicitly “returned” by `<(i,n)` ?

yes correct, the comparison doesn’t hold and `false` is returned before the rest of the code is executed. I believe this is called `short-circuit`ing boolean operators

> What happens to the “nothing” returned by the `println(...)` function?

that `nothing` is never returned, as `false` is returned first

> does it make the idea better?

that is correct but it is preventing to use both `Int` and `Nothing` as second argument to `<`, which is the trick that makes the solution golfy

---

<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 8, 2023, 1:05pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/74 "2023-03-08T13:05:29Z")

</div>

Interestingly enough, can also end with `;1<2` and still will execute the recursion successfully.

In the REPL you get  
`ERROR: error in method definition: function Base.< must be explicitly imported to be extended`

---

<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 8, 2023, 1:08pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/75 "2023-03-08T13:08:49Z")

</div>

This recursive solution is 2B/C more so its 66B/C but I like it  
`n<f=max("Fizz"^(1>n%3)*"Buzz"^(1>n%5),"$n")|>println,101>n+1<f;1<0`

---

<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 8, 2023, 3:05pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/76 "2023-03-08T15:05:57Z")

</div>

like this one as it floads the REPL with nested tuples 😃

---

<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 8, 2023, 4:30pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/77 "2023-03-08T16:30:15Z")

</div>

Chaining seems to have potential for getting to the solution but for that to work it seems there must be another ‘trick’ to put in there with it.

---

<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 8, 2023, 4:46pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/78 "2023-03-08T16:46:47Z")

</div>

something along this line

> [@Code-golfing FizzBuzz](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/54):
>
> the problem is that “$x” length will change, so a unique start condition is tricky… on that line I have a 64 / =gcd;1:100 .|\>n-\>println(max("FizzBuzz"[7-2(3/n):3+5/n],"$n"))

but replacing `gcd` with some dark magic?

and finally making it recursive

---

<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 8, 2023, 4:49pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/79 "2023-03-08T16:49:31Z")

</div>

Yeah that might work,I switched to the Fib problem to see if I find any enlightenment with recursion to take to FizzBuzz,

Maybe this is of no use, but is there a way you have seen to nest the recursion possible using chaining? I suspect that is not of help though

---

<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 8, 2023, 4:53pm UTC](https://discourse.julialang.org/t/code-golfing-fizzbuzz/95354/80 "2023-03-08T16:53:55Z")

</div>

> is there a way you have seen to nest the recursion possible using chaining?

not sure I’m following

---

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

</div>

As in having an outside chaining and and inner chaining, in the recursive manner. I think if thats possible its not of use here so I guess never mind. More of a curiosity on its limit.

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

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