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.
Euler's Fizzbuzz (2020)
21–30 of 92 posts
Re: Euler's Fizzbuzz (2020)
#22Earlier 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.
Re: Euler's Fizzbuzz (2020)
#23Neat 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])
Re: Euler's Fizzbuzz (2020)
#24Earlier 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.
Re: Euler's Fizzbuzz (2020)
#25Earlier 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
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)
#26Re: Euler's Fizzbuzz (2020)
#27This 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…
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)
#28Let i = ((n % 3) == 0) | ((n % 5) == 0) Then map output as { n, 'Fizz', 'Buzz', 'FizzBuzz' }
Re: Euler's Fizzbuzz (2020)
#29This 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/ )
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)
#30I would do this: Let i = ((n % 3) == 0) | ((n % 5) == 0) Then map output as { n, 'Fizz', 'Buzz', 'FizzBuzz' }
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.)