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