# Non Fibonacci Numbers

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, ……..

**Examples :**

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.

## C++

`// C++ program to find n'th Fibonacci number ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns n'th Non-Fibonacci number ` `int` `nonFibonacci(` `int` `n) ` `{ ` ` ` `// curr is to keep track of current fibonacci ` ` ` `// number, prev is previous, prevPrev is ` ` ` `// previous of previous. ` ` ` `int` `prevPrev=1, prev=2, curr=3; ` ` ` ` ` `// While count of non-fibonacci numbers ` ` ` `// doesn't become negative or zero ` ` ` `while` `(n > 0) ` ` ` `{ ` ` ` `// Simple Fibonacci number logic ` ` ` `prevPrev = prev; ` ` ` `prev = curr; ` ` ` `curr = prevPrev + prev; ` ` ` ` ` `// (curr - prev - 1) is count of ` ` ` `// non-Fibonacci numbers between curr ` ` ` `// and prev. ` ` ` `n = n - (curr - prev - 1); ` ` ` `} ` ` ` ` ` `// n might be negative now. Make sure it ` ` ` `// becomes positive by removing last added ` ` ` `// gap. ` ` ` `n = n + (curr - prev - 1); ` ` ` ` ` `// n must be now positive and less than or equal ` ` ` `// to gap between current and previous, i.e., ` ` ` `// (curr - prev - 1); ` ` ` ` ` `// Now add the positive n to previous Fibonacci ` ` ` `// number to find the n'th non-fibonacci. ` ` ` `return` `prev + n; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `cout << nonFibonacci(5); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find ` `// n'th Fibonacci number ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Returns n'th Non- ` ` ` `// Fibonacci number ` ` ` `static` `int` `nonFibonacci(` `int` `n) ` ` ` `{ ` ` ` ` ` `// curr is to keep track of ` ` ` `// current fibonacci number, ` ` ` `// prev is previous, prevPrev ` ` ` `// is previous of previous. ` ` ` `int` `prevPrev = ` `1` `, prev = ` `2` `, curr = ` `3` `; ` ` ` ` ` `// While count of non-fibonacci ` ` ` `// numbers doesn't become ` ` ` `// negative or zero ` ` ` `while` `(n > ` `0` `) ` ` ` `{ ` ` ` `// Simple Fibonacci number logic ` ` ` `prevPrev = prev; ` ` ` `prev = curr; ` ` ` `curr = prevPrev + prev; ` ` ` ` ` `// (curr - prev - 1) is count ` ` ` `// of non-Fibonacci numbers ` ` ` `// between curr and prev. ` ` ` `n = n - (curr - prev - ` `1` `); ` ` ` `} ` ` ` ` ` `// n might be negative now. Make ` ` ` `// sure it becomes positive by ` ` ` `// removing last added gap. ` ` ` `n = n + (curr - prev - ` `1` `); ` ` ` ` ` `// n must be now positive and less ` ` ` `// than or equal to gap between ` ` ` `// current and previous, i.e., ` ` ` `// (curr - prev - 1); ` ` ` ` ` `// Now add the positive n to ` ` ` `// previous Fibonacci number ` ` ` `// to find the n'th non-fibonacci. ` ` ` `return` `prev + n; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main (String args[]) ` ` ` `{ ` ` ` `System.out.println(nonFibonacci(` `5` `)); ` ` ` `} ` `} ` ` ` `// This code is contributed by aj_36 ` |

*chevron_right*

*filter_none*

## Python

