I am a student studying for my BS in Computer Science. We recently got asked this question in on of my class and with no book to reference I am going to ask it here for a little help. I know I will have to use induction but I don't really understand how
Input: a, a natural number
Output: F_a, the ath Fibonacci number
Let i=1
While i<=a do
if i=1 or i=2, then Fib(i)<-1
else Fib(i)<-Fib(i-1)+ Fib(i-2)
i <- i+1
end
Print Fib(a)
I think you might be overthinking the problem. The induction is pretty simple; let's go through the steps:
Base case: The algorithm correctly puts Fib(1) = 1, Fib(2) = 1.
(Strong) Inductive step: Suppose the algorithm correctly calculates the values for Fibonacci numbers up to $k$.
Then, it puts Fib(k+1) = Fib(k) + Fib(k-1), so the value of Fib(k+1) is correctly calculated too.