Find the next Non-Fibonacci number
Last Updated :
26 May, 2022
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
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPerfectSquare( int x)
{
int s = sqrt (x);
return (s * s == x);
}
bool isFibonacci( int N)
{
return isPerfectSquare(5 * N * N + 4)
|| isPerfectSquare(5 * N * N - 4);
}
int nextNonFibonacci( int N)
{
if (N <= 3)
return 4;
if (isFibonacci(N + 1))
return N + 2;
else
return N + 1;
}
int main()
{
int N = 3;
cout << nextNonFibonacci(N)
<< endl;
N = 5;
cout << nextNonFibonacci(N)
<< endl;
N = 7;
cout << nextNonFibonacci(N)
<< endl;
}
|
C
#include <stdio.h>
#include<stdbool.h>
#include<math.h>
bool isPerfectSquare( int x)
{
int s = sqrt (x);
return (s * s == x);
}
bool isFibonacci( int N)
{
return isPerfectSquare(5 * N * N + 4)
|| isPerfectSquare(5 * N * N - 4);
}
int nextNonFibonacci( int N)
{
if (N <= 3)
return 4;
if (isFibonacci(N + 1))
return N + 2;
else
return N + 1;
}
int main()
{
int N = 3;
printf ( "%d\n" ,nextNonFibonacci(N));
N = 5;
printf ( "%d\n" ,nextNonFibonacci(N));
N = 7;
printf ( "%d" ,nextNonFibonacci(N));
}
|
Java
import java.util.*;
class GFG{
static boolean isPerfectSquare( int x)
{
int s = ( int ) Math.sqrt(x);
return (s * s == x);
}
static boolean isFibonacci( int N)
{
return isPerfectSquare( 5 * N * N + 4 )
|| isPerfectSquare( 5 * N * N - 4 );
}
static int nextNonFibonacci( int N)
{
if (N <= 3 )
return 4 ;
if (isFibonacci(N + 1 ))
return N + 2 ;
else
return N + 1 ;
}
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" );
}
}
|
Python 3
from math import sqrt
def isPerfectSquare(x):
s = sqrt(x)
return (s * s = = x)
def isFibonacci(N):
return isPerfectSquare( 5 * N * N + 4 ) or \
isPerfectSquare( 5 * N * N - 4 )
def nextNonFibonacci(N):
if (N < = 3 ):
return 4
if (isFibonacci(N + 1 )):
return N + 2
else :
return N
if __name__ = = '__main__' :
N = 3
print (nextNonFibonacci(N))
N = 4
print (nextNonFibonacci(N))
N = 7
print (nextNonFibonacci(N))
|
C#
using System;
class GFG{
static bool isPerfectSquare( int x)
{
int s = ( int ) Math.Sqrt(x);
return (s * s == x);
}
static bool isFibonacci( int N)
{
return isPerfectSquare(5 * N * N + 4)
|| isPerfectSquare(5 * N * N - 4);
}
static int nextNonFibonacci( int N)
{
if (N <= 3)
return 4;
if (isFibonacci(N + 1))
return N + 2;
else
return N + 1;
}
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" );
}
}
|
Javascript
<script>
function isPerfectSquare(x)
{
var s = parseInt(Math.sqrt(x));
return (s * s == x);
}
function isFibonacci(N)
{
return isPerfectSquare(5 * N * N + 4)
|| isPerfectSquare(5 * N * N - 4);
}
function nextNonFibonacci(N)
{
if (N <= 3)
return 4;
if (isFibonacci(N + 1))
return N + 2;
else
return N + 1;
}
var N = 3;
document.write(nextNonFibonacci(N)+ "<br>" );
N = 5;
document.write(nextNonFibonacci(N)+ "<br>" );
N = 7;
document.write(nextNonFibonacci(N)+ "<br>" );
</script>
|
Time Complexity: O(n1/2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...