Live data from Hacker News

Solving Fizz Buzz with Cosines

susam.net

21–30 of 69 posts

Re: Solving Fizz Buzz with Cosines

#21
post #13

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.

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

Re: Solving Fizz Buzz with Cosines

#22
post #17

Earlier quoted context omitted.

This would be an offer on the spot from me

A massively over-engineered, incorrect solution?

A candidate that appreciates the value of the question, yet won't subject themselves to the absurdity of demonstrating compliance.

Yes, very much yes.

Re: Solving Fizz Buzz with Cosines

#23

https://joelgrus.com/2016/05/23/fizz-buzz-in-tensorflow/

There was another great satirical take on FizzBuzz which had something to do with runes and incantation and magical spells...? I sort of remember that the same author maybe even wrote a follow up? to this extremely experienced developer solving FizzBuzz in the most arcane way possible.

Does this ring a bell for anyone?

---

Found it!

https://aphyr.com/posts/340-reversing-the-technical-intervie...

https://aphyr.com/posts/341-hexing-the-technical-interview

https://aphyr.com/posts/342-typing-the-technical-interview

https://aphyr.com/posts/353-rewriting-the-technical-intervie... (the FizzBuzz one)

https://aphyr.com/posts/354-unifying-the-technical-interview

wow.

Re: Solving Fizz Buzz with Cosines

#24

https://joelgrus.com/2016/05/23/fizz-buzz-in-tensorflow/

There was another great satirical take on FizzBuzz which had something to do with runes and incantation and magical spells...? I sort of remember that the same author maybe even wrote a follow up? to this extremely experienced developer solving FizzBuzz in the most arcane way possible. Does this ring a bell for anyone? --- Found it! https://aphyr.com/posts/340-reversing-the-technical-intervie... https://aphyr.com/pos…

One of my favorite blog posts of all time: https://aphyr.com/posts/342-typing-the-technical-interview

Re: Solving Fizz Buzz with Cosines

#26

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

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.

Re: Solving Fizz Buzz with Cosines

#28
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.)

Re: Solving Fizz Buzz with Cosines

#29
post #13

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.

[deleted]

Re: Solving Fizz Buzz with Cosines

#30
The article conceit is fantastic. That said, is the going-in algo wrong?

I see a case for 3 * 5 in here:

  for n in range(1, 101):
      if n % 15 == 0:
          print('FizzBuzz')
      elif n % 3 == 0:
          print('Fizz')
      elif n % 5 == 0:
          print('Buzz')
      else:
          print(n)
Why?

If we add 'Bazz' for mod 7, are we going to hardcode:

  for n in range(1, 105):
      if n % 105 == 0:          # 3 * 5 * 7
          print('FizzBuzzBazz')
      elif n % 15 == 0:         # 3 * 5
          print('FizzBuzz')
      elif n % 21 == 0:         # 3 * 7
          print('FizzBazz')
      elif n % 35 == 0:         # 5 * 7
          print('BuzzBazz')
      elif n % 3 == 0:
          print('Fizz')
      elif n % 5 == 0:
          print('Buzz')
      elif n % 7 == 0:
          print('Bazz')
      else:
          print(n)
Or should we have done something like:

  for n in range(1, 105):
      out = ''
  
      if n % 3 == 0:
          out += 'Fizz'
      if n % 5 == 0:
          out += 'Buzz'
      if n % 7 == 0:
          out += 'Bazz'
  
      print(out or n)
I've been told sure, but that's a premature optimization, 3 factors wasn't in the spec. OK, but if we changed our minds on even one of the two factors, we're having to find and change 2 lines of code ... still seems off.

Sort of fun to muse whether almost all FizzBuzz implementations are a bit wrong.

Post reply on HN