Weakly Prime Numbers
Last Updated :
25 May, 2021
Given an integer N, the task is to check if N is a weakly prime numbers.
Weakly Prime Numbers are prime numbers which cannot be turned into a prime by changing a single digit.
Examples:
Input: N = 294001
Output: Yes
Input: N = 30
Output: No
Solution : If the number is composite return false else We will replace every digit of the number with all possible digits from 0 to 9 for every position one by one and return false if number formed by this is a prime number.
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0
|| n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0
|| n % (i + 2) == 0)
return false ;
return true ;
}
bool isWeaklyPrimeNum( int N)
{
if (!isPrime(N))
return false ;
string s = to_string(N);
for ( int j = 0; j < s.length(); j++) {
string str = s;
for ( int i = 0; i <= 9; i++) {
char c = '0' + i;
str[j] = c;
int Num = stoi(str);
if (str[j] != s[j]
&& isPrime(Num)) {
return false ;
}
}
}
return true ;
}
int main()
{
int n = 294001;
if (isWeaklyPrimeNum(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG{
static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
if (n <= 3 )
return true ;
if (n % 2 == 0 || n % 3 == 0 )
return false ;
for ( int i = 5 ; i * i <= n; i = i + 6 )
if (n % i == 0 || n % (i + 2 ) == 0 )
return false ;
return true ;
}
static boolean isWeaklyPrimeNum( int N)
{
if (!isPrime(N))
return false ;
String s = String.valueOf(N);
for ( int j = 0 ; j < s.length(); j++)
{
char []str = s.toCharArray();
for ( int i = 0 ; i <= 9 ; i++)
{
char c = ( char )( '0' + i);
str[j] = c;
int Num = Integer.valueOf(
String.valueOf(str));
if (str[j] != s.charAt(j) &&
isPrime(Num))
{
return false ;
}
}
}
return true ;
}
public static void main(String[] args)
{
int n = 294001 ;
if (isWeaklyPrimeNum(n))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def isPrime(n):
if (n < = 1 ) :
return False
if (n < = 3 ) :
return True
if (n % 2 = = 0 or n % 3 = = 0 ):
return False
for i in range ( 5 , int (n * * 0.5 ) + 1 , 6 ):
if (n % i = = 0 or n % (i + 2 ) = = 0 ):
return False
return True
def isWeaklyPrimeNum(N):
if (isPrime(N) = = False ) :
return False
s = str (N)
for j in range ( len (s)):
str1 = s
for i in range ( 10 ):
c = str (i)
str1 = str1[ : j] + c + str1[j + 1 : ]
Num = int (str1)
if (str1[j] ! = s[j] and isPrime(Num)) :
return False
return True
n = 294001
if (isWeaklyPrimeNum(n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
static bool isWeaklyPrimeNum( int N)
{
if (!isPrime(N))
return false ;
String s = String.Join( "" , N);
for ( int j = 0; j < s.Length; j++)
{
char []str = s.ToCharArray();
for ( int i = 0; i <= 9; i++)
{
char c = ( char )( '0' + i);
str[j] = c;
int Num = Int32.Parse(String.Join( "" , str));
if (str[j] != s[j] && isPrime(Num))
{
return false ;
}
}
}
return true ;
}
public static void Main(String[] args)
{
int n = 294001;
if (isWeaklyPrimeNum(n))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isPrime(n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0
|| n % 3 == 0)
return false ;
for ( var i = 5; i * i <= n; i = i + 6)
if (n % i == 0
|| n % (i + 2) == 0)
return false ;
return true ;
}
function isWeaklyPrimeNum(N)
{
if (!isPrime(N))
return false ;
var s = N.toString();
for ( var j = 0; j < s.length; j++) {
var str = s;
for ( var i = 0; i <= 9; i++) {
var c = '0' + i.toString();
str[j] = c;
var Num = parseInt(str);
if (str[j] != s[j]
&& isPrime(Num)) {
return false ;
}
}
}
return true ;
}
var N = 294001;
if (isWeaklyPrimeNum(N))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
References: http://oeis.org/A050249
Share your thoughts in the comments
Please Login to comment...