Given a number N, the task is to convert the given number into a Cypher string on the basis of below conditions:
- If N is a semiprime, then change every digit at even places of N to it’s corresponding matched alphabet as shown below.
- If N can be written as a sum of two primes, then change every digit at odd places of N to it’s corresponding matched alphabet as shown below.
- If both the condition satisfy the concatenate the above two strings formed.
- If N can’t satisfy the above three criteria, then print “-1”.
Below is the list of matched character:
Examples:
Input: N = 61
Output: 6B
Explanation:
Since 61 can be expressed as a sum of two primes: 61 = 2 + 59
Therefore, the resultant string after changing the character at even index is “6B”.
Input: N = 1011243
Output: B0B1C4D
Explanation:
Since 1011243 is Semiprime number: 1011243 = 3 * 337081
Therefore, the resultant string after change the character at even index is “B0B1C4D”.
Approach:
- Check if the given number N is semi prime or not by using the approach discussed in this article. If yes, then do the following:
- Convert the given number N to string(say str) using to_string() function.
- Traverse the above string formed and changed the characters at even index as:
str[i] = char((str[i] - '0') + 65)
- Print the new string formed.
- Check if the given number N can be expressed as a sum of two prime numbers or not using the approach discussed in this article. If yes, then do the following:
- Convert the given number N to string(say str) using to_string() function.
- Traverse the above string formed and changed the characters at odd index as:
str[i] = char((str[i] - '0') + 65)
- Print the new string formed.
- If the above two condition doesn’t satisfy then we can’t form Cypher String. Print “-1”.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include "bits/stdc++.h" using namespace std; // Function to check whether a number // is prime or not bool isPrime( int n) { if (n <= 1) return false ; for ( int i = 2; i <= sqrt (n); i++) { if (n % i == 0) return false ; } return true ; } // Function to check if a prime number // can be expressed as sum of // two Prime Numbers bool isPossibleSum( int N) { // If the N && (N-2) is Prime if (isPrime(N) && isPrime(N - 2)) { return true ; } else { return false ; } } // Function to check semiPrime bool checkSemiprime( int num) { int cnt = 0; // Loop from 2 to sqrt(num) for ( int i = 2; cnt < 2 && i * i <= num; ++i) { while (num % i == 0) { num /= i, // Increment the count of // prime numbers ++cnt; } } // If num is greater than 1, then // add 1 to it if (num > 1) { ++cnt; } // Return '1' if count is 2 else // return '0' return cnt == 2; } // Function to make the Cypher string void makeCypherString( int N) { // Resultant string string semiPrime = "" ; string sumOfPrime = "" ; // Make string for the number N string str = to_string(N); // Check for semiPrime if (checkSemiprime(N)) { // Traverse to make Cypher string for ( int i = 0; str[i]; i++) { // If index is odd add the // current character if (i & 1) { semiPrime += str[i]; } // Else current character is // changed else { semiPrime += char ( str[i] - '0' + 65); } } } // Check for sum of two primes if (isPossibleSum(N)) { // Traverse to make Cypher string for ( int i = 0; str[i]; i++) { // If index is odd then // current character is // changed if (i & 1) { sumOfPrime += char ( str[i] - '0' + 65); } // Else add the current // character else { sumOfPrime += str[i]; } } } // If the resultant string is "" // then print -1 if (semiPrime + sumOfPrime == "" ) { cout << "-1" ; } // Else print the resultant string else { cout << semiPrime + sumOfPrime; } } // Driver Code int main() { // Given Number int N = 1011243; // Function Call makeCypherString(N); return 0; } |
Java
// Java program for // the above approach import java.util.*; class GFG{ // Function to check // whether a number // is prime or not static boolean isPrime( int n) { if (n <= 1 ) return false ; for ( int i = 2 ; i <= Math.sqrt(n); i++) { if (n % i == 0 ) return false ; } return true ; } // Function to check if a prime number // can be expressed as sum of // two Prime Numbers static boolean isPossibleSum( int N) { // If the N && (N-2) is Prime if (isPrime(N) && isPrime(N - 2 )) { return true ; } else { return false ; } } // Function to check semiPrime static boolean checkSemiprime( int num) { int cnt = 0 ; // Loop from 2 to Math.sqrt(num) for ( int i = 2 ; cnt < 2 && i * i <= num; ++i) { while (num % i == 0 ) { num /= i; // Increment the count of // prime numbers ++cnt; } } // If num is greater than 1, then // add 1 to it if (num > 1 ) { ++cnt; } // Return '1' if count is 2 else // return '0' return cnt == 2 ; } // Function to make the Cypher String static void makeCypherString( int N) { // Resultant String String semiPrime = "" ; String sumOfPrime = "" ; // Make String for the number N String str = String.valueOf(N); // Check for semiPrime if (checkSemiprime(N)) { // Traverse to make Cypher String for ( int i = 0 ; i < str.length(); i++) { // If index is odd add the // current character if (i % 2 == 1 ) { semiPrime += str.charAt(i); } // Else current character is // changed else { semiPrime += ( char )(str.charAt(i) - '0' + 65 ); } } } // Check for sum of two primes if (isPossibleSum(N)) { // Traverse to make Cypher String for ( int i = 0 ; i < str.length(); i++) { // If index is odd then // current character is // changed if (i % 2 == 1 ) { sumOfPrime += ( char )(str.charAt(i) - '0' + 65 ); } // Else add the current // character else { sumOfPrime += str.charAt(i); } } } // If the resultant String is "" // then print -1 if (semiPrime + sumOfPrime == "" ) { System.out.print( "-1" ); } // Else print the resultant String else { System.out.print(semiPrime + sumOfPrime); } } // Driver Code public static void main(String[] args) { // Given Number int N = 1011243 ; // Function Call makeCypherString(N); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 program for the above approach import math # Function to check whether a number # is prime or not def isPrime(n): if (n < = 1 ): return False sqt = ( int )(math.sqrt(n)) for i in range ( 2 , sqt): if (n % i = = 0 ): return False return True # Function to check if a prime number # can be expressed as sum of # two Prime Numbers def isPossibleSum(N): # If the N && (N-2) is Prime if (isPrime(N) and isPrime(N - 2 )): return True else : return False # Function to check semiPrime def checkSemiprime(num): cnt = 0 # Loop from 2 to sqrt(num) i = 2 while cnt < 2 and i * i < = num: while (num % i = = 0 ): num / / = i # Increment the count of # prime numbers cnt + = 1 i + = 1 # If num is greater than 1, then # add 1 to it if (num > 1 ): cnt + = 1 # Return '1' if count is 2 else # return '0' return cnt = = 2 # Function to make the Cypher string def makeCypherString(N): # Resultant string semiPrime = "" sumOfPrime = "" # Make string for the number N st = str (N) # Check for semiPrime if (checkSemiprime(N)): # Traverse to make Cypher string for i in range ( len (st)): # If index is odd add the # current character if (i & 1 ): semiPrime + = st[i] # Else current character is # changed else : semiPrime + = chr ( ord (st[i]) - ord ( '0' ) + 65 ) # Check for sum of two primes if (isPossibleSum(N)): # Traverse to make Cypher string for i in range ( len (st)): # If index is odd then # current character is # changed if (i & 1 ): sumOfPrime + = chr ( ord (st[i]) - ord ( '0' ) + 65 ) # Else add the current # character else : sumOfPrime + = st[i] # If the resultant string is "" # then print -1 if (semiPrime + sumOfPrime = = ""): print ( "-1" ) # Else print the resultant string else : print (semiPrime + sumOfPrime) # Driver Code if __name__ = = "__main__" : # Given number N = 1011243 # Function call makeCypherString(N) # This code is contributed by chitranayal |
C#
// C# program for // the above approach using System; class GFG{ // Function to check // whether a number // is prime or not static bool isPrime( int n) { if (n <= 1) return false ; for ( int i = 2; i <= Math.Sqrt(n); i++) { if (n % i == 0) return false ; } return true ; } // Function to check if a prime number // can be expressed as sum of // two Prime Numbers static bool isPossibleSum( int N) { // If the N && (N-2) is Prime if (isPrime(N) && isPrime(N - 2)) { return true ; } else { return false ; } } // Function to check semiPrime static bool checkSemiprime( int num) { int cnt = 0; // Loop from 2 to Math.Sqrt(num) for ( int i = 2; cnt < 2 && i * i <= num; ++i) { while (num % i == 0) { num /= i; // Increment the count of // prime numbers ++cnt; } } // If num is greater than 1, then // add 1 to it if (num > 1) { ++cnt; } // Return '1' if count is 2 else // return '0' return cnt == 2; } // Function to make the Cypher String static void makeCypherString( int N) { // Resultant String String semiPrime = "" ; String sumOfPrime = "" ; // Make String for the number N String str = String.Join( "" , N); // Check for semiPrime if (checkSemiprime(N)) { // Traverse to make Cypher String for ( int i = 0; i < str.Length; i++) { // If index is odd add the // current character if (i % 2 == 1) { semiPrime += str[i]; } // Else current character is // changed else { semiPrime += ( char )(str[i] - '0' + 65); } } } // Check for sum of two primes if (isPossibleSum(N)) { // Traverse to make Cypher String for ( int i = 0; i < str.Length; i++) { // If index is odd then // current character is // changed if (i % 2 == 1) { sumOfPrime += ( char )(str[i] - '0' + 65); } // Else add the current // character else { sumOfPrime += str[i]; } } } // If the resultant String is "" // then print -1 if (semiPrime + sumOfPrime == "" ) { Console.Write( "-1" ); } // Else print the resultant String else { Console.Write(semiPrime + sumOfPrime); } } // Driver Code public static void Main(String[] args) { // Given Number int N = 1011243; // Function Call makeCypherString(N); } } // This code is contributed by Rajput-Ji |
Javascript
<script> // Javascript program for // the above approach // Function to check // whether a number // is prime or not 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 to check if a prime number // can be expressed as sum of // two Prime Numbers function isPossibleSum(N) { // If the N && (N-2) is Prime if (isPrime(N) && isPrime(N - 2)) { return true ; } else { return false ; } } // Function to check semiPrime function checkSemiprime(num) { let cnt = 0; // Loop from 2 to Math.sqrt(num) for (let i = 2; cnt < 2 && i * i <= num; ++i) { while (num % i == 0) { num = Math.floor(num/i); // Increment the count of // prime numbers ++cnt; } } // If num is greater than 1, then // add 1 to it if (num > 1) { ++cnt; } // Return '1' if count is 2 else // return '0' return cnt == 2; } // Function to make the Cypher String function makeCypherString(N) { // Resultant String let semiPrime = "" ; let sumOfPrime = "" ; // Make String for the number N let str = (N).toString(); // Check for semiPrime if (checkSemiprime(N)) { // Traverse to make Cypher String for (let i = 0; i < str.length; i++) { // If index is odd add the // current character if (i % 2 == 1) { semiPrime += str[i]; } // Else current character is // changed else { semiPrime += String.fromCharCode(str[i].charCodeAt(0) - '0' .charCodeAt(0) + 65); } } } // Check for sum of two primes if (isPossibleSum(N)) { // Traverse to make Cypher String for (let i = 0; i < str.length; i++) { // If index is odd then // current character is // changed if (i % 2 == 1) { sumOfPrime += String.fromCharCode(str[i].charCodeAt(0) - '0' .charCodeAt(0) + 65); } // Else add the current // character else { sumOfPrime += str[i]; } } } // If the resultant String is "" // then print -1 if (semiPrime + sumOfPrime == "" ) { document.write( "-1" ); } // Else print the resultant String else { document.write(semiPrime + sumOfPrime); } } // Driver Code // Given Number let N = 1011243; // Function Call makeCypherString(N); // This code is contributed by avanitrachhadiya2155 </script> |
Output:
B0B1C4D
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!