~2 min read • Updated Oct 12, 2025
Program Overview
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:
- If
m = 1orn = 1, thenQ(m, n) = 1. - If
m = n, thenQ(m, n) = 1 + Q(m, m - 1). - If
m > n, thenQ(m, n) = Q(m - 1, n) + Q(m - 1, n - 1). - Otherwise,
Q(m, n) = Q(m, n - 1) + Q(m - 1, n).
Python Code:
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}")
Sample Output (input: m = 3, n = 2):
Q(3, 2) = 5
Written & researched by Dr. Shahin Siami