How on Factorial

Dave Angel davea at ieee.org
Thu Oct 28 09:52:55 EDT 2010


On 2:59 PM, Xavier Ho wrote:
> On 27 October 2010 18:27, Arnaud Delobelle<arnodel at gmail.com>  wrote:
>
>> True.  It's far too verbose.  I'd go for something like:
>>
>>     f=lambda n:n<=0 or n*f(~-n)
>>
>> I've saved a few precious keystrokes and used the very handy ~- idiom!
>>
> Huh, I've never seen that one before. Seems to work on both positive and
> negative numbers. Is there a caveat to this?
>
> Cheers,
> Xav
>
The ~- trick only works on two's complement numbers.  I've worked on 
machines in the past that used one's complement, and this wouldn't work 
there.

DaveA




More information about the Python-list mailing list