From 870eebf349f78047461eb639d60921096179facb Mon Sep 17 00:00:00 2001 From: Yurii <33547678+yuriimchg@users.noreply.github.com> Date: Fri, 18 Oct 2019 08:27:55 +0300 Subject: [PATCH] rewrite the algorithm from scratch (#1351) --- maths/factorial_python.py | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/maths/factorial_python.py b/maths/factorial_python.py index 6c1349fd5..10083af0b 100644 --- a/maths/factorial_python.py +++ b/maths/factorial_python.py @@ -1,19 +1,21 @@ -"""Python program to find the factorial of a number provided by the user.""" +def factorial(input_number: int) -> int: + """ + Non-recursive algorithm of finding factorial of the + input number. + >>> factorial(1) + 1 + >>> factorial(6) + 720 + >>> factorial(0) + 1 + """ -# change the value for a different result -NUM = 10 - -# uncomment to take input from the user -# num = int(input("Enter a number: ")) - -FACTORIAL = 1 - -# check if the number is negative, positive or zero -if NUM < 0: - print("Sorry, factorial does not exist for negative numbers") -elif NUM == 0: - print("The factorial of 0 is 1") -else: - for i in range(1, NUM + 1): - FACTORIAL = FACTORIAL * i - print("The factorial of", NUM, "is", FACTORIAL) + if input_number < 0: + raise ValueError('Input input_number should be non-negative') + elif input_number == 0: + return 1 + else: + result = 1 + for i in range(input_number): + result = result * (i + 1) + return result