-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorial.py
More file actions
41 lines (29 loc) · 1.28 KB
/
factorial.py
File metadata and controls
41 lines (29 loc) · 1.28 KB
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
'''
The problem statement: Find X! - Y! where X and Y are values taken from the User. calculate the number of decimal digits in the
result. Find the number of bytes required to store the result.
'''
import sys
def factorial(x): #Defining a function called factorial
fact= x
for y in range(1,x-1):
fact = fact *(x-y)
return fact #Returns the factorial of the number
x = int(input("Please enter X:"))
y = int(input("Please enter Y:"))
fact_x = factorial(x)#Calling the factorial function
fact_y = factorial(y)
difference = fact_x - fact_y
no_of_bytes =((len(bin(difference)))-2)/8 #Bin converts the number into binary, len gives the length of the string , we subtract -2 coz it includes 0b as b and we divide it by 8 to find the no of bytes
if(no_of_bytes > int(no_of_bytes)):
no_of_bytes =int(no_of_bytes)+ 1 #Consider no_of_bytes as 3.5 and work through the if statement
else:
no_of_bytes = int(no_of_bytes)
remainder = difference # the divident becomes the difference
quotient = 0
while( remainder >= 10):
remainder = remainder/10
quotient = quotient + 1
no_of_decimal_digits = quotient + 1
print ( "The value of Z: " ,difference)
print ( "The number of decimal digits in Z : " ,no_of_decimal_digits)
print ( "The number of bytes required to store Z : ",no_of_bytes)