Re: please i need explanation
On Sun, 13 Jan 2013 15:04:34 -0600, Tony the Tiger wrote: On Fri, 11 Jan 2013 09:35:10 -0600, kwakukwatiah wrote: HTMLHEAD/HEAD BODY dir=ltr DIV dir=ltr DIV style=FONT-FAMILY: 'Calibri'; COLOR: #00; FONT-SIZE: 12pt DIVdef factorial(n):/DIV Right, another html junkie, on windoze, no doubt. X-Mailer: Microsoft Windows Live Mail 15.4.3508.1109 -- http://mail.python.org/mailman/listinfo/python-list
please i need explanation
def factorial(n): if n2: return 1 f = 1 while n= 2: f *= n f -= 1 return f -- http://mail.python.org/mailman/listinfo/python-list
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, kwakukwat...@gmail.com wrote: def factorial(n): if n2: return 1 f = 1 while n= 2: f *= n f -= 1 return f -- http://mail.python.org/mailman/listinfo/python-list
Re: please i need explanation
On 11/01/2013 16:35, kwakukwat...@gmail.com wrote: def factorial(n): if n2: return 1 f = 1 while n= 2: f *= n f -= 1 return f What explanation this a function representing the math factorial. You provide a parameter n: if n est lower than 2 the factorial is 1 (return by the function). in other case you multiply previous factoriel value by n (f *= n = f = f *n). And you decrement n by 1 (f -=1 = f = f - 1). This gives n*(n-)*(n-2) general formula for factorial. Regards Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: please i need explanation
Le 11/01/13 16:35, kwakukwat...@gmail.com a écrit : def factorial(n): if n2: return 1 f = 1 while n= 2: f *= n f -= 1 return f I guess you mean: f = 1 while n= 2: f *= n n -= 1 return f Try it. -- Vincent V.V. Oqapy https://launchpad.net/oqapy . Qarte https://launchpad.net/qarte . PaQager https://launchpad.net/paqager -- http://mail.python.org/mailman/listinfo/python-list
Re: please i need explanation
-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, kwakukwat...@gmail.com wrote: def factorial(n): if n2: 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: please i need explanation
On 11/01/2013 17:33, kwakukwat...@gmail.com wrote: -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, kwakukwat...@gmail.com wrote: def factorial(n): if n2: 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. factorial or n! = n*(n-1)*(n-2) for n 1. For n = 0, 1 the factorial is 1 the reason for the 'return 1'. f *= n = f = f* n f -= 1 = f = f - 1 but sure this is not correct as Kimmo said n = n - 1 or n -= 1 Regards Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: please i need explanation
kwakukwat...@gmail.com writes: 11.01.2013 17:35, kwakukwat...@gmail.com wrote: def factorial(n): if n2: 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. :) -- http://mail.python.org/mailman/listinfo/python-list
Re: please i need explanation
Am 11.01.2013 17:33 schrieb kwakukwat...@gmail.com: def factorial(n): if n2: 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: please i need explanation
I read the question as I've got this function and it does what I expect but I don't understand the code. On that basis... The function creates a factorialfor the input number 'n' (i.e. 1*2*3*4.*n) The first 2 lines checks to see that the input is less than 2 and, if so, returns a value of 1 The rest of the code counts down from n to 1 multiplying f by n at each iteration. If I guessed the right question, reply to the post for further clarification. Steve On 11/01/2013 16:33, kwakukwat...@gmail.com wrote: -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, kwakukwat...@gmail.com wrote: def factorial(n): if n2: 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: please i need explanation
On 11/01/13 16:35:10, kwakukwat...@gmail.com wrote: def factorial(n): if n2: return 1 f = 1 while n= 2: f *= n f -= 1 U think this line should have been: n -= 1 return f Hope this helps, -- HansM -- http://mail.python.org/mailman/listinfo/python-list