Solving Fizz Buzz with Cosines

(susam.net)

106 points | by hprotagonist 7 hours ago ago

27 comments

  • nine_k 4 hours ago

    Well, there must be an obvious solution where the fizzbuzz sequence is seen as a spectrum of two frequencies (1/3 and 1/5), and a Fourier transform gives us a periodic signal with peaks of one amplitude at fizz spots, another amplitude at buzz spots, and their sum at fizzbuzz spots. I mean. that would be approximately the same solution as the article offers, just through a more straightforward mechanism.

    • susam 3 hours ago

      That is precisely how I began writing this post. I thought I'd demonstrate how to apply the discrete Fourier transform (DFT) but to do so for each of the 15 coefficients turned out to be a lot of tedious work. That's when I began noticing shortcuts for calculating each coefficient c_k based on the divisibility properties of k. One shortcut led to another and this post is the end result. It turns out it was far less tedious (and more interesting as well) to use the shortcuts than to perform a full-blown DFT calculation for each coefficient.

      Of course, we could calculate the DFT using a tool, and from there work out the coefficients for the cosine terms. For example, we could get the coefficients for the exponential form like this:

      https://www.wolframalpha.com/input?i=Fourier%5B%7B3%2C+0%2C+...

      And then convert them to the coefficients for the cosine form like this:

      https://www.wolframalpha.com/input?i=%7B11%2F15%2C+2*0%2C+2*...

      That's certainly one way to avoid the tedious work but I decided to use the shortcuts as the basis for my post because I found this approach more interesting. The straightforward DFT method is perfectly valid as well and it would make an interesting post by itself.

    • luc4 32 minutes ago

      Could you elaborate on why the Fourier transform of cos(x/3) + cos(x/5) (I assume that's what you mean by "spectrum of two frequencies") yields a periodic signal? It seems to me that one would get exactly 4 peaks, at +-1/3 and +-1/5 respectively.

    • mr_wiglaf 2 hours ago

      Ah so taking the Fourier transform of this function[0]? The summation of the fizz and buzz frequencies don't lead to perfect peaks for the fizz and buzz locations. I need to revisit Fourier cause I would have thought the transform would have just recovered the two fizz and buzz peaks not the fizzbuzz spot.

      [0]: https://www.desmos.com/calculator/wgr3zvhazp

    • atemerev 4 hours ago

      Yes. Exactly. This is how it _should_ have been done.

      Also probably easy enough to encode as quantum superpositions.

      • HPsquared 3 hours ago

        How would someone do FizzBuzz on a quantum computer? It seems like a nice toy example problem.

  • thomasjudge 6 hours ago
  • ok123456 3 hours ago

    I once had a coworker who used the FFT to determine whether coordinates formed a regular 2D grid. It didn't really work because of the interior points.

  • isoprophlex 4 hours ago

    What a neat trick. I'm thinking you can abuse polynomials similarly. If the goal is to print the first, say, 100 elements, a 99-degree polynomial would do just fine :^)

    EDIT: the llm gods do recreational mathematics as well. claude actually thinks it was able to come up with and verify a solution...

    https://claude.ai/share/5664fb69-78cf-4723-94c9-7a381f947633

    • jiggawatts an hour ago

      That's the most expletive-laden LLM output I've ever seen. ChatGPT would have aborted half way through to protect its pure and unsullied silicon mind from the filthy impure thoughts.

  • layer8 4 hours ago

    I think that implementation will break down around 2^50 or so.

  • siegelzero 5 hours ago

    Very cool! There's definitely some similarity to Ramanujan Sums, though the approach here sort of packages the fizz-buzz divisibility properties into one function. https://en.wikipedia.org/wiki/Ramanujan%27s_sum

  • burnt-resistor 34 minutes ago

    While it's cute use of mathematics, it's extremely inefficient in the real world because it introduces floating point multiplications and cos() which are very expensive. The only thing it lacks is branching which reduces the chances of a pipeline stall due to branch prediction miss.

    (The divisions will get optimized away.)

  • jmclnx an hour ago

    I wonder where this is coming from. I saw on USENET in comp.os.linux.misc a conversation about fizzbuzz too. That was on Nov 12.

    Anyway an interesting read.

  • throwaway81523 3 hours ago
  • tantalor 5 hours ago

    There are several mentions of "closed-form expression" without precisely defining what that means, only "finite combinations of basic operations".

    TFA implies that branches (if statements and piecewise statements) are not allowed, but I don't see why not. Seems like a basic operation to me.

    Nevermind that `s[i]` is essentially a piecewise statement.

    • susam 5 hours ago

      > There are several mentions of "closed-form expression" without precisely defining what that means, only "finite combinations of basic operations".

      There is no universal definition of 'closed-form expression'. But there are some basic operations and functions that are broadly accepted, and they are spelled out directly after the 'finite combinations' phrase you quoted from the post. Quoting the remainder of that sentence here:

      '[...] finite combinations of basic operations such as addition, subtraction, multiplication, division, integer exponents and roots with integer index as well as functions such as exponentials, logarithms and trigonometric functions.'

  • ivan_ah 6 hours ago

    This is very nice.