Find two Fibonacci numbers whose sum can be represented as N | C # program to find two Fibonacci numbers whose sum can be represented as N ; Function to create hash table to check Fibonacci numbers ; Storing the first two numbers in the hash ; Finding Fibonacci numbers up to N and storing them in the hash ; Function to find the Fibonacci pair with the given sum ; creating a set containing all fibonacci numbers ; Traversing all numbers to find first pair ; If both i and ( N - i ) are Fibonacci ; Printing the pair because i + ( N - i ) = N ; If no fibonacci pair is found whose sum is equal to n ; Driven code
using System ; using System . Collections . Generic ; class GFG { static void createHash ( HashSet < int > hash , int maxElement ) { int prev = 0 , curr = 1 ; hash . Add ( prev ) ; hash . Add ( curr ) ; while ( curr < maxElement ) { int temp = curr + prev ; hash . Add ( temp ) ; prev = curr ; curr = temp ; } } static void findFibonacciPair ( int n ) { HashSet < int > hash = new HashSet < int > ( ) ; createHash ( hash , n ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hash . Contains ( i ) && hash . Contains ( n - i ) ) { Console . Write ( i + " , ▁ " + ( n - i ) + " STRNEWLINE " ) ; return ; } } Console . Write ( " - 1 STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int N = 90 ; findFibonacciPair ( N ) ; } }