Given a number N, the task is to evaluate below expression. Expected time complexity is O(1).

f(n-1)*f(n+1) - f(n)*f(n)

Where f(n) is the n-th Fibonacci number with n >= 1. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, ………..i.e. (considering 0 as 0th Fibonacci number)

**Examples :**

Input : n = 5 Output : -1 f(5-1=4) = 3 f(5+1=6) = 8 f(5)*f(5)= 5*5 = 25 f(4)*f(6)- f(5)*f(5)= 24-25= -1

Although the task is simple i.e. find n-1th, nth and (n+1)-th Fibonacci numbers. Evaluate the expression and display the result. But this can be done in O(1) time using Cassini’s Identity which states that:

f(n-1)*f(n+1) - f(n*n) = (-1)^n

So, we don’t need to calculate any Fibonacci term,the only thing is to check whether n is even or odd.

**How does above formula work?**

The formula is based on matrix representation of Fibonacci numbers.

## C/C++

`// C++ implementation to demonstrate working ` `// of Cassini’s Identity ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns (-1)^n ` `int` `cassini(` `int` `n) ` `{ ` ` ` `return` `(n & 1) ? -1 : 1; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `cout << cassini(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to demonstrate working ` `// of Cassini’s Identity ` ` ` `class` `Gfg ` `{ ` ` ` `// Returns (-1)^n ` ` ` `static` `int` `cassini(` `int` `n) ` ` ` `{ ` ` ` `return` `(n & ` `1` `) != ` `0` `? -` `1` `: ` `1` `; ` ` ` `} ` ` ` ` ` `// Driver method ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `System.out.println(cassini(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python implementation ` `# to demonstrate working ` `# of Cassini’s Identity ` ` ` `# Returns (-1)^n ` `def` `cassini(n): ` ` ` ` ` `return` `-` `1` `if` `(n & ` `1` `) ` `else` `1` ` ` `# Driver program ` ` ` `n ` `=` `5` `print` `(cassini(n)) ` ` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to demonstrate ` `// working of Cassini’s Identity ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Returns (-1) ^ n ` ` ` `static` `int` `cassini(` `int` `n) ` ` ` `{ ` ` ` `return` `(n & 1) != 0 ? -1 : 1; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 5; ` ` ` `Console.Write(cassini(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Nitin Mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation to ` `// demonstrate working of ` `// Cassini’s Identity ` ` ` `// Returns (-1)^n ` `function` `cassini(` `$n` `) ` `{ ` ` ` `return` `(` `$n` `& 1) ? -1 : 1; ` `} ` ` ` `// Driver Code ` `$n` `= 5; ` `echo` `(cassini(` `$n` `)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

-1

**Reference : **

https://en.wikipedia.org/wiki/Cassini_and_Catalan_identities

This article is contributed by **Sahil Chhabra**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Brahmagupta Fibonacci Identity
- Euler's Four Square Identity
- Program for Identity Matrix
- Significance of Pascal’s Identity
- Check if it is possible to reach a number by making jumps of two given length
- Find the final X and Y when they are Altering under given condition
- Program to check if a number is divisible by any of its digits
- Print all multiplicative primes <= N
- Biggest Reuleaux Triangle inscirbed within a square inscribed in a semicircle
- Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle
- Largest number less than or equal to N/2 which is coprime to N
- Find the area of largest circle inscribed in ellipse
- Print prime numbers with prime sum of digits in an array
- Find K Closest Points to the Origin