My issue
Fibonacci Series
Given an integer
𝑁
N, output the
𝑁
𝑡
ℎ
N
th
term in the Fibonacci Series.
The Fibonacci Series is : 0 1 1 2 3 5 8…
Input Format
The first line contains an integer
𝑇
T, denoting the number of test cases.
The next
𝑇
T lines contain a single integer
𝑁
N.
Output Format
For each test case, output the
𝑁
𝑡
ℎ
N
th
term in the Fibonacci Series.
Constraints
1
≤
𝑇
≤
100
1≤T≤100
1
≤
𝑁
≤
100
1≤N≤100
Sample 1:
Input
Output
1
0
Sample 2:
Input
Output
2
1
Sample 3:
Input
Output
7
8
My code
def fibonacci_up_to(max_n):
fib = [0] * (max_n + 1)
fib[0] = 0
if max_n > 0:
fib[1] = 1
for i in range(2, max_n + 1):
fib[i] = fib[i-1] + fib[i-2]
return fib
# Read number of test cases
T = int(input().strip())
# Process each test case
results = []
max_n = 0
test_cases = []
for _ in range(T):
N = int(input().strip())
test_cases.append(N)
if N > max_n:
max_n = N
# Precompute fibonacci numbers up to max_n needed
fib = fibonacci_up_to(max_n)
# Output results for each test case
for N in test_cases:
results.append(fib[N])
for result in results:
print(result)
Learning course: Design and Analysis of Algorithms
Problem Link: Fibonacci Series in Design and Analysis of Algorithms