Live data from Hacker News

Solving Fizz Buzz with Cosines

susam.net

41–50 of 69 posts

Re: Solving Fizz Buzz with Cosines

#41

Earlier quoted context omitted.

> Sort of fun to muse whether almost all FizzBuzz implementations are a bit wrong. They're only wrong if they provide output that isn't in the spec. Adding "bazz" isn't in the spec, and assuming that something indeterminate MIGHT come later is also not part.

Yep, that's how people answer. Folks really really don't like thinking that "FizzBuzz" case maybe shouldn't be there, future extension or factor edit or no. // And as long as we're just manually computing factor times factor and typing out the results for it like "FizzBuzz" we might as well just hardcode the whole series...

I think the reqirement should be to n digits. Then at least we can benchmark it.

Re: Solving Fizz Buzz with Cosines

#43
HN is a great place to learn non-trivial things about trivial things, and that’s why I like it. My comment won’t add much to the discussion, but I just wanted to say that I learned something new today about a trivial topic I thought I already understood. Thank you, HN, for the great discussion thread.

Re: Solving Fizz Buzz with Cosines

#44
post #34

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

This can be translated to the discrete domain pretty easily, just like the NTT. Pick a sufficiently large prime with order 15k, say, p = 2^61-1. 37 generates the whole multiplicative group, and 37^((2^61-2)/3) and 37^((2^61-2)/5) are appropriate roots of unity. Putting it all together yields f(n) = 5226577487551039623 + 1537228672809129301*(1669582390241348315^n + 636260618972345635^n) + 3689348814741910322*(72555445…

Integer exponentiation is still really, really expensive. 3-4 modulus operations and a few branches is a lot cheaper.

Re: Solving Fizz Buzz with Cosines

#45
So, there’s a similar way to do it with a function that produces one of the characters in “FizBu\nx” and a while true loop that

- increases i on every \n,

- prints i when that produces x, otherwise prints the character

(Disregarding rounding errors)

That would be fairly obfuscated, I think.

Re: Solving Fizz Buzz with Cosines

#47
Inspired by this post & TF comment I tried symbollic regression [0] Basically it uses genetic algorithm to find a formula that matches known input and output vectors with minimal loss I tried to force it to use pi constant but was unable I don't have much expreience with this library but I'm sure with more tweaks you'll get the right result

  from pysr import PySRRegressor

  def f(n):
      if n % 15 == 0:
          return 3
      elif n%5 == 0:
          return 2
      elif n%3 == 0:
          return 1
      return 0

  n = 500
  X = np.array(range(1,n)).reshape(-1,1)
  Y = np.array([f(n) for n in range(1,n)]).reshape(-1,1)
  model = PySRRegressor(
          maxsize=25,
          niterations=200,  # 
)

  model.fit(X,Y)
Result I got is this:

((cos((x0 + x0) * 1.0471969) * 0.66784626) + ((cos(sin(x0 * 0.628323) * -4.0887628) + 0.06374673) * 1.1508249)) + 1.1086457

with compleixty 22 loss: 0.000015800686 The first term is close to 2/3 * cos(2pi*n/3) which is featured in the actual formula in the article. the constant doesn't compare to 11/15 though

[0] https://github.com/MilesCranmer/PySR

Re: Solving Fizz Buzz with Cosines

#48

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…

"Unavailable Due to the UK Online Safety Act"

Does aphyr even have comments, or is it a pure political protest?

That's the first thing that's tempted to break out an ssh tunnel - I can live without the occasional NSFW reddit group.

Re: Solving Fizz Buzz with Cosines

#49

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.

> LMAOOOOO OKAY SO THE POLYNOMIAL IS LITERALLY SHITTING ITSELF

That was a fun read, but I can see that persona quickly becoming wearing. I had a "talk like a wiki article" persona for a while that worked better (for me) than any attempt to inject personality. The greyer the better, when it comes to tools.

(Being a child of the internet rather than the classroom my abusive solution would be to look up the sequence in OEIS, but I think fizzbuzz could be encoded into an L-system quite neatly).

Re: Solving Fizz Buzz with Cosines

#50
post #48

Earlier quoted context omitted.

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…

"Unavailable Due to the UK Online Safety Act" Does aphyr even have comments, or is it a pure political protest? That's the first thing that's tempted to break out an ssh tunnel - I can live without the occasional NSFW reddit group.

They do show comments at the bottom of the posts.
Post reply on HN