Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: please i need explanation

Reply
Thread Tools

Re: please i need explanation

 
 
Jussi Piitulainen
Guest
Posts: n/a
 
      01-11-2013
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
> 11.01.2013 17:35, (E-Mail Removed) wrote:
> > def factorial(n):
> > if n<2:
> > return 1
> > f = 1
> > while n>= 2:
> > f *= n
> > f -= 1
> > return f

>
> please it works.but don’t get why the return 1 and the code below.


Ignoring the error that has been pointed out, this code seems to be
"optimized" to avoid multiplication by 1. I doubt if the difference is
measurable. If anyone cares enough to measure:

def fast_factorial(n):
if n < 2: return 1
f = 1
while n > 1:
f *= n
n -= 1
return f

def slow_factorial(n):
f = 1
while n != 0:
f *= n
n -= 1
return f

(Untested, and just kidding. For fast factorial routines, search for a
paper by Richard Fateman on the web. They're in Common Lisp, but the
branching techniques should be generally applicable.)

Additionally, your code produces nonsense quietly when called with
(real) numbers outside its intended domain (negative integers and
non-integers are outside). Not a good idea. (My version will loop
indefinitely instead. Better than a wrong answer but also not very
good. Caller beware.
 
Reply With Quote
 
 
 
 
Thomas Rachel
Guest
Posts: n/a
 
      01-11-2013
Am 11.01.2013 17:33 schrieb (E-Mail Removed):

>> def factorial(n):
>> if n<2:
>> return 1
>> f = 1
>> while n>= 2:
>> f *= n
>> f -= 1
>> return f
>>
>>
>>

> please it works.


I doubt this.

If you give n = 4, you run into an endless loop.


> but don’t get why the return 1 and the code below.


The "if n < 2: return 1" serves to shorten the calculation process
below. It is redundant, as you have a "f = 1" and a "return f" for n < 2.

The code below first sets f, which holds the result, to 1 and then
multiplies it by n in each step. As the loop should contain a 'n -= 1',
n decreases by 1 every step, turning it into f = n * (n-1) * (n-2) * ...
* 2 and then, as n is not >= 2 any longer, stops the loop, returning f.

HTH,

Thomas
 
Reply With Quote
 
 
 
 
kwakukwatiah@gmail.com
Guest
Posts: n/a
 
      01-11-2013


-----Original Message-----
From: K. Elo
Sent: Friday, January 11, 2013 3:56 AM
To: (E-Mail Removed)
Subject: Re: please i need explanation

Hi!

Since there is no stated question, I need to guess:

n -= 1 (instead of "f -= 1")

should work.

Or maybe the question was a totally different one...

-Kimmo

11.01.2013 17:35, (E-Mail Removed) wrote:
> def factorial(n):
> if n<2:
> return 1
> f = 1
> while n>= 2:
> f *= n
> f -= 1
> return f
>
>
>

please it works.but don’t get why the return 1 and the code below.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
please i need explanation kwakukwatiah@gmail.com Python 2 01-13-2013 09:08 PM
Re: please i need explanation Karim Python 0 01-11-2013 10:41 AM
Re: please i need explanation Vincent Vande Vyvre Python 0 01-11-2013 10:11 AM
Re: please i need explanation Karim Python 0 01-11-2013 09:59 AM
Re: please i need explanation K. Elo Python 0 01-11-2013 09:56 AM



Advertisments