From bd96a9174b49e1da7d80c96e9d23463c90f1e556 Mon Sep 17 00:00:00 2001 From: Pooja Date: Fri, 17 Jan 2020 19:30:10 +0530 Subject: [PATCH 1/6] Create factorial_iterative.py --- maths/factorial_iterative.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 maths/factorial_iterative.py diff --git a/maths/factorial_iterative.py b/maths/factorial_iterative.py new file mode 100644 index 000000000000..3e520a3c5ca6 --- /dev/null +++ b/maths/factorial_iterative.py @@ -0,0 +1,12 @@ +# factorial of a number + + +n=int(input("Enter a number")) + +def factorial(n): + fact=1 + for i in range(1,n+1): + fact*=i + return fact + +print(factorial(n)) From 7da46aabad0139a221430e49cad3296f4de6ae28 Mon Sep 17 00:00:00 2001 From: Pooja Date: Fri, 17 Jan 2020 21:09:25 +0530 Subject: [PATCH 2/6] Update factorial_iterative.py --- maths/factorial_iterative.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/maths/factorial_iterative.py b/maths/factorial_iterative.py index 3e520a3c5ca6..1cf96a864cfc 100644 --- a/maths/factorial_iterative.py +++ b/maths/factorial_iterative.py @@ -2,11 +2,13 @@ n=int(input("Enter a number")) - def factorial(n): fact=1 for i in range(1,n+1): fact*=i return fact -print(factorial(n)) +if n>=0: + print(factorial(n)) +else: + print("number should be positive") From 648e170e49a5e2c71dbbb298a75d066e05c71281 Mon Sep 17 00:00:00 2001 From: Pooja Date: Sat, 18 Jan 2020 12:38:07 +0530 Subject: [PATCH 3/6] Update factorial_iterative.py --- maths/factorial_iterative.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/maths/factorial_iterative.py b/maths/factorial_iterative.py index 1cf96a864cfc..94a6cd5bfd2a 100644 --- a/maths/factorial_iterative.py +++ b/maths/factorial_iterative.py @@ -11,4 +11,9 @@ def factorial(n): if n>=0: print(factorial(n)) else: - print("number should be positive") + raise ValueError("ValueError: factorial() not defined for negative vlaues") + + + + + From 9f75648b66af95d467f35b8d8ae3c65eb2478cda Mon Sep 17 00:00:00 2001 From: Pooja Date: Sat, 18 Jan 2020 13:01:57 +0530 Subject: [PATCH 4/6] Update factorial_iterative.py --- maths/factorial_iterative.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/maths/factorial_iterative.py b/maths/factorial_iterative.py index 94a6cd5bfd2a..8120f4b83246 100644 --- a/maths/factorial_iterative.py +++ b/maths/factorial_iterative.py @@ -1,7 +1,7 @@ # factorial of a number - -n=int(input("Enter a number")) +if __name__=="__main__": + n=int(input("Enter a number")) def factorial(n): fact=1 for i in range(1,n+1): @@ -17,3 +17,6 @@ def factorial(n): + + + From 2a96cfc72e14104273161a98fa554f28cec174d6 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 18 Jan 2020 13:47:11 +0100 Subject: [PATCH 5/6] print(f"factorial{n} is {factorial(n)}") --- maths/factorial_iterative.py | 44 +++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/maths/factorial_iterative.py b/maths/factorial_iterative.py index 8120f4b83246..249408cb5b4e 100644 --- a/maths/factorial_iterative.py +++ b/maths/factorial_iterative.py @@ -1,22 +1,30 @@ -# factorial of a number +# factorial of a positive integer -- https://en.wikipedia.org/wiki/Factorial -if __name__=="__main__": - n=int(input("Enter a number")) -def factorial(n): - fact=1 - for i in range(1,n+1): - fact*=i - return fact -if n>=0: - print(factorial(n)) -else: - raise ValueError("ValueError: factorial() not defined for negative vlaues") - +def factorial(n: int) -> int: + """ + >>> import math + >>> all(factorial(i) == math.factorial(i) for i in range(20)) + True + >>> factorial(0.1) + Traceback (most recent call last): + ... + ValueError: factorial() only accepts integral values + >>> factorial(-1) + Traceback (most recent call last): + ... + ValueError: factorial() not defined for negative values + """ + if n != int(n): + raise ValueError("factorial() only accepts integral values") + if n < 0: + raise ValueError("factorial() not defined for negative values") + value = 1 + for i in range(1, n + 1): + value *= i + return value - - - - - +if __name__ == "__main__": + n = int(input("Enter a positivve integer: ").strip() or 0) + print(f"factorial{n} is {factorial(n)}") From ab332a70ba1945a17f04a544799284f03727f1b3 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 18 Jan 2020 13:54:48 +0100 Subject: [PATCH 6/6] Update factorial_recursive.py --- maths/factorial_recursive.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/maths/factorial_recursive.py b/maths/factorial_recursive.py index 4f7074d16587..137112738905 100644 --- a/maths/factorial_recursive.py +++ b/maths/factorial_recursive.py @@ -1,26 +1,24 @@ def factorial(n: int) -> int: """ - Calculate the factorial of specified number + Calculate the factorial of a positive integer + https://en.wikipedia.org/wiki/Factorial - >>> factorial(1) - 1 - >>> factorial(6) - 720 - >>> factorial(0) - 1 - >>> factorial(-1) - Traceback (most recent call last): - ... - ValueError: factorial() not defined for negative values + >>> import math + >>> all(factorial(i) == math.factorial(i) for i in range(20)) + True >>> factorial(0.1) Traceback (most recent call last): ... ValueError: factorial() only accepts integral values + >>> factorial(-1) + Traceback (most recent call last): + ... + ValueError: factorial() not defined for negative values """ - if n < 0: - raise ValueError("factorial() not defined for negative values") if not isinstance(n, int): raise ValueError("factorial() only accepts integral values") + if n < 0: + raise ValueError("factorial() not defined for negative values") return 1 if n == 0 or n == 1 else n * factorial(n - 1)