Nearest Fibonacci Number to N

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 = 20Output: 21Explanation: Nearest Fibonacci number to 20 is 21.
Input: N = 17Output: 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.
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++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to find the Fibonacci// number which is nearest to Nvoid nearestFibonacci(int num){    // Base Case    if (num == 0) {        cout << 0;        return;    }     // Initialize the first & second    // terms of the Fibonacci series    int first = 0, second = 1;     // Store the third term    int third = first + second;     // Iterate until the third term    // is less than or equal to num    while (third <= num) {         // Update the first        first = second;         // Update the second        second = third;         // Update the third        third = first + second;    }     // Store the Fibonacci number    // having smaller difference with N    int ans = (abs(third - num)               >= abs(second - num))                  ? second                  : third;     // Print the result    cout << ans;} // Driver Codeint main(){    int N = 17;    nearestFibonacci(N);     return 0;}
// Java program for the above approachclass GFG{     // Function to find the Fibonacci// number which is nearest to Nstatic void nearestFibonacci(int num){         // Base Case    if (num == 0)    {        System.out.print(0);        return;    }     // Initialize the first & second    // terms of the Fibonacci series    int first = 0, second = 1;     // Store the third term    int third = first + second;     // Iterate until the third term    // is less than or equal to num    while (third <= num)    {                 // Update the first        first = second;         // Update the second        second = third;         // Update the third        third = first + second;    }     // Store the Fibonacci number    // having smaller difference with N    int ans = (Math.abs(third - num) >=               Math.abs(second - num)) ?               second : third;     // Print the result     System.out.print(ans);} // Driver Codepublic static void main (String[] args){    int N = 17;         nearestFibonacci(N);}} // This code is contributed by AnkThon
# Python3 program for the above approach # Function to find the Fibonacci# number which is nearest to Ndef nearestFibonacci(num):         # Base Case    if (num == 0):        print(0)        return     # Initialize the first & second    # terms of the Fibonacci series    first = 0    second = 1     # Store the third term    third = first + second     # Iterate until the third term    # is less than or equal to num    while (third <= num):                 # Update the first        first = second         # Update the second        second = third         # Update the third        third = first + second     # Store the Fibonacci number    # having smaller difference with N    if (abs(third - num) >=        abs(second - num)):        ans =  second    else:        ans = third     # Print the result    print(ans) # Driver Codeif __name__ == '__main__':         N = 17         nearestFibonacci(N) # This code is contributed by SURENDRA_GANGWAR
// C# program for the above approachusing System; class GFG{ // Function to find the Fibonacci// number which is nearest to Nstatic void nearestFibonacci(int num){         // Base Case    if (num == 0)    {        Console.Write(0);        return;    }     // Initialize the first & second    // terms of the Fibonacci series    int first = 0, second = 1;     // Store the third term    int third = first + second;     // Iterate until the third term    // is less than or equal to num    while (third <= num)    {                 // Update the first        first = second;         // Update the second        second = third;         // Update the third        third = first + second;    }     // Store the Fibonacci number    // having smaller difference with N    int ans = (Math.Abs(third - num) >=              Math.Abs(second - num)) ?                       second : third;     // Print the result     Console.Write(ans);} // Driver Codepublic static void Main(string[] args){    int N = 17;         nearestFibonacci(N);}} // This code is contributed by sanjoy_62
<script>// Javascript program for the above approach // Function to find the Fibonacci// number which is nearest to Nfunction nearestFibonacci(num){    // Base Case    if (num == 0) {        document.write(0);        return;    }     // Initialize the first & second    // terms of the Fibonacci series    let first = 0, second = 1;     // Store the third term    let third = first + second;     // Iterate until the third term    // is less than or equal to num    while (third <= num) {         // Update the first        first = second;         // Update the second        second = third;         // Update the third        third = first + second;    }     // Store the Fibonacci number    // having smaller difference with N    let ans = (Math.abs(third - num)               >= Math.abs(second - num))                  ? second                  : third;     // Print the result    document.write(ans);} // Driver Code    let N = 17;    nearestFibonacci(N); // This code is contributed by subhammahato348.</script>
13
 
Time Complexity: O(log N)Auxiliary Space: O(1)
SURENDRA_GANGWAR
ankthon
sanjoy_62
subhammahato348
Fibonacci
Numbers
Mathematical
Mathematical
Fibonacci
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Algorithm to solve Rubik's Cube
Minimum number of jumps to reach end
The Knight's tour problem | Backtracking-1
Modulo Operator (%) in C/C++ with Examples
Modulo 10^9+7 (1000000007)
Program for factorial of a number
Program to find sum of elements in a given array