Construct the Cypher string based on the given conditions | 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 Math . 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 ; Given Number ; Function Call
function isPrime ( n ) { if ( n <= 1 ) return false ; for ( let i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } function isPossibleSum ( N ) { if ( isPrime ( N ) && isPrime ( N - 2 ) ) { return true ; } else { return false ; } } function checkSemiprime ( num ) { let cnt = 0 ; for ( let i = 2 ; cnt < 2 && i * i <= num ; ++ i ) { while ( num % i == 0 ) { num = Math . floor ( num / i ) ; ++ cnt ; } } if ( num > 1 ) { ++ cnt ; } return cnt == 2 ; } function makeCypherString ( N ) { let semiPrime = " " ; let sumOfPrime = " " ; let str = ( N ) . toString ( ) ; if ( checkSemiprime ( N ) ) { for ( let i = 0 ; i < str . length ; i ++ ) { if ( i % 2 == 1 ) { semiPrime += str [ i ] ; } else { semiPrime += String . fromCharCode ( str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + 65 ) ; } } } if ( isPossibleSum ( N ) ) { for ( let i = 0 ; i < str . length ; i ++ ) { if ( i % 2 == 1 ) { sumOfPrime += String . fromCharCode ( str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + 65 ) ; } else { sumOfPrime += str [ i ] ; } } } if ( semiPrime + sumOfPrime == " " ) { document . write ( " " ) ; } else { document . write ( semiPrime + sumOfPrime ) ; } } let N = 1011243 ; makeCypherString ( N ) ;