This Python program reads two integers m and n and computes the value of the recursive function Q(m, n) based on the following rules:
m = 1 or n = 1, then Q(m, n) = 1.m = n, then Q(m, n) = 1 + Q(m, m - 1).m > n, then Q(m, n) = Q(m - 1, n) + Q(m - 1, n - 1).Q(m, n) = Q(m, n - 1) + Q(m - 1, n).
def Q(m: int, n: int) -> int:
if m == 1 or n == 1:
return 1
elif m == n:
return 1 + Q(m, m - 1)
elif m > n:
return Q(m - 1, n) + Q(m - 1, n - 1)
else:
return Q(m, n - 1) + Q(m - 1, n)
# Read inputs from user
m = int(input("Enter value for m: "))
n = int(input("Enter value for n: "))
result = Q(m, n)
print(f"Q({m}, {n}) = {result}")
Q(3, 2) = 5