Find the next Non | Python 3 implementation of the approach ; Function to check if a number is perfect square ; Function to check if a number is Fibinacci Number ; N is Fibinacci if either ( 5 * N * N + 4 ) , ( 5 * N * N - 4 ) or both is a perferct square ; Function to find the next Non - Fibinacci Number ; Case 1 If N <= 3 , then 4 will be next Non - Fibinacci Number ; Case 2 If N + 1 is Fibinacci , then N + 2 will be next Non - Fibinacci Number ; If N + 1 is Non - Fibinacci , then N + 2 will be next Non - Fibinacci Number ; Driver code
from math import sqrt NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT s = sqrt ( x ) NEW_LINE return ( s * s == x ) NEW_LINE DEDENT def isFibonacci ( N ) : NEW_LINE INDENT return isPerfectSquare ( 5 * N * N + 4 ) or isPerfectSquare ( 5 * N * N - 4 ) NEW_LINE DEDENT def nextNonFibonacci ( N ) : NEW_LINE INDENT if ( N <= 3 ) : NEW_LINE INDENT return 4 NEW_LINE DEDENT if ( isFibonacci ( N + 1 ) ) : NEW_LINE INDENT return N + 2 NEW_LINE DEDENT else : NEW_LINE INDENT return N NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE print ( nextNonFibonacci ( N ) ) NEW_LINE N = 4 NEW_LINE print ( nextNonFibonacci ( N ) ) NEW_LINE N = 7 NEW_LINE print ( nextNonFibonacci ( N ) ) NEW_LINE DEDENT