4 ms·
Those are very convenient numbers for fizz buzz. Have you ever pulled out that trick in an interview?
by curun1r 10y ago
Those are very convenient numbers for fizz buzz. Have you ever pulled out that trick in an interview?
- raphlinus 10y agoYes, just last week. I'm applying for a batch at Recurse Center as a sabbatical this fall, and wanted to put in something to show off a bit.
- contravariant 10y agoHmm, I wonder if this is more efficient than calculating mod 15 directly. It's basically something like gcd15(int x) { return ((x * 0xaaaaaaab) < (0xfffffff/3 + 1) ? 1 : 3) * ((x * 0xaaaaaaab) < (0xfffffff/5 + 1) ? 1 : 5) } versus something like gcd15(int x) { LOOKUP_TABLE = { /* gcd(n,15) for n = 0,...,15 */ } int q = (x * ((1 << 28) / 15)) >> 28; return LOOKUP_TABLE[x - 15 * q]; } Well, you're probably better of using the method in the article for the division, but this should work for small numbers.