Given a positive integer n, return the nth number that does not belong to the Fibonacci sequence.
The Fibonacci sequence is defined as:
- fib(0) = 0
- fib(1) = 1
- fib(i) = fib(i-1) + fib(i-2), for i > 1
First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, âĶâĶ.. and so on.
Examples:Â
Input: n = 5
Output: 10
Explanation: The first 5 non-Fibonacci numbers are 4, 6, 7, 9 and 10. Hence, the answer is 10.Input: n = 15
Output: 22
Explanation: 22 is the 15th non-Fibonacci number. Hence, the answer is 22.
Table of Content
[Naive Approach] Brute Force Approach - O(n * log n) Time and O(1) Space
- Initialize count = 0 and start checking integers from num = 1.
- For each integer, determine whether it belongs to the Fibonacci sequence by generating Fibonacci numbers until the current number is reached or exceeded.
- If the current integer is not a Fibonacci number, increment count.
- If count becomes equal to n, return the current integer as the nth non-Fibonacci number.
#include <iostream>
using namespace std;
// Returns true if x is a Fibonacci number.
bool isFibonacci(int x)
{
if (x == 1 || x == 2)
return true;
int a = 1, b = 2;
while (b < x)
{
int c = a + b;
a = b;
b = c;
}
return b == x;
}
// Returns the nth non-Fibonacci number.
int nonFibonacci(int n)
{
int count = 0;
int num = 1;
while (true)
{
if (!isFibonacci(num))
{
count++;
if (count == n)
return num;
}
num++;
}
}
int main()
{
int n = 5;
cout << nonFibonacci(n) << endl;
return 0;
}
class GFG {
// Returns true if x is a Fibonacci number.
static boolean isFibonacci(int x)
{
if (x == 1 || x == 2)
return true;
int a = 1, b = 2;
while (b < x) {
int c = a + b;
a = b;
b = c;
}
return b == x;
}
// Returns the nth non-Fibonacci number.
static int nonFibonacci(int n)
{
int count = 0;
int num = 1;
while (true) {
if (!isFibonacci(num)) {
count++;
if (count == n)
return num;
}
num++;
}
}
public static void main(String[] args)
{
int n = 5;
System.out.println(nonFibonacci(n));
}
}
# Returns True if x is a Fibonacci number.
def isFibonacci(x):
if x == 1 or x == 2:
return True
a, b = 1, 2
while b < x:
a, b = b, a + b
return b == x
# Returns the nth non-Fibonacci number.
def nonFibonacci(n):
count = 0
num = 1
while True:
if not isFibonacci(num):
count += 1
if count == n:
return num
num += 1
# Driver code
if __name__ == "__main__":
n = 5
print(nonFibonacci(n))
using System;
class GFG {
// Returns true if x is a Fibonacci number.
static bool IsFibonacci(int x)
{
if (x == 1 || x == 2)
return true;
int a = 1, b = 2;
while (b < x) {
int c = a + b;
a = b;
b = c;
}
return b == x;
}
// Returns the nth non-Fibonacci number.
static int nonFibonacci(int n)
{
int count = 0;
int num = 1;
while (true) {
if (!IsFibonacci(num)) {
count++;
if (count == n)
return num;
}
num++;
}
}
static void Main()
{
int n = 5;
Console.WriteLine(nonFibonacci(n));
}
}
// Returns true if x is a Fibonacci number.
function isFibonacci(x)
{
if (x === 1 || x === 2)
return true;
let a = 1, b = 2;
while (b < x) {
let c = a + b;
a = b;
b = c;
}
return b === x;
}
// Returns the nth non-Fibonacci number.
function nonFibonacci(n)
{
let count = 0;
let num = 1;
while (true) {
if (!isFibonacci(num)) {
count++;
if (count === n)
return num;
}
num++;
}
}
// Driver code
let n = 5;
console.log(nonFibonacci(n));
Output
10
[Expected Approach] Using Gaps Between Consecutive Fibonacci Numbers - O(n) Time and O(1) Space
The idea is to use the fact that between every pair of consecutive Fibonacci numbers, all the numbers in between are non-Fibonacci numbers. Instead of checking each integer individually, we skip entire gaps between consecutive Fibonacci numbers until we find the gap containing the nth non-Fibonacci number.
- Initialize the first three Fibonacci numbers as 1, 2, and 3.
- Generate the next Fibonacci number and compute the gap between consecutive Fibonacci numbers.
- If the gap is smaller than n, subtract the gap from n and continue.
- Repeat until the required number lies in the current gap.
- Restore the last gap and return previous Fibonacci number + n as the answer.
#include <iostream>
using namespace std;
// Returns the nth non-Fibonacci number.
int nonFibonacci(int n)
{
// Initialize the first three Fibonacci numbers.
int prevPrev = 1, prev = 2, curr = 3;
// Keep generating Fibonacci numbers until
// the required non-Fibonacci number lies
// in the current gap.
while (n > 0)
{
// Generate the next Fibonacci number.
prevPrev = prev;
prev = curr;
curr = prevPrev + prev;
// Number of non-Fibonacci numbers between
// the current and previous Fibonacci numbers.
int gap = curr - prev - 1;
// Skip the entire gap.
n -= gap;
}
// Undo the last subtraction to get the
// position of the required number within
// the current gap.
n += (curr - prev - 1);
// The answer is the nth number after
// the previous Fibonacci number.
return prev + n;
}
int main()
{
int n = 5;
cout << nonFibonacci(n) << endl;
return 0;
}
class GFG {
// Returns the nth non-Fibonacci number.
static int nonFibonacci(int n)
{
// Initialize the first three Fibonacci numbers.
int prevPrev = 1, prev = 2, curr = 3;
// Keep generating Fibonacci numbers until
// the required non-Fibonacci number lies
// in the current gap.
while (n > 0) {
// Generate the next Fibonacci number.
prevPrev = prev;
prev = curr;
curr = prevPrev + prev;
// Number of non-Fibonacci numbers between
// the current and previous Fibonacci numbers.
int gap = curr - prev - 1;
// Skip the entire gap.
n -= gap;
}
// Undo the last subtraction to get the
// position of the required number within
// the current gap.
n += (curr - prev - 1);
// The answer is the nth number after
// the previous Fibonacci number.
return prev + n;
}
public static void main(String[] args)
{
int n = 5;
System.out.println(nonFibonacci(n));
}
}
def nonFibonacci(n):
# Initialize the first three Fibonacci numbers.
prevPrev, prev, curr = 1, 2, 3
# Keep generating Fibonacci numbers until
# the required non-Fibonacci number lies
# in the current gap.
while n > 0:
# Generate the next Fibonacci number.
prevPrev, prev = prev, curr
curr = prevPrev + prev
# Number of non-Fibonacci numbers between
# the current and previous Fibonacci numbers.
gap = curr - prev - 1
# Skip the entire gap.
n -= gap
# Undo the last subtraction to get the
# position of the required number within
# the current gap.
n += (curr - prev - 1)
# The answer is the nth number after
# the previous Fibonacci number.
return prev + n
# Driver code
if __name__ == "__main__":
n = 5
print(nonFibonacci(n))
using System;
class GFG {
// Returns the nth non-Fibonacci number.
static int nonFibonacci(int n)
{
// Initialize the first three Fibonacci numbers.
int prevPrev = 1, prev = 2, curr = 3;
// Keep generating Fibonacci numbers until
// the required non-Fibonacci number lies
// in the current gap.
while (n > 0) {
// Generate the next Fibonacci number.
prevPrev = prev;
prev = curr;
curr = prevPrev + prev;
// Number of non-Fibonacci numbers between
// the current and previous Fibonacci numbers.
int gap = curr - prev - 1;
// Skip the entire gap.
n -= gap;
}
// Undo the last subtraction to get the
// position of the required number within
// the current gap.
n += (curr - prev - 1);
// The answer is the nth number after
// the previous Fibonacci number.
return prev + n;
}
static void Main()
{
int n = 5;
Console.WriteLine(nonFibonacci(n));
}
}
// Returns the nth non-Fibonacci number.
function nonFibonacci(n)
{
// Initialize the first three Fibonacci numbers.
let prevPrev = 1, prev = 2, curr = 3;
// Keep generating Fibonacci numbers until
// the required non-Fibonacci number lies
// in the current gap.
while (n > 0) {
// Generate the next Fibonacci number.
prevPrev = prev;
prev = curr;
curr = prevPrev + prev;
// Number of non-Fibonacci numbers between
// the current and previous Fibonacci numbers.
let gap = curr - prev - 1;
// Skip the entire gap.
n -= gap;
}
// Undo the last subtraction to get the
// position of the required number within
// the current gap.
n += (curr - prev - 1);
// The answer is the nth number after
// the previous Fibonacci number.
return prev + n;
}
// Driver code
let n = 5;
console.log(nonFibonacci(n));
Output
10