Let's take an exponential time algorithm and fix it up so it can run in polynomial time!
The Fibonacci sequence is a sequence of numbers where each number is the sum of the two numbers before it. Like this:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
We want a function that, given an index in the sequence, returns the Fibonacci number at that index. For example:
fib(0) -> 0fib(1) -> 1fib(2) -> 1fib(3) -> 2fib(4) -> 3fib(5) -> 5fib(6) -> 8fib(7) -> 13Here are the implementation details to do it in polynomial time:
n represents the index of the desired Fibonacci number.grandparent = 0, parent = 1, and a placeholder current to store the new Fibonacci number at each step.n - 1 times. (For example, if n = 2, one iteration occurs.)current = parent + grandparentparent and grandparent) to maintain the sequence.current.Our data scientists at LockedIn have found that the growth of the average influencer's follow count is roughly the same growth rate as the Fibonacci sequence! In other words, after 6 weeks of good social media posts, the average influencer will have 8 followers. After 7 weeks, 13 followers, and so on.
The trouble is, our current implementation of the fib function takes so long (exponential time!) to complete that when our influencers navigate to their analytics page it often never completes loading!
Adjust the fib function using the given algorithm to achieve polynomial runtime.