Calculating factorial
Mikael Olofsson
mikael at isy.liu.se
Wed Dec 6 07:11:54 EST 2000
On 06-Dec-00 Roger Hansen wrote:
> Agree! it's a nice version. But I think the recursive version is even
> clearer.
>
> >>> def fact(n):
> ... if n > 1:
> ... return n*fact(n-1)
> ... return 1
> ...
> >>> fact(4)
> 24
>
> You can change line 3 with
> ... return long(n)*fact(n-1)
>
> and you have long integers
The result of the proposed function with long(n) is of type long for
all n>1. I would actually prefer that the function returns type long
for all legal inputs. The following does that.
>>> def fact(n):
... if n > 1:
... return n*fact(n-1)
... return 1L
...
/Mikael
-----------------------------------------------------------------------
E-Mail: Mikael Olofsson <mikael at isy.liu.se>
WWW: http://www.dtr.isy.liu.se/dtr/staff/mikael
Phone: +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339
Date: 06-Dec-00
Time: 13:05:48
/"\
\ / ASCII Ribbon Campaign
X Against HTML Mail
/ \
This message was sent by XF-Mail.
-----------------------------------------------------------------------
More information about the Python-list
mailing list