Length of longest Fibonacci subarray | Python3 program for the above approach ; A utility function that returns true if x is perfect square ; Returns true if n is a Fibonacci Number , else false ; Here n is fibonacci if one of 5 * n * n + 4 or 5 * n * n - 4 or both is a perfect square ; Function to find the length of the largest sub - array of an array every element of whose is a Fibonacci number ; Traverse the array arr ; Check if arr [ i ] is a Fibonacci number ; stores the maximum length of the Fibonacci number subarray ; Finally , return the maximum length ; Driver code ; Given Input ; Function Call
import math NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT s = int ( math . sqrt ( x ) ) NEW_LINE if s * s == x : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def isFibonacci ( n ) : NEW_LINE INDENT return ( isPerfectSquare ( 5 * n * n + 4 ) or isPerfectSquare ( 5 * n * n - 4 ) ) NEW_LINE DEDENT def contiguousFibonacciNumber ( arr , n ) : NEW_LINE INDENT current_length = 0 NEW_LINE max_length = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if isFibonacci ( arr [ i ] ) : NEW_LINE INDENT current_length += 1 NEW_LINE DEDENT else : NEW_LINE INDENT current_length = 0 NEW_LINE DEDENT max_length = max ( max_length , current_length ) NEW_LINE DEDENT return max_length NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 11 , 8 , 21 , 5 , 3 , 28 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( contiguousFibonacciNumber ( arr , n ) ) NEW_LINE DEDENT