Factorial -- my code works, but did I do it the "right way"?


def factorial(x):
    digit_list = []
    while x > 0:
       x -=1
    y = 1
    for digit in digit_list:
        y *= digit
    return y

My function creates a list (digit_list), then just multiplies the numbers in the list. I tested with a bunch of different integers and it looks like it returns the correct result each time. I assume there's a more efficient way of achieving the same result (like one that does not require creating a list). I passed the code checker so I can start the next section, but I was curious if there was a correct way, or a different way that we were supposed to complete this exercise? I looked at the hint and didn't quite understand what it was suggesting, so I'm assuming I did not write this the recommended way. Sort of related question. . . as long as it works, does it matter?


Oops... I just realized that I replied with previous exercise, "digit_sum". Here's what I had on Factorial:

def factorial(x):
    total = 1
    x = int(x)
    for i in range(x):
        total *= i+1
    return total

As in the previous explanation, this bypasses your method of populating the "digit_list" first. I kept getting a zero until I remembered that my "i" value was starting at zero which... adding the "+1" cleared that up.

Still pretty new to all of this but enjoying the challenge! Good luck.


Thanks for the response. It totally didn't occur to me that I could just use "for i in range(x)" and that would give me a range of numbers up to whatever number was passed to the function.

I don't quite understand why "total *= i+1" works though. I would think the " + 1" part would mess it up, where everytime it multiplied the total by the next number in the range it would also add 1 so the final total would be too high.


The way I understand it (could be wrong), but the "i" will start with a value of zero which, naturally, as we're using multiplication the 0*1 == 0... then every subsequent number in the list will result in 0 for the same reason. Try this to help visualize it:

def factorial(x):
    total = 1
    x = int(x)
    for i in range(x):
        total *= (i+1)  # I suppose you could also add the () to clean it up.
        print i # <-- This will print the "i" value for each run in the loop.
    return total

When I put in the print command in the loop I noticed that the first "i" value generated for the range[5] (for example) was 0. Hope that makes sense.


My approach for this task was recursive:

def factorial(x):
    if x == 1:
        return 1
        return x*factorial(x-1)

Have fun with coding :grinning:


Woah, nice! I didn't even know you could do that! Thanks @inter2k3.


Ohhhh. So, starting with "0" the "+1" just increments it to 1 first time through the loop and then continues to increment it after that. Your function is definitely better. thanks for the education!


and my approach to this topic is:

def factorial(x):
for i in range(1,x):
return x
first of all we assume that x would be an integer.No need to convert into a integer first.
than,by the for..range function,we create a list of numbers from 1 to the number x-1,which is stored in [i],and than we multiply the number with each number in [i].
We return the result which is x itself.


In programming is this efficient to continually recall the function?


since i am new to python - i don't know how efficient it is.
but i don't care about performance issues when using python
if i want to write fast code - i definitely don't use python since its very slow compared to C++ or Java for example.


continually recalling the function is called "recursion". Your function is recursive when it calls itself. Try looking up on Google "recursion vs iteration". You will find many answers to that, also many advanced ones.

It really depends on what you do whether you use recursion or iteration. Usually, calculating a factorial is most times the introduction to recursion, as the code is very comprehensible and easy. Most times, recursive functions are shorter to code than iterative ones, but that doesn't mean they are always better.

Hope that clears it up a bit, I recommend googling! Happy coding


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.