Given a range, count Fibonacci numbers in given range. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, ..
Example :
Input: low = 10, high = 100
Output: 5
There are five Fibonacci numbers in given
range, the numbers are 13, 21, 34, 55 and 89.
Input: low = 10, high = 20
Output: 1
There is only one Fibonacci Number, 13.
Input: low = 0, high = 1
Output: 3
Fibonacci numbers are 0, 1 and 1
We strongly recommend you to minimize your browser and try this yourself first.
A Brute Force Solution is to one by one find all Fibonacci Numbers and count all Fibonacci numbers in given range
An Efficient Solution is to use previous Fibonacci Number to generate next using simple Fibonacci formula that fn = fn-1 + fn-2.
C++
#include <bits/stdc++.h>
using namespace std;
int countFibs(int low, int high)
{
int f1 = 0, f2 = 1, f3 = 1;
int result = 0;
while (f1 <= high)
{
if (f1 >= low)
result++;
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
return result;
}
int main()
{
int low = 10, high = 100;
cout << "Count of Fibonacci Numbers is "
<< countFibs(low, high);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int countFibs(int low, int high)
{
int f1 = 0, f2 = 1, f3 = 1;
int result = 0;
while (f1 <= high) {
if (f1 >= low)
result++;
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
return result;
}
public static void main(String[] args)
{
int low = 10, high = 100;
System.out.println("Count of Fibonacci Numbers is "
+ countFibs(low, high));
}
}
|
Python3
def countFibs(low, high):
f1, f2, f3 = 0, 1, 1
result = 0
while (f1 <= high):
if (f1 >= low):
result += 1
f1 = f2
f2 = f3
f3 = f1 + f2
return result
low, high = 10, 100
print("Count of Fibonacci Numbers is",
countFibs(low, high))
|
C#
using System;
public class GFG
{
static int countFibs(int low,
int high)
{
int f1 = 0, f2 = 1, f3 = 1;
int result = 0;
while (f1 <= high)
{
if (f1 >= low)
result++;
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
return result;
}
public static void Main(String []args)
{
int low = 10, high = 100;
Console.WriteLine("Count of Fibonacci Numbers is "
+ countFibs(low, high));
}
}
|
PHP
<?php
function countFibs($low, $high)
{
$f1 = 0; $f2 = 1; $f3 = 1;
$result = 0;
while ($f1 <= $high)
{
if ($f1 >= $low)
$result++;
$f1 = $f2;
$f2 = $f3;
$f3 = $f1 + $f2;
}
return $result;
}
$low = 10; $high = 100;
echo "Count of Fibonacci Numbers is ",
countFibs($low, $high);
?>
|
Javascript
<script>
function countFibs(low, high)
{
let f1 = 0, f2 = 1, f3 = 1;
let result = 0;
while (f1 <= high)
{
if (f1 >= low)
result++;
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
return result;
}
let low = 10, high = 100;
document.write("Count of Fibonacci Numbers is "
+ countFibs(low, high));
</script>
|
Output :
Count of Fibonacci Numbers is 5
Time Complexity Analysis:
Consider the that Fibonacci Numbers can be written as below
![Rendered by QuickLaTeX.com fib(n)=\left [ \frac {1}{\sqrt{5}}\left ( \frac {1+\sqrt{5}}{2} \right )^n \right ]\sim c*1.62^n](https://web.archive.org/web/20240131163229im_/https://www.geeksforgeeks.org/wp-content/ql-cache/quicklatex.com-0da7b0fd9c9398e2b691efa30b4e2d14_l3.png)

So the value of Fibonacci numbers grow exponentially. It means that the while loop grows exponentially till it reaches ‘high’. So the loop runs O(Log (high)) times.
One solution could be directly use above formula to find count of Fibonacci Numbers, but that is not practically feasible (See this for details).
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 geeks!
Last Updated :
28 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...