Construct the Cypher string based on the given conditions | Python3 program for the above approach ; Function to check whether a number is prime or not ; Function to check if a prime number can be expressed as sum of two Prime Numbers ; If the N && ( N - 2 ) is Prime ; Function to check semiPrime ; Loop from 2 to sqrt ( num ) ; Increment the count of prime numbers ; If num is greater than 1 , then add 1 to it ; Return '1' if count is 2 else return '0 ; Function to make the Cypher string ; Resultant string ; Make string for the number N ; Check for semiPrime ; Traverse to make Cypher string ; If index is odd add the current character ; Else current character is changed ; Check for sum of two primes ; Traverse to make Cypher string ; If index is odd then current character is changed ; Else add the current character ; If the resultant string is " " then print - 1 ; Else print the resultant string ; Driver Code ; Given number ; Function call
import math NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT sqt = ( int ) ( math . sqrt ( n ) ) NEW_LINE for i in range ( 2 , sqt ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isPossibleSum ( N ) : NEW_LINE INDENT if ( isPrime ( N ) and isPrime ( N - 2 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def checkSemiprime ( num ) : NEW_LINE INDENT cnt = 0 NEW_LINE i = 2 NEW_LINE while cnt < 2 and i * i <= num : NEW_LINE INDENT while ( num % i == 0 ) : NEW_LINE INDENT num //= i NEW_LINE cnt += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if ( num > 1 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT return cnt == 2 NEW_LINE DEDENT def makeCypherString ( N ) : NEW_LINE INDENT semiPrime = " " NEW_LINE sumOfPrime = " " NEW_LINE st = str ( N ) NEW_LINE if ( checkSemiprime ( N ) ) : NEW_LINE INDENT for i in range ( len ( st ) ) : NEW_LINE INDENT if ( i & 1 ) : NEW_LINE INDENT semiPrime += st [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT semiPrime += chr ( ord ( st [ i ] ) - ord ( '0' ) + 65 ) NEW_LINE DEDENT DEDENT DEDENT if ( isPossibleSum ( N ) ) : NEW_LINE INDENT for i in range ( len ( st ) ) : NEW_LINE INDENT if ( i & 1 ) : NEW_LINE INDENT sumOfPrime += chr ( ord ( st [ i ] ) - ord ( '0' ) + 65 ) NEW_LINE DEDENT else : NEW_LINE INDENT sumOfPrime += st [ i ] NEW_LINE DEDENT DEDENT DEDENT if ( semiPrime + sumOfPrime == " " ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( semiPrime + sumOfPrime ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 1011243 NEW_LINE makeCypherString ( N ) NEW_LINE DEDENT