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
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:
- If N <= 3, then the next Non-Fibonacci number will be 4
- 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
- If (N + 1) is a fibonacci number then (N + 2) will be the next Non-Fibonacci number.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;// Function to check if a// number is perfect squarebool isPerfectSquare(int x){ int s = sqrt(x); return (s * s == x);}// Function to check if a// number is Fibonacci Numberbool isFibonacci(int N){ // N is Fibonacci if either // (5*N*N + 4), (5*N*N - 4) or both // is a perfect square return isPerfectSquare(5 * N * N + 4) || isPerfectSquare(5 * N * N - 4);}// Function to find// the next Non-Fibonacci Numberint nextNonFibonacci(int N){ // Case 1 // If N<=3, then 4 will be // next Non-Fibonacci Number if (N <= 3) return 4; // Case 2 // If N+1 is Fibonacci, then N+2 // will be next Non-Fibonacci Number if (isFibonacci(N + 1)) return N + 2; // If N+1 is Non-Fibonacci, then N+2 // will be next Non-Fibonacci Number else return N + 1;}// Driver codeint main(){ int N = 3; cout << nextNonFibonacci(N) << endl; N = 5; cout << nextNonFibonacci(N) << endl; N = 7; cout << nextNonFibonacci(N) << endl;} |
C
// C implementation of the approach#include <stdio.h>#include<stdbool.h>#include<math.h>// Function to check if a// number is perfect squarebool isPerfectSquare(int x){ int s = sqrt(x); return (s * s == x);}// Function to check if a// number is Fibonacci Numberbool isFibonacci(int N){ // N is Fibonacci if either // (5*N*N + 4), (5*N*N - 4) or both // is a perfect square return isPerfectSquare(5 * N * N + 4) || isPerfectSquare(5 * N * N - 4);}// Function to find// the next Non-Fibonacci Numberint nextNonFibonacci(int N){ // Case 1 // If N<=3, then 4 will be // next Non-Fibonacci Number if (N <= 3) return 4; // Case 2 // If N+1 is Fibonacci, then N+2 // will be next Non-Fibonacci Number if (isFibonacci(N + 1)) return N + 2; // If N+1 is Non-Fibonacci, then N+2 // will be next Non-Fibonacci Number else return N + 1;}// Driver codeint main(){ int N = 3; printf("%d\n",nextNonFibonacci(N)); N = 5; printf("%d\n",nextNonFibonacci(N)); N = 7; printf("%d",nextNonFibonacci(N)); }// This code is contributed by allwink45. |
Java
// Java implementation of the approachimport java.util.*;class GFG{ // Function to check if a// number is perfect squarestatic boolean isPerfectSquare(int x){ int s = (int) Math.sqrt(x); return (s * s == x);} // Function to check if a// number is Fibonacci Numberstatic boolean isFibonacci(int N){ // N is Fibonacci if either // (5*N*N + 4), (5*N*N - 4) or both // is a perfect square return isPerfectSquare(5 * N * N + 4) || isPerfectSquare(5 * N * N - 4);} // Function to find// the next Non-Fibonacci Numberstatic int nextNonFibonacci(int N){ // Case 1 // If N<=3, then 4 will be // next Non-Fibonacci Number if (N <= 3) return 4; // Case 2 // If N+1 is Fibonacci, then N+2 // will be next Non-Fibonacci Number if (isFibonacci(N + 1)) return N + 2; // If N+1 is Non-Fibonacci, then N+2 // will be next Non-Fibonacci Number else return N + 1;} // Driver codepublic 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 approachfrom math import sqrt# Function to check if a# number is perfect squaredef isPerfectSquare(x): s = sqrt(x) return (s * s == x)# Function to check if a# number is Fibonacci Numberdef isFibonacci(N): # N is Fibonacci if either # (5*N*N + 4), (5*N*N - 4) or both # is a perfect square return isPerfectSquare(5 * N * N + 4) or \ isPerfectSquare(5 * N * N - 4)# Function to find# the next Non-Fibonacci Numberdef nextNonFibonacci(N): # Case 1 # If N<=3, then 4 will be # next Non-Fibonacci Number if (N <= 3): return 4 # Case 2 # If N+1 is Fibonacci, then N+2 # will be next Non-Fibonacci Number if (isFibonacci(N + 1)): return N + 2 # If N+1 is Non-Fibonacci, then N+2 # will be next Non-Fibonacci Number else: return N # Driver codeif __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 approachusing System;class GFG{ // Function to check if a// number is perfect squarestatic bool isPerfectSquare(int x){ int s = (int) Math.Sqrt(x); return (s * s == x);} // Function to check if a// number is Fibonacci Numberstatic bool isFibonacci(int N){ // N is Fibonacci if either // (5*N*N + 4), (5*N*N - 4) or both // is a perfect square return isPerfectSquare(5 * N * N + 4) || isPerfectSquare(5 * N * N - 4);} // Function to find// the next Non-Fibonacci Numberstatic int nextNonFibonacci(int N){ // Case 1 // If N<=3, then 4 will be // next Non-Fibonacci Number if (N <= 3) return 4; // Case 2 // If N+1 is Fibonacci, then N+2 // will be next Non-Fibonacci Number if (isFibonacci(N + 1)) return N + 2; // If N+1 is Non-Fibonacci, then N+2 // will be next Non-Fibonacci Number else return N + 1;} // Driver codepublic 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 |
Javascript
<script>// Javascript implementation of the approach// Function to check if a// number is perfect squarefunction isPerfectSquare(x){ var s = parseInt(Math.sqrt(x)); return (s * s == x);}// Function to check if a// number is Fibonacci Numberfunction isFibonacci(N){ // N is Fibonacci if either // (5*N*N + 4), (5*N*N - 4) or both // is a perfect square return isPerfectSquare(5 * N * N + 4) || isPerfectSquare(5 * N * N - 4);}// Function to find// the next Non-Fibonacci Numberfunction nextNonFibonacci(N){ // Case 1 // If N<=3, then 4 will be // next Non-Fibonacci Number if (N <= 3) return 4; // Case 2 // If N+1 is Fibonacci, then N+2 // will be next Non-Fibonacci Number if (isFibonacci(N + 1)) return N + 2; // If N+1 is Non-Fibonacci, then N+2 // will be next Non-Fibonacci Number else return N + 1;}// Driver codevar N = 3;document.write(nextNonFibonacci(N)+"<br>");N = 5;document.write(nextNonFibonacci(N)+"<br>");N = 7;document.write(nextNonFibonacci(N)+"<br>");// This code is contributed by rutvik_56.</script> |
Output:
4 6 9
Time Complexity: O(n1/2)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



