5 Factorial HELLP


#1

def factorial(x):
if x<0:
return "none"
elif float(x) != int(x):
return "none"
elif x==0:
return 1
elif x>1:
while (x-1)>0:
return x*factorial(x-1)
else:
return 1

when I submit this by itself it tells me that factorial(1) gives None as an output.
If I write print factorial(3) and submit it gives me errors on lines 10 and 13


#3

You can't call a function from inside itself. It makes an infinite loop of confusion lol. Could you post the link to the specific problem that you are working on so I can get a better idea of what you are being asked to do.

If it is what I think it is than here is my code:

def factorial(x):
result = 1

for i in range(0,x):
    result = result * (i+1)
return result

#4

This is called recursion, which is a valid form of programming. So you can in fact call the function from within itself so long as you pass in new parameters. It is important to have a base case that when reached will terminate the recursion and go through the return stack.

def factorial(n):
    if n < 2: return 1
    return n * factorial(n-1)

#5

Thank you for the clarification. I am familiar with recursion as I have used it before. I just was not being specific enough in my response. In this particular case, recursion seemed incorrect but feel free to correct me if I am wrong.


#6

Thanks everyone, but I figured it out a while ago (I think).


#7

For beginners recursion will not be the go to approach. Most people won't be familiar with it and will instead look for an algorithmic approach. In the case of factorial, recursion is not necessarily the most efficient or fastest method, either.

Your approach above is simple and efficient. I would only change a couple of things.

def factorial(n):
    n = abs(n)           # n must be positive or 0
    if n < 2: return 1   # 0! and 1! are both 1
    result = 1
    for i in range(1, n):
        result *= n + 1
    return result

#8

Is there a way to complete this exercise using the while function?


#9
if n < 2: return 1
result = 1
while n > 1:
    result *= n
    n -= 1
return result

#10

def factorial (x):
    if x >0:
        return x*factorial(x - 1)
    else:
        return 1

#11

What if x is 1?

def factorial (x):
    return x * factorial(x -1) if x > 1 else 1

or

def factorial (x):
    return reduce(lambda a,b: a * b, range(1, abs(x)+1)) or 1

#12

When x is 1 the code will return 1*factorial(0)=1*1=1
maybe I should add:
elif x<0:
print "Please, input a non-negative integer"

because according to condition of the task we need to calculate only them


#13

Brain fart, on my part. You are right. What I meant to ask was, 'what if x is negative?'

We could protect the program from invalid inputs by making it a dependency of another, say, get_factorial() and validate inputs before calling factorial(). This way the function can be left pure. Same thing in, same thing out. Nothing asked for outside and nothing changed.


#14

def factorial(x):
result = 1

while x != 1:
    result *= x
    x -= 1

return result

Note - I did != 1 instead of 0, as 4 * 3 * 2 is same as 4 * 3 * 2 * 1. No need to include 1.


#15

Yes, this could be better


#16

Does not address the negative inputs problem.