Given a positive integer n, the task is to print the n’th non Fibonacci number. The Fibonacci numbers are defined as:
Fib(0) = 0 Fib(1) = 1 for n >1, Fib(n) = Fib(n-1) + Fib(n-2)
First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, ……..
Input : n = 2 Output : 6 Input : n = 5 Output : 10
A naive solution is to find find Fibonacci numbers and then print first n numbers not present in the found Fibonacci numbers.
A better solution is to use the formula of Fibonacci numbers and keep adding of gap between two consecutive Fibonacci numbers. Value of sum of gap is count of non-fibonacci numbers seen so far. Below is implementation of above idea.
Time Complexity : O(n)
Auxiliary Space : O(1)
The above problem and solution are contributed by Hemang Sarkar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sum of all Non-Fibonacci numbers in a range for Q queries
- Check if a M-th fibonacci number divides N-th fibonacci number
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- Zeckendorf's Theorem (Non-Neighbouring Fibonacci Representation)
- Find the next Non-Fibonacci number
- Count of integers up to N which are non divisors and non coprime with N
- Minimum increments of Non-Decreasing Subarrays required to make Array Non-Decreasing
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space
- Sum of Fibonacci Numbers
- GCD and Fibonacci Numbers
- Even Fibonacci Numbers Sum
- Largest subset whose all elements are Fibonacci numbers
- Interesting facts about Fibonacci numbers
- The Magic of Fibonacci Numbers
- C Program for Fibonacci numbers
- Prime numbers and Fibonacci
- Print first n Fibonacci Numbers using direct formula
- Generating large Fibonacci numbers using boost library