Nearest Fibonacci Number to N
Last Updated :
13 Apr, 2021
Given a positive integer N, the task is to find the nearest Fibonacci number to the given integer N. If there are two Fibonacci Numbers having same difference from N, then print the smaller value.
Examples:
Input: N = 20
Output: 21
Explanation: Nearest Fibonacci number to 20 is 21.
Input: N = 17
Output: 13
Approach: Follow the steps below to solve the problem:
- If N is equal to 0, then print 0 as the result.
- Initialize a variable, say ans, to store the Fibonacci Number nearest to N.
- Initialize two variables, say First as 0, and Second as 1, to store the first and second terms of the Fibonacci Series.
- Store the sum of First and Second in a variable, say Third.
- Iterate until the value of Third is at most N and perform the following steps:
- Update the value of First to Second and Second to Third.
- Store the sum of First and Second in the variable Third.
- If the absolute difference of Second and N is at most the value of Third and N, then update the value of ans as Second.
- Otherwise, update the value of ans as Third.
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void nearestFibonacci( int num)
{
if (num == 0) {
cout << 0;
return ;
}
int first = 0, second = 1;
int third = first + second;
while (third <= num) {
first = second;
second = third;
third = first + second;
}
int ans = ( abs (third - num)
>= abs (second - num))
? second
: third;
cout << ans;
}
int main()
{
int N = 17;
nearestFibonacci(N);
return 0;
}
|
Java
class GFG{
static void nearestFibonacci( int num)
{
if (num == 0 )
{
System.out.print( 0 );
return ;
}
int first = 0 , second = 1 ;
int third = first + second;
while (third <= num)
{
first = second;
second = third;
third = first + second;
}
int ans = (Math.abs(third - num) >=
Math.abs(second - num)) ?
second : third;
System.out.print(ans);
}
public static void main (String[] args)
{
int N = 17 ;
nearestFibonacci(N);
}
}
|
Python3
def nearestFibonacci(num):
if (num = = 0 ):
print ( 0 )
return
first = 0
second = 1
third = first + second
while (third < = num):
first = second
second = third
third = first + second
if ( abs (third - num) > =
abs (second - num)):
ans = second
else :
ans = third
print (ans)
if __name__ = = '__main__' :
N = 17
nearestFibonacci(N)
|
C#
using System;
class GFG{
static void nearestFibonacci( int num)
{
if (num == 0)
{
Console.Write(0);
return ;
}
int first = 0, second = 1;
int third = first + second;
while (third <= num)
{
first = second;
second = third;
third = first + second;
}
int ans = (Math.Abs(third - num) >=
Math.Abs(second - num)) ?
second : third;
Console.Write(ans);
}
public static void Main( string [] args)
{
int N = 17;
nearestFibonacci(N);
}
}
|
Javascript
<script>
function nearestFibonacci(num)
{
if (num == 0) {
document.write(0);
return ;
}
let first = 0, second = 1;
let third = first + second;
while (third <= num) {
first = second;
second = third;
third = first + second;
}
let ans = (Math.abs(third - num)
>= Math.abs(second - num))
? second
: third;
document.write(ans);
}
let N = 17;
nearestFibonacci(N);
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...