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.…
Modulus and power commute, so you can use ((n%15)**4)%15 to keep it small for any integer n
Euler's Fizzbuzz (2020)
61–70 of 92 posts
Re: Euler's Fizzbuzz (2020)
#62This 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.
Re: Euler's Fizzbuzz (2020)
#63Good luck running ^4 on larger numbers and overflowing integer / long bounds orders of magnitude faster than the plain "boring" solutions
EDIT: Pasting into Lynx screwed formatting from Groff.
Re: Euler's Fizzbuzz (2020)
#64Good 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)
#65Good luck running ^4 on larger numbers and overflowing integer / long bounds orders of magnitude faster than the plain "boring" solutions
https://medium.com/@c0D3M/introduction-to-rsa-e8cb39af508e EDIT: Pasting into Lynx screwed formatting from Groff.
That is not generally true. A quick counterexample:
a = 2, n = 3, b = 5
2^3 % 5 = 8 % 5 = 3
2 % 5 = 2
3 != 2Re: Euler's Fizzbuzz (2020)
#66That 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.…
Re: Euler's Fizzbuzz (2020)
#67The fundamental mathematical concepts revealed in this answer, especially the use of Euler's totient theorem, are worth pondering because it is this branch of modular arithmetic that Diffie and Hellman first proposed, and then Rivest, Shamir, and Adleman concretely used to produce, well, RSA: https://en.wikipedia.org/wiki/RSA_(cryptosystem)#Operation Sadly, quantum supremacy may mean this form of cryptography will so…
Re: Euler's Fizzbuzz (2020)
#68Earlier quoted context omitted.
Well, the loop in your `map()` does obviously have a halting condition. But I believe by map, the GP meant the associative array used by the implementations to lookup the correct output.
In most languages, looping is implemented with conditional jumps, but I actually think Python is unique in this case because of the way it uses exceptions for flow control. Rather than checking an index to see if the loop has reached the end of an iterator, the iterator just raises a StopIteration exception and the runtime catches it. The key lookup in dict definitely has conditionals, though. Of course, the guy coul…
And how does the iterator know that it should raise an exception? A conditional.
Re: Euler's Fizzbuzz (2020)
#69Earlier quoted context omitted.
Modulus and power commute, so you can use ((n%15)**4)%15 to keep it small for any integer n
Is "commute" the right word for that? I'm not a mathematician, but it doesn't match any usage in my poorly trained math-lang semantic net.
> If the commutative property holds for a pair of elements under a certain binary operation then the two elements are said to commute under that operation.
Re: Euler's Fizzbuzz (2020)
#70Earlier quoted context omitted.
https://medium.com/@c0D3M/introduction-to-rsa-e8cb39af508e EDIT: Pasting into Lynx screwed formatting from Groff.
> a^n % b = a % b. That is not generally true. A quick counterexample: a = 2, n = 3, b = 5 2^3 % 5 = 8 % 5 = 3 2 % 5 = 2 3 != 2