`# Python program to find n'th ` `# Fibonacci number ` ` ` `# Returns n'th Non-Fibonacci ` `# number ` `def` `nonFibonacci(n): ` ` ` ` ` `# curr is to keep track of ` ` ` `# current fibonacci number, ` ` ` `# prev is previous, prevPrev ` ` ` `# is previous of previous. ` ` ` `prevPrev ` `=` `1` ` ` `prev ` `=` `2` ` ` `curr ` `=` `3` ` ` ` ` `# While count of non-fibonacci ` ` ` `# numbers doesn't become ` ` ` `# negative or zero ` ` ` `while` `n > ` `0` `: ` ` ` `prevPrev ` `=` `prev ` ` ` `prev ` `=` `curr ` ` ` `curr ` `=` `prevPrev ` `+` `prev ` ` ` ` ` `# (curr - prev - 1) is ` ` ` `# count of non-Fibonacci ` ` ` `# numbers between curr ` ` ` `# and prev. ` ` ` `n ` `=` `n ` `-` `(curr ` `-` `prev ` `-` `1` `) ` ` ` ` ` `# n might be negative now. ` ` ` `# Make sure it becomes positive ` ` ` `# by removing last added gap. ` ` ` `n ` `=` `n ` `+` `(curr ` `-` `prev ` `-` `1` `) ` ` ` ` ` `# n must be now positive and ` ` ` `# less than or equal to gap ` ` ` `# between current and previous, ` ` ` `# i.e., (curr - prev - 1) ` ` ` ` ` `# Now add the positive n to ` ` ` `# previous Fibonacci number to ` ` ` `# find the n'th non-fibonacci. ` ` ` `return` `prev ` `+` `n ` ` ` `# Driver code ` `print` `(nonFibonacci(` `5` `)) ` ` ` `# This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find ` `// n'th Fibonacci number ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Returns n'th Non- ` ` ` `// Fibonacci number ` ` ` `static` `int` `nonFibonacci (` `int` `n) ` ` ` `{ ` ` ` ` ` `// curr is to keep track of ` ` ` `// current fibonacci number, ` ` ` `// prev is previous, prevPrev ` ` ` `// is previous of previous. ` ` ` `int` `prevPrev = 1, prev = 2, curr = 3; ` ` ` ` ` `// While count of non-fibonacci ` ` ` `// numbers doesn't become ` ` ` `// negative or zero ` ` ` `while` `(n > 0) ` ` ` `{ ` ` ` `// Simple Fibonacci number logic ` ` ` `prevPrev = prev; ` ` ` `prev = curr; ` ` ` `curr = prevPrev + prev; ` ` ` ` ` `// (curr - prev - 1) is count ` ` ` `// of non-Fibonacci numbers ` ` ` `// between curr and prev. ` ` ` `n = n - (curr - prev - 1); ` ` ` `} ` ` ` ` ` `// n might be negative now. Make ` ` ` `// sure it becomes positive by ` ` ` `// removing last added gap. ` ` ` `n = n + (curr - prev - 1); ` ` ` ` ` `// n must be now positive and less ` ` ` `// than or equal to gap between ` ` ` `// current and previous, i.e., ` ` ` `// (curr - prev - 1); ` ` ` ` ` `// Now add the positive n to ` ` ` `// previous Fibonacci number ` ` ` `// to find the n'th non-fibonacci. ` ` ` `return` `prev + n; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `Console.WriteLine (nonFibonacci(5)); ` ` ` `} ` `} ` ` ` `//This code is contributed by aj_36 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find ` `// n'th Fibonacci number ` ` ` `// Returns n'th Non- ` `// Fibonacci number ` `function` `nonFibonacci(` `$n` `) ` `{ ` ` ` `// curr is to keep track of ` ` ` `// current fibonacci number, ` ` ` `// prev is previous, prevPrev ` ` ` `// is previous of previous. ` ` ` `$prevPrev` `= 1; ` ` ` `$prev` `= 2; ` ` ` `$curr` `= 3; ` ` ` ` ` `// While count of non-fibonacci ` ` ` `// numbers doesn't become ` ` ` `// negative or zero ` ` ` `while` `(` `$n` `> 0) ` ` ` `{ ` ` ` `// Simple Fibonacci ` ` ` `// number logic ` ` ` `$prevPrev` `= ` `$prev` `; ` ` ` `$prev` `= ` `$curr` `; ` ` ` `$curr` `= ` `$prevPrev` `+ ` `$prev` `; ` ` ` ` ` `// (curr - prev - 1) is count ` ` ` `// of non-Fibonacci numbers ` ` ` `// between curr and prev. ` ` ` `$n` `= ` `$n` `- (` `$curr` `- ` `$prev` `- 1); ` ` ` `} ` ` ` ` ` `// n might be negative now. Make ` ` ` `// sure it becomes positive by ` ` ` `// removing last added gap. ` ` ` `$n` `= ` `$n` `+ (` `$curr` `- ` `$prev` `- 1); ` ` ` ` ` `// n must be now positive and ` ` ` `// less than or equal to gap ` ` ` `// between current and previous, ` ` ` `// i.e., (curr - prev - 1); ` ` ` ` ` `// Now add the positive n to ` ` ` `// previous Fibonacci number ` ` ` `// to find the n'th non-fibonacci. ` ` ` `return` `$prev` `+ ` `$n` `; ` `} ` ` ` `// Driver code ` `echo` `nonFibonacci(5); ` ` ` `// This code is contributed by m_kit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

10

**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 contribute@geeksforgeeks.org. 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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Even Fibonacci Numbers Sum
- GCD and Fibonacci Numbers
- Sum of Fibonacci Numbers
- Prime numbers and Fibonacci
- Sum of Fibonacci Numbers in a range
- Sum of squares of Fibonacci numbers
- The Magic of Fibonacci Numbers
- Find the sum of first N odd Fibonacci numbers
- Alternate Fibonacci Numbers
- Program for Fibonacci numbers
- C Program for Fibonacci numbers
- Interesting facts about Fibonacci numbers
- Find two Fibonacci numbers whose sum can be represented as N
- Remove all the fibonacci numbers from the given array
- Program to print first n Fibonacci Numbers | Set 1
- Sum of numbers in the Kth level of a Fibonacci triangle
- Large Fibonacci Numbers in Java
- Sum of all Non-Fibonacci numbers in a range for Q queries
- Sum of Fibonacci Numbers with alternate negatives
- Find the GCD of N Fibonacci Numbers with given Indices