给定整数N ,任务是检查N是否为二面质数。二面体质数是素数,当在七段显示器中读取时,无论其方向和表面如何,都可以将其本身或另一个质数读取。
例子:
Input: N = 108881
Output: Yes
Input: N = 789
Output: No
方法:为素数测试预先计算素数筛。 Eratosthenes的筛子可以n * logn * logn时间计算。对数字及其不同方向进行素性测试。如果该数字通过了素数测试,请检查是否有任何数字属于排除集[3、4、6、7、9]。如果数字通过两个测试,则返回true。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
bool isPrime[int(1e5) + 5];
// Function to return the reverse
// of a number
int reverse(int n)
{
int temp = n;
int sum = 0;
while (temp > 0) {
int rem = temp % 10;
sum = sum * 10 + rem;
temp /= 10;
}
return sum;
}
// Function to generate mirror reflection
// of a number
int mirror(int n)
{
int temp = n;
int sum = 0;
while (temp > 0) {
int rem = temp % 10;
if (rem == 2)
rem = 5;
else if (rem == 5)
rem = 2;
sum = sum * 10 + rem;
temp /= 10;
}
return sum;
}
// Function to initialize prime number sieve
bool sieve()
{
memset(isPrime, true, sizeof isPrime);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i <= int(1e5); i++) {
for (int j = 2; i * j <= int(1e5); j++) {
isPrime[i * j] = false;
}
}
}
// Function that returns true if n is
// Dihedral Prime number
bool isDihedralPrime(int n)
{
// Check if the orientations of n
// is also prime
if (!isPrime[n]
|| !isPrime[mirror(n)]
|| !isPrime[reverse(n)]
|| !isPrime[reverse(mirror(n))])
return false;
int temp = n;
while (temp > 0) {
int rem = temp % 10;
if (rem == 3 || rem == 4 || rem == 6
|| rem == 7 || rem == 9)
return false;
temp /= 10;
}
return true;
}
// Driver code
int main()
{
sieve();
int n = 18181;
if (isDihedralPrime(n))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java implementation of the above approach
import java.util.*;
class GFG
{
static boolean[] isPrime = new boolean[(int) (1e5) + 5];
// Function to return the reverse
// of a number
static int reverse(int n)
{
int temp = n;
int sum = 0;
while (temp > 0)
{
int rem = temp % 10;
sum = sum * 10 + rem;
temp /= 10;
}
return sum;
}
// Function to generate mirror reflection
// of a number
static int mirror(int n)
{
int temp = n;
int sum = 0;
while (temp > 0)
{
int rem = temp % 10;
if (rem == 2)
{
rem = 5;
}
else if (rem == 5)
{
rem = 2;
}
sum = sum * 10 + rem;
temp /= 10;
}
return sum;
}
// Function to initialize
// prime number sieve
static void sieve()
{
Arrays.fill(isPrime, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2;
i <= (int) 1e5; i++)
{
for (int j = 2;
i * j <= (int) 1e5; j++)
{
isPrime[i * j] = false;
}
}
}
// Function that returns true if n is
// Dihedral Prime number
static boolean isDihedralPrime(int n)
{
// Check if the orientations of n
// is also prime
if (!isPrime[n] ||
!isPrime[mirror(n)] ||
!isPrime[reverse(n)] ||
!isPrime[reverse(mirror(n))])
{
return false;
}
int temp = n;
while (temp > 0)
{
int rem = temp % 10;
if (rem == 3 || rem == 4 ||
rem == 6 || rem == 7 ||
rem == 9)
{
return false;
}
temp /= 10;
}
return true;
}
// Driver code
public static void main(String[] args)
{
sieve();
int n = 18181;
if (isDihedralPrime(n))
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
}
// This code is contributed by Rajput-Ji
Python3
# Python implementation of the above approach
isPrime = (int(1e5)+5)*[True]
# Function to return the reverse
# of a number
def reverse(n):
temp = n
sum = 0
while temp>0:
rem = temp % 10
sum = sum * 10 + rem
temp//= 10
return sum
# Function to generate mirror reflection
# of a number
def mirror(n):
temp = n
sum = 0
while temp>0:
rem = temp % 10
if rem == 2:
rem = 5
elif rem == 5:
rem = 2
sum = sum * 10 + rem
temp//= 10
return sum
# Function to initialize prime number sieve
def sieve():
isPrime[0] = isPrime[1] = False
for i in range(2, int(1e5)+1):
j = 2
while i * j<= int(1e5):
isPrime[i * j] = False
j+= 1
# Function that returns true if n is
# Dihedral Prime number
def isDihedralPrime(n):
# Check if the orientations of n is also prime
if (not isPrime[n]) or (not isPrime[mirror(n)]) \
or (not isPrime[reverse(n)]) \
or (not isPrime[reverse(mirror(n))]):
return False
temp = n
while temp>0:
rem = temp % 10;
if rem == 3 or rem == 4 or \
rem == 6 or rem == 7 or rem == 9:
return False
temp//= 10
return True
# Driver code
if __name__ == '__main__':
sieve()
n = 18181
if isDihedralPrime(n):
print("Yes")
else :
print("No")
C#
// C# implementation of the above approach
using System;
class GFG
{
static Boolean[] isPrime = new Boolean[(int) (1e5) + 5];
// Function to return the reverse
// of a number
static int reverse(int n)
{
int temp = n;
int sum = 0;
while (temp > 0)
{
int rem = temp % 10;
sum = sum * 10 + rem;
temp /= 10;
}
return sum;
}
// Function to generate mirror reflection
// of a number
static int mirror(int n)
{
int temp = n;
int sum = 0;
while (temp > 0)
{
int rem = temp % 10;
if (rem == 2)
{
rem = 5;
}
else if (rem == 5)
{
rem = 2;
}
sum = sum * 10 + rem;
temp /= 10;
}
return sum;
}
// Function to initialize
// prime number sieve
static void sieve()
{
for(int k = 0; k < isPrime.Length; k++)
isPrime[k] = true;
isPrime[0] = isPrime[1] = false;
for (int i = 2;
i <= (int) 1e5; i++)
{
for (int j = 2;
i * j <= (int) 1e5; j++)
{
isPrime[i * j] = false;
}
}
}
// Function that returns true if n is
// Dihedral Prime number
static Boolean isDihedralPrime(int n)
{
// Check if the orientations of n
// is also prime
if (!isPrime[n] ||
!isPrime[mirror(n)] ||
!isPrime[reverse(n)] ||
!isPrime[reverse(mirror(n))])
{
return false;
}
int temp = n;
while (temp > 0)
{
int rem = temp % 10;
if (rem == 3 || rem == 4 ||
rem == 6 || rem == 7 ||
rem == 9)
{
return false;
}
temp /= 10;
}
return true;
}
// Driver code
public static void Main(String[] args)
{
sieve();
int n = 18181;
if (isDihedralPrime(n))
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
}
}
// This code is contributed by Rajput-Ji
PHP
0)
{
$rem = $temp % 10;
$sum = $sum * 10 + $rem;
$temp = floor($temp / 10);
}
return $sum;
}
// Function to generate mirror reflection
// of a number
function mirror($n)
{
$temp = $n;
$sum = 0;
while ($temp > 0)
{
$rem = $temp % 10;
if ($rem == 2)
$rem = 5;
else if ($rem == 5)
$rem = 2;
$sum = $sum * 10 + $rem;
$temp = floor($temp / 10);
}
return $sum;
}
// Function to initialize prime number sieve
function sieve()
{
$GLOBALS['isPrime'][0] = $GLOBALS['isPrime'][1] = false;
for ($i = 2; $i <= floor(1e4); $i++)
{
for ($j = 2; $i * $j <= floor(1e4); $j++)
{
$GLOBALS['isPrime'][$i * $j] = false;
}
}
}
// Function that returns true if n is
// Dihedral Prime number
function isDihedralPrime($n)
{
// Check if the orientations of n
// is also prime
if (!$GLOBALS['isPrime'][$n] ||
!$GLOBALS['isPrime'][mirror($n)] ||
!$GLOBALS['isPrime'][reverse($n)] ||
!$GLOBALS['isPrime'][reverse(mirror($n))])
return false;
$temp = $n;
while ($temp > 0)
{
$rem = $temp % 10;
if ($rem == 3 || $rem == 4 ||
$rem == 6 || $rem == 7 || $rem == 9)
return false;
$temp = floor($temp / 10);
}
return true;
}
// Driver code
sieve();
$n = 18181;
if (isDihedralPrime($n))
echo "Yes";
else
echo "No";
// This code is contributed by Ryuga
?>
输出:
Yes
时间复杂度: O(n * log(log n))
辅助空间: O(10 5 )