Count Fibonacci numbers in given range in O(Log n) time and O(1) space - GeeksforGeeks
Count Fibonacci numbers in given range in O(Log n) time and O(1) space
Given a range, count Fibonacci numbers in given range. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, ..
Example:
Input: low = 10, high = 100 Output: 5 There are five Fibonacci numbers in given range, the numbers are 13, 21, 34, 55 and 89. Input: low = 10, high = 20 Output: 1 There is only one Fibonacci Number, 13. Input: low = 0, high = 1 Output: 3 Fibonacci numbers are 0, 1 and 1
We strongly recommend you to minimize your browser and try this yourself first.
A Brute Force Solution is to one by one find all Fibonacci Numbers and count all Fibonacci numbers in given range
An Efficient Solution is to use previous Fibonacci Number to generate next using simple Fibonacci formula that fn = fn-1 + fn-2.
Read full article from Count Fibonacci numbers in given range in O(Log n) time and O(1) space - GeeksforGeeks
No comments:
Post a Comment