Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #36614 > unrolled thread
| Started by | kwakukwatiah@gmail.com |
|---|---|
| First post | 2013-01-11 10:33 -0600 |
| Last post | 2013-01-11 13:58 +0100 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: please i need explanation kwakukwatiah@gmail.com - 2013-01-11 10:33 -0600
Re: please i need explanation Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-01-11 14:20 +0200
Re: please i need explanation Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2013-01-11 13:58 +0100
| From | kwakukwatiah@gmail.com |
|---|---|
| Date | 2013-01-11 10:33 -0600 |
| Subject | Re: please i need explanation |
| Message-ID | <mailman.393.1357900426.2939.python-list@python.org> |
-----Original Message----- From: K. Elo Sent: Friday, January 11, 2013 3:56 AM To: python-list@python.org 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, kwakukwatiah@gmail.com 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.
[toc] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2013-01-11 14:20 +0200 |
| Message-ID | <qotr4lrykq8.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #36614 |
kwakukwatiah@gmail.com writes:
> 11.01.2013 17:35, kwakukwatiah@gmail.com 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. :)
[toc] | [prev] | [next] | [standalone]
| From | Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> |
|---|---|
| Date | 2013-01-11 13:58 +0100 |
| Message-ID | <kcp2a1$3ke$1@r03.glglgl.gl> |
| In reply to | #36614 |
Am 11.01.2013 17:33 schrieb kwakukwatiah@gmail.com: >> 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web