Yu-Xi Lim wrote: > John Machin wrote: > > > How did you avoid division when testing for leap year? > > > > Well, you're testing for a modulus, not really a division, so a nasty hack: > > def isleapyear(year): > return not year&3 > > works for 1901-2099. > > But programming like this causes all kinds of problems in future. Let's > just hope there aren't too many 1MHz 8-bit computers left by then.
No, I meant a wider range, at least 1 <= year <= 9999. And it's not only 1MHz 8-bit computers where non-nasty division/modulus avoidance hacks can come in handy -- IIRC the millicode for the 32-bit HP PA-RISC architecture takes 32 divide-step instructions to do an unsigned 32-bit division. Division is intrinsically relatively slow on most boxes, but division by a non-power-of-2 constant can be sped up, and so can "x % constant == 0". Cheers, John -- http://mail.python.org/mailman/listinfo/python-list