# Factorial

#1

I'm just a little suspicious if this is bugged at all. It works supposedly.

If this does indeed work, is it written poorly/ not very robust? I thought the solution would need a little more complexity than this.

``````def factorial(x):
for n in range(1, x):
x = x * n
return x``````

#2

Its actually a really clever solution.

Its fine, it works fine. Simple is actually better in programming, less changes something breaks

#3

Hey thanks! I think i'll still be a little skeptical when things work the first time, but definitely got some confidence now.

#4

that is a good thing, something like this:

hehe

#5

Btw, although it is quite a good solution, it doesn't work completely because factorial(0) should give 1, but in your solution gives 0

#6

To solve this, I had the value of 'factorial' initiated to 1, as multiplying any number to 1 will not change the result and zero factorial is 1. Starting at 1 sounded good.

Even though the above looks like a very elegant solution, simple and efficient, it does not work with zero as pv1011 mentioned.
See my solution below:

``````def factorial(n):
fact = 1
for i in range(1, n+1):
fact *= i
return fact``````

#7

This is my solution, it seems to work for all the numbers I've tried including zero (tell me if it doesn't) - i'm not saying it is the best solution.

``````def factorial(x):
result = 1
for i in range(x-1):
result *= x-i
return result``````

#8

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