Construct the Cypher string based on the given conditions
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 notbool 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 Numbersbool isPossibleSum(int N){ // If the N && (N-2) is Prime if (isPrime(N) && isPrime(N - 2)) { return true; } else { return false; }}// Function to check semiPrimebool 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 stringvoid 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 Codeint main(){ // Given Number int N = 1011243; // Function Call makeCypherString(N); return 0;} |
Java
// Java program for// the above approachimport java.util.*;class GFG{// Function to check// whether a number// is prime or notstatic 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 Numbersstatic 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 semiPrimestatic 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 Stringstatic 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 Codepublic 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 approachimport math# Function to check whether a number# is prime or notdef 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 Numbersdef isPossibleSum(N): # If the N && (N-2) is Prime if (isPrime(N) and isPrime(N - 2)): return True else: return False# Function to check semiPrimedef 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 stringdef 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 Codeif __name__ == "__main__": # Given number N = 1011243 # Function call makeCypherString(N)# This code is contributed by chitranayal |
C#
// C# program for// the above approachusing System;class GFG{// Function to check// whether a number// is prime or notstatic 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 Numbersstatic 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 semiPrimestatic 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 Stringstatic 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 Codepublic 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 notfunction 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 Numbersfunction isPossibleSum(N){ // If the N && (N-2) is Prime if (isPrime(N) && isPrime(N - 2)) { return true; } else { return false; }}// Function to check semiPrimefunction 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 Stringfunction 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> |
B0B1C4D
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


