Non Fibonacci Numbers
Last Updated :
07 Jan, 2024
Given a positive integer n, the task is to print the nth 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
Below is the implementation of the above idea.
C++
#include <bits/stdc++.h>
using namespace std;
int nonFibonacci( int n)
{
int prevPrev = 1, prev = 2, curr = 3;
while (n > 0) {
prevPrev = prev;
prev = curr;
curr = prevPrev + prev;
n = n - (curr - prev - 1);
}
n = n + (curr - prev - 1);
return prev + n;
}
int main()
{
cout << nonFibonacci(5);
return 0;
}
|
C
#include<stdio.h>
int nonFibonacci( int n)
{
int prevPrev=1, prev=2, curr=3;
while (n > 0)
{
prevPrev = prev;
prev = curr;
curr = prevPrev + prev;
n = n - (curr - prev - 1);
}
n = n + (curr - prev - 1);
return prev + n;
}
int main()
{
printf ( "%d" ,nonFibonacci(5));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int nonFibonacci( int n)
{
int prevPrev = 1 , prev = 2 , curr = 3 ;
while (n > 0 ) {
prevPrev = prev;
prev = curr;
curr = prevPrev + prev;
n = n - (curr - prev - 1 );
}
n = n + (curr - prev - 1 );
return prev + n;
}
public static void main(String args[])
{
System.out.println(nonFibonacci( 5 ));
}
}
|
Python
def nonFibonacci(n):
prevPrev = 1
prev = 2
curr = 3
while n > 0 :
prevPrev = prev
prev = curr
curr = prevPrev + prev
n = n - (curr - prev - 1 )
n = n + (curr - prev - 1 )
return prev + n
print (nonFibonacci( 5 ))
|
C#
using System;
class GFG
{
static int nonFibonacci ( int n)
{
int prevPrev = 1, prev = 2, curr = 3;
while (n > 0)
{
prevPrev = prev;
prev = curr;
curr = prevPrev + prev;
n = n - (curr - prev - 1);
}
n = n + (curr - prev - 1);
return prev + n;
}
public static void Main ()
{
Console.WriteLine (nonFibonacci(5));
}
}
|
Javascript
<script>
function nonFibonacci(n)
{
let prevPrev=1, prev=2, curr=3;
while (n > 0)
{
prevPrev = prev;
prev = curr;
curr = prevPrev + prev;
n = n - (curr - prev - 1);
}
n = n + (curr - prev - 1);
return prev + n;
}
document.write(nonFibonacci(5));
</script>
|
PHP
<?php
function nonFibonacci( $n )
{
$prevPrev = 1;
$prev = 2;
$curr = 3;
while ( $n > 0)
{
$prevPrev = $prev ;
$prev = $curr ;
$curr = $prevPrev + $prev ;
$n = $n - ( $curr - $prev - 1);
}
$n = $n + ( $curr - $prev - 1);
return $prev + $n ;
}
echo nonFibonacci(5);
?>
|
Output :
10
Time Complexity : O(n) , Auxiliary Space : O(1)
Now geeks you must be wondering what if we were supposed to print Non-Fibonacci Series in a range, then the code is as follows:
C++
#include <iostream>
using namespace std;
int main()
{
int i = 0, j = 1, k, m, no, b[10];
no = 10;
b[1] = 0;
b[2] = 1;
if (no <= 1) {
cout << "You have enter a wrong range" ;
}
else if (no <= 5 && no > 1) {
cout << "\nThere is not any Non-Fibonacci series "
"that lies between 1 to "
<< no << " term of Fibonacci Series." ;
}
else {
for (m = 2; m < no; m++) {
k = i + j;
i = j;
j = k;
b[m] = k;
}
i = 5;
cout << "\nThe Non-Fibonacci series that lies "
"between 1 to "
<< no << " term of Fibonacci Series is: \n" ;
for ( int ans = 4; ans < b[no - 1]; ans++) {
if (ans != b[i])
cout << ans << " " ;
else
i++;
}
}
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0, j = 1, k, m, no, b[10];
no = 10;
b[1] = 0;
b[2] = 1;
if (no <= 1) {
printf ( "You have enter a wrong range" );
}
else if (no <= 5 && no > 1) {
printf ( "\nThere is not any Non-Fibonacci series "
"that lies between 1 to %d term of "
"Fibonacci Series." ,
no);
}
else {
for (m = 2; m < no; m++) {
k = i + j;
i = j;
j = k;
b[m] = k;
}
i = 5;
printf (
"\nThe Non-Fibonacci series that lies between "
"1 to %d term of Fibonacci Series is: \n" ,
no);
for ( int ans = 4; ans < b[no - 1]; ans++) {
if (ans != b[i])
printf ( "%d " , ans);
else
i++;
}
}
return 0;
}
|
Java
import java.io.*;
class GFG {
int [] holes = { 21 , 3 , 6 };
int i = 0 , j = 1 , k, m, no;
int [] b = new int [ 10 ];
no = 10 ;
b[ 1 ] = 0 ;
b[ 2 ] = 1 ;
if (no <= 1 ) {
System.out.print( "You have enter a wrong range" );
}
else if (no <= 5 && no > 1 ) {
System.out.print( "\n" + "There is not any Non-Fibonacci series that lies between 1 to" + no +
" term of Fibonacci Series." );
}
else {
for (m = 2 ; m < no; m++) {
k = i + j;
i = j;
j = k;
b[m] = k;
}
i = 5 ;
System.out.println( "\n" + "The Non-Fibonacci series that lies between 1 to "
+ no + " term of Fibonacci Series is: " + "\n" );
for ( int ans = 4 ; ans < b[no - 1 ]; ans++) {
if (ans != b[i])
System.out.print(ans + " " );
else
i++;
}
}
}
|
Python3
i = 0
j = 1
b = []
no = 10
b.append( 0 )
b.append( 1 )
if (no < = 1 ):
print ( "You have enter a wrong range..." )
elif (no < = 5 and no > 1 ):
print ( "\nThere is not any Non-Fibonacci series that lies between 1 to " ,
no, " term of Fibonacci Series." )
else :
for m in range ( 2 , no):
k = i + j
i = j
j = k
b.append(k)
i = 5
print ( "\nThe Non-Fibonacci series that lies between 1 to " ,
no, " term of Fibonacci Series is:" )
for ans in range ( 4 , b[no - 1 ]):
if ans ! = b[i]:
print (ans, end = " " )
else :
i = i + 1
|
C#
using System;
class GFG {
public static void Main( string [] args)
{
int [] holes = { 21, 3, 6 };
int i = 0, j = 1, k, m, no = 10;
int [] b = new int [10];
b[1] = 0;
b[2] = 1;
if (no <= 1) {
Console.Write( "You have enter a wrong range" );
}
else if (no <= 5 && no > 1) {
Console.Write(
"\n"
+ "There is not any Non-Fibonacci series that lies between 1 to"
+ no + " term of Fibonacci Series." );
}
else {
for (m = 2; m < no; m++) {
k = i + j;
i = j;
j = k;
b[m] = k;
}
i = 5;
Console.WriteLine(
"\n"
+ "The Non-Fibonacci series that lies between 1 to "
+ no + " term of Fibonacci Series is: " );
for ( int ans = 4; ans < b[no - 1]; ans++) {
if (ans != b[i])
Console.Write(ans + " " );
else
i++;
}
}
}
}
|
Javascript
<script>
let i = 0, j = 1, k, m, no, b = new Array(10);
no = 10;
b[1] = 0;
b[2] = 1;
if (no <= 1) {
console.log( "You have enter a wrong range" );
}
else if (no <= 5 && no > 1) {
document.write( "</br>" , "There is not any Non-Fibonacci series that lies between 1 to " + no + " term of Fibonacci Series." );
}
else {
for (m = 2; m < no; m++) {
k = i + j;
i = j;
j = k;
b[m] = k;
}
i = 5;
document.write( "</br>" , "The Non-Fibonacci series that lies between 1 to " + no + " term of Fibonacci Series is: " , "</br>" );
for (let ans = 4; ans < b[no - 1]; ans++) {
if (ans != b[i])
document.write(ans , " " );
else
i++;
}
}
</script>
|
Output
The Non-Fibonacci series that lies between 1 to 10 term of Fibonacci Series is:
4 6 7 9 10 11 12 14 15 16 17 18 19 20 22 23 24 25 26 27 28 29 30 31 32 33
Time Complexity : O(n) , Auxiliary Space : O(n)
The above problem and solution are contributed by Hemang Sarkar.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...