Live data from Hacker News

Euler's Fizzbuzz (2020)

philcrissman.net

31–40 of 92 posts

Re: Euler's Fizzbuzz (2020)

#31
Maybe I'm being old and grumpy, but is there really a point to writing python without conditional logic? Each operation is going to have all sorts of stuff going in the interpreter, and the amount of code being executed that tiny snippet is way more than one would expect.

Re: Euler's Fizzbuzz (2020)

#32
post #31

Maybe I'm being old and grumpy, but is there really a point to writing python without conditional logic? Each operation is going to have all sorts of stuff going in the interpreter, and the amount of code being executed that tiny snippet is way more than one would expect.

I think it's about the math porn, not execution time.

Re: Euler's Fizzbuzz (2020)

#34
That was fun!

Not that anyone cares for FizzBuzz but I'll just note that n**4%15 is more efficiently written using the 3 argument pow function in python, eg pow(n, 4, 15).

    >>> timeit("(1>> timeit("pow(1>> 
If n gets large then n**4 is very large so the % 15 has to deal with a big number. pow runs the modulo operation at the same time as the power operation so the intermediates never get bigger than 15.

https://docs.python.org/3/library/functions.html#pow

Re: Euler's Fizzbuzz (2020)

#35
post #3

This is of course a really neat solution, but the proof doesn't really give me much value as a reader. I am much more interested in an explanation of how to find this solution, than a theoretical solution of why it is correct. Specifically I don't understand from the article why the trick of raising n to the power of LCM(phi(3), phi(5)) works.

> This is of course a really neat solution, but the proof doesn't really give me much value as a reader.

This is because your number theory-fu is poor.

Don't take this in a bad way, I'm probably even less capable. What I mean is that readability is predicated on a reader. Seasoned number theorists might not be as cool with goroutines or walrii operators or virtual DOMs.

Re: Euler's Fizzbuzz (2020)

#36
post #31

Maybe I'm being old and grumpy, but is there really a point to writing python without conditional logic? Each operation is going to have all sorts of stuff going in the interpreter, and the amount of code being executed that tiny snippet is way more than one would expect.

Conditional logic is, in general, harder to analyze for correctness than declarative data and mathematical formulae.

Re: Euler's Fizzbuzz (2020)

#38

Good luck running ^4 on larger numbers and overflowing integer / long bounds orders of magnitude faster than the plain "boring" solutions

Modular exponentiation can be done very quickly, you don't need to compute n^4 at all. Not sure if this particular implementation makes use of that, though. In Haskell, I can compute (100000 ^ 1000000 `mod` 15) near instantly.

Re: Euler's Fizzbuzz (2020)

#39

Good luck running ^4 on larger numbers and overflowing integer / long bounds orders of magnitude faster than the plain "boring" solutions

The trick is to do the modulus before the exponentiation. It gives the same result.

  n^4 % x = m
  == (n % x)^4 % x = m
By way of demonstration:

  n = 18, x = 15
  18^4 = 104976 = 6 (mod 15)
  ----
  18 % 15 = 3
  3^4 = 81 = 6 (mod 15)
A very handy result to remember for cases where you don't want to use or don't have easy access to arbitrary precision integers.

Re: Euler's Fizzbuzz (2020)

#40

Was this the expected answer to the coding interview question at places like Renaissance Technologies, Jane Street Capital, and Galois? This is really funny. I had an intuition there must be a lambda function solution for fizzbuzz, but I don't do coding interviews and never pursued it. I can see why now, because it's waaay out of my skillset, but so neat to read.

Jane Street maybe. I've not heard of Galois having a tricky interview process. From what I've heard of Renaissance, this would be insultingly trivial to ask.
Post reply on HN