Live data from Hacker News

Euler's Fizzbuzz (2020)

philcrissman.net

21–30 of 92 posts

Re: Euler's Fizzbuzz (2020)

#21
post #16
post #13

Earlier quoted context omitted.

In this case though you could just use an 11-element array as a lookup table which would technically make this branchless.

true - but would require modifying the proposed solutions. Besides, it does not remove the branching issue of mod ... unless you use a lookup table for that too, but then you might as well have the lookup table for the whole FizzBuzz solution space.

heh, the branching is now in the implementation of the lookup table

Re: Euler's Fizzbuzz (2020)

#22
post #16
post #13

Earlier quoted context omitted.

In this case though you could just use an 11-element array as a lookup table which would technically make this branchless.

true - but would require modifying the proposed solutions. Besides, it does not remove the branching issue of mod ... unless you use a lookup table for that too, but then you might as well have the lookup table for the whole FizzBuzz solution space.

Yeah, but given that `mod` (if we’re talking native integers rather than some bigint type) is implemented as a single machine instruction, i’d count that as branchless for all reasonable intents and purposes even though the hardware has to internally do something equivalent to a loop to do division.

Re: Euler's Fizzbuzz (2020)

#23
post #15
post #11

Neat trick, the only issue I see is mis-selling the idea that no conditionals are used ... map on it's own (independently how it's implemented) is a conditional. Also, if you break down how mod can be implemented, it definitely requires conditionals. In terms of the computational overhead this is way worse than just going for mod 15 and then mapping every of the possible 15 results to Fizz, Buzz or FizzBuzz.

Why would map have conditions? //pseudocode map(f, ns) for i in (0 .. ns.length - 1) f(ns[i])

The map data structure, not the map higher order function. As implemented, most map data structures will have a conditional in their lookup functions to handle the case of collisions or a key being absent from the map.

Re: Euler's Fizzbuzz (2020)

#24
post #22
post #16

Earlier quoted context omitted.

true - but would require modifying the proposed solutions. Besides, it does not remove the branching issue of mod ... unless you use a lookup table for that too, but then you might as well have the lookup table for the whole FizzBuzz solution space.

Yeah, but given that `mod` (if we’re talking native integers rather than some bigint type) is implemented as a single machine instruction, i’d count that as branchless for all reasonable intents and purposes even though the hardware has to internally do something equivalent to a loop to do division.

on the CPU level (I'm talking Intel here, but it applies to most other chips) DIV/IDIV instructions (returning division result and remainder a.k.a. modulo) are the ones using up the most CPU cycles. The reason for this is that they can't be completely parallelised because of having to check conditions.

Re: Euler's Fizzbuzz (2020)

#25
post #21
post #16

Earlier quoted context omitted.

true - but would require modifying the proposed solutions. Besides, it does not remove the branching issue of mod ... unless you use a lookup table for that too, but then you might as well have the lookup table for the whole FizzBuzz solution space.

heh, the branching is now in the implementation of the lookup table

It's a common pattern in embedded systems (for better or worse) when you have something like function pointers or computed go tos available. C, of course, has function pointers and I've seen dispatch tables like this before:

  message_handler = {type_0, type_1, ..., type_999}; // imagine it being generated
  message_handler[message.type](message);
In the case of computed go to in Fortran, it's similar. I had the "pleasure" of maintaining this once. It's been a while so I had to look up the syntax, IIRC it used something like a dispatch tree and dispatched off each digit in the type but I could be wrong:

  go to (0, 100, 200, 300, ...), message_type / 100 -- integer division

  0
    go to ...
  100
    go to ...
  200
    go to ...
  ...

Re: Euler's Fizzbuzz (2020)

#27
post #2

This is a cute solution but I feel like the exposition makes it more mysterious seeming then necessary. If you have n%15, that's enough to do FizzBuzz on it's own, without having to take a fourth power. It's just then you need to map n%15=3,6,9,12 to "Fizz" instead of just 6. Taking the fourth power simplifies things because 3^4, 6^4, 9^4 and 12^4 all equal 6 (mod 15). And similarly for the multiples of 5. Though thi…

His point was writing a function without conditional logic, and then of course generalizing to any "fizzbuzz category" problem.

I thought it was a fantastic post on a extremely well trodden subject, up there with solving fizzbuzz in Tensorflow post. (https://joelgrus.com/2016/05/23/fizz-buzz-in-tensorflow/)

Re: Euler's Fizzbuzz (2020)

#29
post #2

This is a cute solution but I feel like the exposition makes it more mysterious seeming then necessary. If you have n%15, that's enough to do FizzBuzz on it's own, without having to take a fourth power. It's just then you need to map n%15=3,6,9,12 to "Fizz" instead of just 6. Taking the fourth power simplifies things because 3^4, 6^4, 9^4 and 12^4 all equal 6 (mod 15). And similarly for the multiples of 5. Though thi…

His point was writing a function without conditional logic, and then of course generalizing to any "fizzbuzz category" problem. I thought it was a fantastic post on a extremely well trodden subject, up there with solving fizzbuzz in Tensorflow post. ( https://joelgrus.com/2016/05/23/fizz-buzz-in-tensorflow/ )

My entry to use random-isn't-random: https://github.com/LanceH/fizzbuzz/blob/master/fb.go

I feel like I need to revisit after seeing tensorflow and Euler. I need to find some way of making the lucky number using things around a room, like a mentalist or something.

Some other subversive ones I've seen of are the java enterprise version, and several that import a fizzbuzz library and just run.

Re: Euler's Fizzbuzz (2020)

#30
post #28

I would do this: Let i = ((n % 3) == 0) | ((n % 5) == 0) Then map output as { n, 'Fizz', 'Buzz', 'FizzBuzz' }

Or, using only elementary math:

Let i = ((n % 3) ** 2) + ((n % 5) ** 4) * 2

Or, given any number n of primes p_j, the "fizzbuzz index" i is just

Let i = sum_over_j(((n % p_j) ** (p_j - 1)) * (n ** j))

(This doesn't generalize to non-primes via the totient function for the same reason the post's solution doesn't generalize - (a % k) * phi(k) for prime k is zero if and only if k divides a, but for non-prime k it can also become zero for other a.)

Post reply on HN