Re: please i need explanation

Discussion in 'Python' started by Jussi Piitulainen, Jan 11, 2013.

  1. writes:
    > 11.01.2013 17:35, 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. :)
    Jussi Piitulainen, Jan 11, 2013
    #1
    1. Advertising

  2. Am 11.01.2013 17:33 schrieb :

    >> 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
    Thomas Rachel, Jan 11, 2013
    #2
    1. Advertising

  3. Jussi Piitulainen

    Guest

    -----Original Message-----
    From: K. Elo
    Sent: Friday, January 11, 2013 3:56 AM
    To:
    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, 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.
    , Jan 11, 2013
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    116
    Walter Hurry
    Jan 13, 2013
  2. K. Elo

    Re: please i need explanation

    K. Elo, Jan 11, 2013, in forum: Python
    Replies:
    0
    Views:
    124
    K. Elo
    Jan 11, 2013
  3. Karim

    Re: please i need explanation

    Karim, Jan 11, 2013, in forum: Python
    Replies:
    0
    Views:
    124
    Karim
    Jan 11, 2013
  4. Vincent Vande Vyvre

    Re: please i need explanation

    Vincent Vande Vyvre, Jan 11, 2013, in forum: Python
    Replies:
    0
    Views:
    104
    Vincent Vande Vyvre
    Jan 11, 2013
  5. Karim

    Re: please i need explanation

    Karim, Jan 11, 2013, in forum: Python
    Replies:
    0
    Views:
    101
    Karim
    Jan 11, 2013
Loading...

Share This Page