Comments

On Fizz Buzz with Cosines
Post Comment

Isogash wrote:

Gonna memorize this for my next technical interview so I can say "and then we apply the FizzBuzz formula".

21 Nov 2025 10:53 UTC | #1 of 10 comments

Cosmic wrote:

This is grotesque. Love it! <3

21 Nov 2025 11:58 UTC | #2 of 10 comments

Tri wrote:

Great! Can it be extended to print 'Jazz' if the number is divisible by 7?

21 Nov 2025 12:11 UTC | #3 of 10 comments

Susam Pal wrote:

Yes, it is quite straightforward to extend the solution to include numbers divisible by 7. The new indicator function can be defined like this: \[ I_7(n) = \frac{1}{7} \sum_{k = 0}^6 e^{2 \pi i k n/7} = \frac{1}{7} + \frac{2}{7} \cos \left( \frac{2 \pi n}{7} \right) + \frac{2}{7} \cos \left( \frac{4 \pi n}{7} \right) + \frac{2}{7} \cos \left( \frac{6 \pi n}{7} \right). \] The new Fourier series is going to be: \[ g(n) = I_3(n) + 2 \, I_5(n) + 4 \, I_7(n) = f(n) + 4 \, I_7(n). \] So we only need to add the new term \( 4 \, I_7(n) \) to \( f(n), \) which gives the new Fourier series \begin{align*} g(n) = \frac{137}{105} &+ \frac{2}{3} \cos \left( \frac{2 \pi n}{3} \right) + \frac{4}{5} \cos \left( \frac{2 \pi n}{5} \right) + \frac{4}{5} \cos \left( \frac{4 \pi n}{5} \right) \\ &+ \frac{8}{7} \cos \left( \frac{2 \pi n}{7} \right) + \frac{8}{7} \cos \left( \frac{4 \pi n}{7} \right) + \frac{8}{7} \cos \left( \frac{6 \pi n}{7} \right). \end{align*} The indicators-based solution simply becomes:

from math import cos, pi
for n in range(1, 1001):
    s = [n, 'Fizz', 'Buzz', 'FizzBuzz', 'Jazz', 'FizzJazz', 'BuzzJazz', 'FizzBuzzJazz']
    i = (n % 3 == 0) + 2 * (n % 5 == 0) + 4 * (n % 7 == 0)
    print(s[i])

The cosine-based solution becomes:

from math import cos, pi
for n in range(1, 1001):
    s = [n, 'Fizz', 'Buzz', 'FizzBuzz', 'Jazz', 'FizzJazz', 'BuzzJazz', 'FizzBuzzJazz']
    i = round((137 / 105) + (2 / 3) * cos(2 * pi * n / 3)
                          + (4 / 5) * cos(2 * pi * n / 5)
                          + (4 / 5) * cos(4 * pi * n / 5)
                          + (8 / 7) * cos(2 * pi * n / 7)
                          + (8 / 7) * cos(4 * pi * n / 7)
                          + (8 / 7) * cos(6 * pi * n / 7))
    print(s[i])

The value of \( g(n) \) in the formula above (or equivalently the value of the variable i in the code) can be viewed as a flag in which each bit corresponds to whether \( \mathtt{Fizz}, \) \( \mathtt{Buzz} \) or \( \mathtt{Jazz} \) appears in the \( n \)th term of the sequence. All other details follow from this idea.

21 Nov 2025 12:28 UTC | #4 of 10 comments

Omepiet wrote:

This is morally degenerate. Also, it works.

21 Nov 2025 12:34 UTC | #5 of 10 comments

ByTheNumbers10 wrote:

The things you have to do with low-level languages like math and VHDL.

21 Nov 2025 13:40 UTC | #6 of 10 comments

Jose wrote:

This was a beautiful reading. Thanks.

21 Nov 2025 16:54 UTC | #7 of 10 comments

Wayne wrote:

Thanks, I enjoyed that!

22 Nov 2025 11:06 UTC | #8 of 10 comments

Jwosty wrote:

This is the exactly kind of thing that should win an obfuscation contest. I hate it but also I love it.

22 Nov 2025 20:52 UTC | #9 of 10 comments

Rowan wrote:

If we don't do the simplification to make the function real valued based on \( n \) being an integer but instead focus on the complex-valued functions through to the final function (which coincides for integers), and plot the path in the complex plane (Argand diagram), we get something pretty cool and spirally that satisfied my need for more "intuition" on why this works.

Even better than the static complex plane path: an animated web page with \( 6 \) arms rotating at different rates drawing out the path. Really explains why it's periodic. It hits \( 3 \) exactly when all the arms are in alignment forming a straight line.

30 Nov 2025 02:55 UTC | #10 of 10 comments
Post Comment