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++
#include "bits/stdc++.h"
using namespace std;
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 ;
}
bool isPossibleSum( int N)
{
if (isPrime(N)
&& isPrime(N - 2)) {
return true ;
}
else {
return false ;
}
}
bool checkSemiprime( int num)
{
int cnt = 0;
for ( int i = 2; cnt < 2
&& i * i <= num;
++i) {
while (num % i == 0) {
num /= i,
++cnt;
}
}
if (num > 1) {
++cnt;
}
return cnt == 2;
}
void makeCypherString( int N)
{
string semiPrime = "" ;
string sumOfPrime = "" ;
string str = to_string(N);
if (checkSemiprime(N)) {
for ( int i = 0; str[i]; i++) {
if (i & 1) {
semiPrime += str[i];
}
else {
semiPrime
+= char (
str[i] - '0' + 65);
}
}
}
if (isPossibleSum(N)) {
for ( int i = 0; str[i]; i++) {
if (i & 1) {
sumOfPrime
+= char (
str[i] - '0' + 65);
}
else {
sumOfPrime += str[i];
}
}
}
if (semiPrime + sumOfPrime == "" ) {
cout << "-1" ;
}
else {
cout << semiPrime + sumOfPrime;
}
}
int main()
{
int N = 1011243;
makeCypherString(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
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 ;
}
static boolean isPossibleSum( int N)
{
if (isPrime(N) && isPrime(N - 2 ))
{
return true ;
}
else
{
return false ;
}
}
static boolean checkSemiprime( int num)
{
int cnt = 0 ;
for ( int i = 2 ; cnt < 2 &&
i * i <= num; ++i)
{
while (num % i == 0 )
{
num /= i;
++cnt;
}
}
if (num > 1 )
{
++cnt;
}
return cnt == 2 ;
}
static void makeCypherString( int N)
{
String semiPrime = "" ;
String sumOfPrime = "" ;
String str = String.valueOf(N);
if (checkSemiprime(N))
{
for ( int i = 0 ; i < str.length(); i++)
{
if (i % 2 == 1 )
{
semiPrime += str.charAt(i);
}
else
{
semiPrime += ( char )(str.charAt(i) -
'0' + 65 );
}
}
}
if (isPossibleSum(N))
{
for ( int i = 0 ; i < str.length(); i++)
{
if (i % 2 == 1 )
{
sumOfPrime += ( char )(str.charAt(i) -
'0' + 65 );
}
else
{
sumOfPrime += str.charAt(i);
}
}
}
if (semiPrime + sumOfPrime == "" )
{
System.out.print( "-1" );
}
else
{
System.out.print(semiPrime +
sumOfPrime);
}
}
public static void main(String[] args)
{
int N = 1011243 ;
makeCypherString(N);
}
}
|
Python3
import math
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
def isPossibleSum(N):
if (isPrime(N) and isPrime(N - 2 )):
return True
else :
return False
def checkSemiprime(num):
cnt = 0
i = 2
while cnt < 2 and i * i < = num:
while (num % i = = 0 ):
num / / = i
cnt + = 1
i + = 1
if (num > 1 ):
cnt + = 1
return cnt = = 2
def makeCypherString(N):
semiPrime = ""
sumOfPrime = ""
st = str (N)
if (checkSemiprime(N)):
for i in range ( len (st)):
if (i & 1 ):
semiPrime + = st[i]
else :
semiPrime + = chr ( ord (st[i]) -
ord ( '0' ) + 65 )
if (isPossibleSum(N)):
for i in range ( len (st)):
if (i & 1 ):
sumOfPrime + = chr ( ord (st[i]) -
ord ( '0' ) + 65 )
else :
sumOfPrime + = st[i]
if (semiPrime + sumOfPrime = = ""):
print ( "-1" )
else :
print (semiPrime + sumOfPrime)
if __name__ = = "__main__" :
N = 1011243
makeCypherString(N)
|
C#
using System;
class GFG{
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 ;
}
static bool isPossibleSum( int N)
{
if (isPrime(N) && isPrime(N - 2))
{
return true ;
}
else
{
return false ;
}
}
static bool checkSemiprime( int num)
{
int cnt = 0;
for ( int i = 2; cnt < 2 &&
i * i <= num; ++i)
{
while (num % i == 0)
{
num /= i;
++cnt;
}
}
if (num > 1)
{
++cnt;
}
return cnt == 2;
}
static void makeCypherString( int N)
{
String semiPrime = "" ;
String sumOfPrime = "" ;
String str = String.Join( "" , N);
if (checkSemiprime(N))
{
for ( int i = 0; i < str.Length; i++)
{
if (i % 2 == 1)
{
semiPrime += str[i];
}
else
{
semiPrime += ( char )(str[i] -
'0' + 65);
}
}
}
if (isPossibleSum(N))
{
for ( int i = 0; i < str.Length; i++)
{
if (i % 2 == 1)
{
sumOfPrime += ( char )(str[i] -
'0' + 65);
}
else
{
sumOfPrime += str[i];
}
}
}
if (semiPrime + sumOfPrime == "" )
{
Console.Write( "-1" );
}
else
{
Console.Write(semiPrime +
sumOfPrime);
}
}
public static void Main(String[] args)
{
int N = 1011243;
makeCypherString(N);
}
}
|
Javascript
<script>
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) -
'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) -
'0' .charCodeAt(0) + 65);
}
else
{
sumOfPrime += str[i];
}
}
}
if (semiPrime + sumOfPrime == "" )
{
document.write( "-1" );
}
else
{
document.write(semiPrime +
sumOfPrime);
}
}
let N = 1011243;
makeCypherString(N);
</script>
|
Last Updated :
04 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...