Pair of fibonacci numbers with a given sum and minimum absolute difference | Python 3 program to find the pair of Fibonacci numbers with a given sum and minimum absolute difference ; Hash to store all the Fibonacci numbers ; Function to generate fibonacci Series and create hash table to check Fibonacci numbers ; Adding the first two Fibonacci numbers into the Hash set ; Computing the remaining Fibonacci numbers based on the previous two numbers ; Function to find the pair of Fibonacci numbers with the given sum and minimum absolute difference ; Start from N / 2 such that the difference between i and N - i will be minimum ; If both ' i ' and ' sum ▁ - ▁ i ' are fibonacci numbers then print them and break the loop ; If there is no Fibonacci pair possible ; Driver code ; Generate the Fibonacci numbers ; Find the Fibonacci pair
MAX = 10005 NEW_LINE fib = set ( ) NEW_LINE def fibonacci ( ) : NEW_LINE INDENT global fib NEW_LINE global MAX NEW_LINE prev = 0 NEW_LINE curr = 1 NEW_LINE l = 2 NEW_LINE fib . add ( prev ) NEW_LINE fib . add ( curr ) NEW_LINE while ( l <= MAX ) : NEW_LINE INDENT temp = curr + prev NEW_LINE fib . add ( temp ) NEW_LINE prev = curr NEW_LINE curr = temp NEW_LINE l += 1 NEW_LINE DEDENT DEDENT def findFibonacci ( N ) : NEW_LINE INDENT global fib NEW_LINE i = N // 2 NEW_LINE while ( i > 1 ) : NEW_LINE INDENT if ( i in fib and ( N - i ) in fib ) : NEW_LINE INDENT print ( i , ( N - i ) ) NEW_LINE return NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT print ( " - 1" ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT fibonacci ( ) NEW_LINE sum = 199 NEW_LINE findFibonacci ( sum ) NEW_LINE DEDENT