Recursion Algorithm Practice: Fibonacci Sequence Generator
kaelscion
Posted on December 19, 2018
for more of our great content, visit the Coding Duck Blog at: ccstechme.com/coding-duck-blog
Hey there Dev community! So, I've got an interview on Thursday for a position at a company near my house. They informed me that recursion algorithm efficiency is important to them and I will be tested on it. Therefore, I will be posting a couple of solutions I've come up with to HackerRank recursion practice problems for a review! Below is my solution to the Fibonacci sequence generator in Python3. I think it is quite good but am open to suggestions for improvement. My test criteria are how quickly my sequence can find the 200,000th Fibonacci number. This solution does it in around 13 seconds (measured with cProfiles) on my local machine. The next closest Python solution I found on HackerRank averaged about 30 seconds on the same setup. What do you all think? Let me know what your questions, critiques, and/or comments are! I look forward to reading them!
def fibonacci(n):
iterator = 1
first_fib_num = 0
second_fib_num = 1
while iterator < n:
added_to = first_fib_num + second_fib_num
first_fib_num = second_fib_num
second_fib_num = added_to
iterator+=1
return(added_to)
print(fibonacci(200000))
Posted on December 19, 2018
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.