# Find the next Non-Fibonacci number

Given a number N, the task is to find the next Non-Fibonacci number.

Examples:

Input: N = 4
Output: 6
6 is the next non-fibonacci number after 4

Input: N = 6
Output: 7

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: As the fibonacci series is given as

0, 1, 1, 2, 3, 5, 8, 13, 21, 34….

It can be observed that there does not exists any 2 consecutive fibonacci numbers. Therefore, inorder to find the next Non-Fibonacci number, the following cases arise:

1. If N <= 3, then the next Non-Fibonacci number will be 4
2. If N > 3, then we will check if (N + 1) is fibonacci number or not.
• If (N + 1) is a fibonacci number then (N + 2) will be the next Non-Fibonacci number.
• Else (N + 1) will be the answer

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to check if a ` `// number is perfect square ` `bool` `isPerfectSquare(``int` `x) ` `{ ` `    ``int` `s = ``sqrt``(x); ` `    ``return` `(s * s == x); ` `} ` ` `  `// Function to check if a ` `// number is Fibinacci Number ` `bool` `isFibonacci(``int` `N) ` `{ ` `    ``// N is Fibinacci if either ` `    ``// (5*N*N + 4), (5*N*N - 4) or both ` `    ``// is a perferct square ` `    ``return` `isPerfectSquare(5 * N * N + 4) ` `           ``|| isPerfectSquare(5 * N * N - 4); ` `} ` ` `  `// Function to find ` `// the next Non-Fibinacci Number ` `int` `nextNonFibonacci(``int` `N) ` `{ ` ` `  `    ``// Case 1 ` `    ``// If N<=3, then 4 will be ` `    ``// next Non-Fibinacci Number ` `    ``if` `(N <= 3) ` `        ``return` `4; ` ` `  `    ``// Case 2 ` `    ``// If N+1 is Fibinacci, then N+2 ` `    ``// will be next Non-Fibinacci Number ` `    ``if` `(isFibonacci(N + 1)) ` `        ``return` `N + 2; ` ` `  `    ``// If N+1 is Non-Fibinacci, then N+2 ` `    ``// will be next Non-Fibinacci Number ` `    ``else` `        ``return` `N + 1; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `N = 3; ` `    ``cout << nextNonFibonacci(N) ` `         ``<< endl; ` ` `  `    ``N = 5; ` `    ``cout << nextNonFibonacci(N) ` `         ``<< endl; ` ` `  `    ``N = 7; ` `    ``cout << nextNonFibonacci(N) ` `         ``<< endl; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.*; ` ` `  `class` `GFG{ ` `  `  `// Function to check if a ` `// number is perfect square ` `static` `boolean` `isPerfectSquare(``int` `x) ` `{ ` `    ``int` `s = (``int``) Math.sqrt(x); ` `    ``return` `(s * s == x); ` `} ` `  `  `// Function to check if a ` `// number is Fibinacci Number ` `static` `boolean` `isFibonacci(``int` `N) ` `{ ` `    ``// N is Fibinacci if either ` `    ``// (5*N*N + 4), (5*N*N - 4) or both ` `    ``// is a perferct square ` `    ``return` `isPerfectSquare(``5` `* N * N + ``4``) ` `           ``|| isPerfectSquare(``5` `* N * N - ``4``); ` `} ` `  `  `// Function to find ` `// the next Non-Fibinacci Number ` `static` `int` `nextNonFibonacci(``int` `N) ` `{ ` `  `  `    ``// Case 1 ` `    ``// If N<=3, then 4 will be ` `    ``// next Non-Fibinacci Number ` `    ``if` `(N <= ``3``) ` `        ``return` `4``; ` `  `  `    ``// Case 2 ` `    ``// If N+1 is Fibinacci, then N+2 ` `    ``// will be next Non-Fibinacci Number ` `    ``if` `(isFibonacci(N + ``1``)) ` `        ``return` `N + ``2``; ` `  `  `    ``// If N+1 is Non-Fibinacci, then N+2 ` `    ``// will be next Non-Fibinacci Number ` `    ``else` `        ``return` `N + ``1``; ` `} ` `  `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `N = ``3``; ` `    ``System.out.print(nextNonFibonacci(N) ` `         ``+``"\n"``); ` `  `  `    ``N = ``5``; ` `    ``System.out.print(nextNonFibonacci(N) ` `         ``+``"\n"``); ` `  `  `    ``N = ``7``; ` `    ``System.out.print(nextNonFibonacci(N) ` `         ``+``"\n"``); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## Python 3

 `# Python 3 implementation of the approach ` `from` `math ``import` `sqrt ` ` `  `# Function to check if a ` `# number is perfect square ` `def` `isPerfectSquare(x): ` `    ``s ``=` `sqrt(x) ` `    ``return` `(s ``*` `s ``=``=` `x) ` ` `  `# Function to check if a ` `# number is Fibinacci Number ` `def` `isFibonacci(N): ` ` `  `    ``# N is Fibinacci if either ` `    ``# (5*N*N + 4), (5*N*N - 4) or both ` `    ``# is a perferct square ` `    ``return` `isPerfectSquare(``5` `*` `N ``*` `N ``+` `4``) ``or` `\ ` `            ``isPerfectSquare(``5` `*` `N ``*` `N ``-` `4``) ` ` `  `# Function to find ` `# the next Non-Fibinacci Number ` `def` `nextNonFibonacci(N): ` `     `  `    ``# Case 1 ` `    ``# If N<=3, then 4 will be ` `    ``# next Non-Fibinacci Number ` `    ``if` `(N <``=` `3``): ` `        ``return` `4` ` `  `    ``# Case 2 ` `    ``# If N+1 is Fibinacci, then N+2 ` `    ``# will be next Non-Fibinacci Number ` `    ``if` `(isFibonacci(N ``+` `1``)): ` `        ``return` `N ``+` `2` ` `  `    ``# If N+1 is Non-Fibinacci, then N+2 ` `    ``# will be next Non-Fibinacci Number ` `    ``else``: ` `        ``return` `N  ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``N ``=` `3` `    ``print``(nextNonFibonacci(N)) ` `    ``N ``=` `4` `    ``print``(nextNonFibonacci(N)) ` ` `  `    ``N ``=` `7` `    ``print``(nextNonFibonacci(N)) ` `     `  `# This code is contributed by Surendra_Gangwar `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG{ ` `   `  `// Function to check if a ` `// number is perfect square ` `static` `bool` `isPerfectSquare(``int` `x) ` `{ ` `    ``int` `s = (``int``) Math.Sqrt(x); ` `    ``return` `(s * s == x); ` `} ` `   `  `// Function to check if a ` `// number is Fibinacci Number ` `static` `bool` `isFibonacci(``int` `N) ` `{ ` `    ``// N is Fibinacci if either ` `    ``// (5*N*N + 4), (5*N*N - 4) or both ` `    ``// is a perferct square ` `    ``return` `isPerfectSquare(5 * N * N + 4) ` `           ``|| isPerfectSquare(5 * N * N - 4); ` `} ` `   `  `// Function to find ` `// the next Non-Fibinacci Number ` `static` `int` `nextNonFibonacci(``int` `N) ` `{ ` `   `  `    ``// Case 1 ` `    ``// If N<=3, then 4 will be ` `    ``// next Non-Fibinacci Number ` `    ``if` `(N <= 3) ` `        ``return` `4; ` `   `  `    ``// Case 2 ` `    ``// If N+1 is Fibinacci, then N+2 ` `    ``// will be next Non-Fibinacci Number ` `    ``if` `(isFibonacci(N + 1)) ` `        ``return` `N + 2; ` `   `  `    ``// If N+1 is Non-Fibinacci, then N+2 ` `    ``// will be next Non-Fibinacci Number ` `    ``else` `        ``return` `N + 1; ` `} ` `   `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `N = 3; ` `    ``Console.Write(nextNonFibonacci(N) ` `         ``+``"\n"``); ` `   `  `    ``N = 5; ` `    ``Console.Write(nextNonFibonacci(N) ` `         ``+``"\n"``); ` `   `  `    ``N = 7; ` `    ``Console.Write(nextNonFibonacci(N) ` `         ``+``"\n"``); ` `} ` `} ` ` `  `// This code is contributed by Princi Singh `

Output:

```4
6
9
```

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.