Check whether the given numbers are Cousin prime or not | Python program to check Cousin prime ; Function to check whether a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Returns true if n1 and n2 are Cousin primes ; Check if they differ by 4 or not ; Check if both are prime number or not ; Get the 2 numbers ; Check the numbers for cousin prime
import math NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if n <= 1 : NEW_LINE INDENT return False NEW_LINE DEDENT if n <= 3 : NEW_LINE INDENT return True NEW_LINE DEDENT if n % 2 == 0 or n % 3 == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 5 , int ( math . sqrt ( n ) + 1 ) , 6 ) : NEW_LINE INDENT if n % i == 0 or n % ( i + 2 ) == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isCousinPrime ( n1 , n2 ) : NEW_LINE INDENT if ( not ( abs ( n1 - n2 ) == 4 ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return ( isPrime ( n1 ) and isPrime ( n2 ) ) NEW_LINE DEDENT DEDENT n1 = 7 NEW_LINE n2 = 11 NEW_LINE if ( isCousinPrime ( n1 , n2 ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT