Fibonacci problem (Value of Fib(N)*Fib(N) – Fib(N-1) * Fib(N+1))

Given a positive integer N, the task is to find Fib(N)2 – (Fib(N-1) * Fib(N+1)) where Fib(N) returns the Nth Fibonacci number.

Examples:

Input: N = 3
Output: 1
Fib(3) * Fib(3) – Fib(2) * Fib(4) = 4 – 3 = 1

Input: N = 2
Output: -1
Fib(2) * Fib(2) – Fib(1) * Fib(3) = 1 – 2 = -1

Approach: This question can be approached by first trying out a few test cases. Let’s take a few examples:

For N = 1 : Fib(1) * Fib(1) – Fib(0) * Fib(2) = 1 – 0 = 1
For N = 2 : Fib(2) * Fib(2) – Fib(1) * Fib(3) = 1 – 2 = -1
For N = 3 : Fib(3) * Fib(3) – Fib(2) * Fib(4) = 4 – 3 = 1
For N = 4 : Fib(4) * Fib(4) – Fib(3) * Fib(5) = 9 – 10 = -1
We observe here that when N is even then the answer will be -1 and when the N is odd then the answer will be 1.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
using namespace std;
  
int getResult(int n)
{
    if (n & 1)
        return 1;
    return -1;
}
  
// Driver code
int main()
{
    int n = 3;
    cout << getResult(n);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

//Java implementation of the approach
  
import java.io.*;
  
class GFG {
      
static int getResult(int n)
{
    if ((n & 1)>0)
        return 1;
    return -1;
}
  
// Driver code
    public static void main (String[] args) {
      
    int n = 3;
    System.out.println(getResult(n));
    }
//This code is contributed by @Tushil.    
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of 
# the approach
def getResult(n):
  
    if n & 1:
        return 1
    return -1
  
# Driver code
n = 3
print(getResult(n))
  
# This code is contributed 
# by Shrikant13

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

//C# implementation of the approach
  
using System;
  
class GFG {
      
static int getResult(int n)
{
    if ((n & 1)>0)
        return 1;
    return -1;
}
  
// Driver code
    public static void Main () {
      
    int n = 3;
    Console.WriteLine(getResult(n));
    }
//This code is contributed by anuj_67..
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach 
  
function getResult($n
    if ($n & 1) 
        return 1; 
    return -1; 
  
// Driver code 
$n = 3; 
echo getResult($n); 
  
// This code is contributed by akt_mit
?>

chevron_right


Output:

1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.