AI Assignment 2
HARSH KUMAR MISHRA I22MA059
##question 1
def factorial(num: int):
if num == 0:
return 1
else:
return num * factorial(num - 1)
print(factorial(4))
24
##question 2
##takes in the nth fibonacci number and prints out the series till that number
def fibonacci_numbers(n: int):
f0 = 0
f1 = 1
if (n == 0):
print(0)
elif (n == 1):
print(1)
else:
print(f0)
print(f1)
for i in range(n-1):
f2 = f0 + f1
f0 = f1
f1 = f2
print(f2)
fibonacci_numbers(2)
0
1
1
##question 3
def greatest():
a = int(input("Enter the first number"))
b = int(input("Enter the first number"))
c = int(input("Enter the first number"))
print("The maximum of the three given numbers is : " + str(max(a, b, c)))
greatest()
Enter the first number 1
Enter the first number 22
Enter the first number 36
The maximum of the three given numbers is : 36
# question 4
# minimum number of steps required to measure
# d litre water using jugs of m liters and n
# liters capacity.
def gcd(a, b):
if b==0:
return a
return gcd(b, a%b)
def Pour(toJugCap, fromJugCap, d):
fromJug = fromJugCap
toJug = 0
step = 1
while ((fromJug is not d) and (toJug is not d)):
temp = min(fromJug, toJugCap-toJug)
toJug = toJug + temp
fromJug = fromJug - temp
step = step + 1
if ((fromJug == d) or (toJug == d)):
break
if fromJug == 0:
fromJug = fromJugCap
step = step + 1
if toJug == toJugCap:
toJug = 0
step = step + 1
return step
def minSteps(n, m, d):
if m> n:
temp = m
m = n
n = temp
if (d%(gcd(n,m)) is not 0):
return -1
return(min(Pour(n,m,d), Pour(m,n,d)))
n = 3
m = 5
d = 4
print('Minimum number of steps required is',
minSteps(n, m, d))
Minimum number of steps required is 6
##question 5
def add():
a = int(input("Enter the first number"))
b = int(input("Enter the second number"))
print("The sum of the two numbers is : " + str(a + b))
add()
Enter the first number 11
Enter the second number 45
The sum of the two numbers is : 56
##question 6
def greatest():
a = int(input("Enter the first number"))
b = int(input("Enter the first number"))
c = int(input("Enter the first number"))
print("The maximum of the three given numbers is : " + str(max(a, b, c)))
greatest()
Enter the first number 45
Enter the first number 63
Enter the first number 99
The maximum of the three given numbers is : 99