forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactorial_perm_comp.py
77 lines (57 loc) · 1.83 KB
/
factorial_perm_comp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Script Name : factorial_perm_comp.py
# Author : Ebiwari Williams
# Created : 20th May 2017
# Last Modified :
# Version : 1.0
# Modifications :
# Description : Find Factorial, Permutation and Combination of a Number
def factorial(n):
fact = 1
while n >= 1:
fact = fact * n
n = n - 1
return fact
def permutation(n, r):
return factorial(n) / factorial(n - r)
def combination(n, r):
return permutation(n, r) / factorial(r)
def main():
print("choose between operator 1,2,3")
print("1) Factorial")
print("2) Permutation")
print("3) Combination")
operation = input("\n")
if operation == "1":
print("Factorial Computation\n")
while True:
try:
n = int(input("\n Enter Value for n "))
print("Factorial of {} = {}".format(n, factorial(n)))
break
except ValueError:
print("Invalid Value")
continue
elif operation == "2":
print("Permutation Computation\n")
while True:
try:
n = int(input("\n Enter Value for n "))
r = int(input("\n Enter Value for r "))
print("Permutation of {}P{} = {}".format(n, r, permutation(n, r)))
break
except ValueError:
print("Invalid Value")
continue
elif operation == "3":
print("Combination Computation\n")
while True:
try:
n = int(input("\n Enter Value for n "))
r = int(input("\n Enter Value for r "))
print("Combination of {}C{} = {}".format(n, r, combination(n, r)))
break
except ValueError:
print("Invalid Value")
continue
if __name__ == "__main__":
main()