Solving Fizz Buzz with Cosines

70 points by susam


slot

All this beautiful maths only to have to stick a round in there at the end -- floats really do suck :)

Here's an alternative solution, using a slightly different notion of primitive function (= those allowed in a closed form), because we are working with a computer, after all!

for n in range(1, 101):
    print([n, "Fizz", "Buzz", "FizzBuzz"][2*(0x10842108421084210842108421>>n&1) + (0x249249249249249249249249249>>n&1)])

With slightly less obscure notation, this is

for n in range(1, 101):
    print([n, "Fizz", "Buzz", "FizzBuzz"][2*(int("00001"*32,2)>>n&1) + (int("001"*36,2)>>n&1)])
smlckz

This reminds me of the "formula" for the nth prime number.

Armavica

Wonderful :)

squarism

What would your assessment and/or feedback be to the hiring manager if you got this solution during an interview? :)