给定一个正整数n,任务是检查它是否为Euclid Number。如果给定的号码是欧几里德号码,则打印“ YES”,否则打印“ NO”。
欧几里德数: 在数学中,欧几里德数是以下形式的整数:
在哪里是前n个质数的乘积。
前几个Euclid号码是-
3, 7, 31, 211, 2311, 30031, 510511, 9699691, ……….
例子:
Input: N = 31
Output: YES
31 can be expressed in the form of
pn# + 1 as p3# + 1
(First 3 prime numbers are 2, 3, 5 and their product is 30 )
Input: N = 43
Output: NO
43 cannot be expressed in the form of pn# + 1
天真的方法:
- 使用Eratosthenes筛网生成该范围内的所有素数。
- 然后从第一个质数(即2)开始乘以下一个质数,并继续检查乘积+ 1 = n 。
- 如果乘积+1 = n,则n是欧几里德数。否则不行。
下面是上述方法的实现:
C++
// CPP program to check Euclid Number
#include
using namespace std;
#define MAX 10000
vector arr;
// Function to generate prime numbers
void SieveOfEratosthenes()
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
bool prime[MAX];
memset(prime, true, sizeof(prime));
for (int p = 2; p * p < MAX; p++) {
// If prime[p] is not changed, then it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i < MAX; i += p)
prime[i] = false;
}
}
// store all prime numbers
// to vector 'arr'
for (int p = 2; p < MAX; p++)
if (prime[p])
arr.push_back(p);
}
// Function to check the number for Euclid Number
bool isEuclid(long n)
{
long long product = 1;
int i = 0;
while (product < n) {
// Multiply next prime number
// and check if product + 1 = n
// holds or not
product = product * arr[i];
if (product + 1 == n)
return true;
i++;
}
return false;
}
// Driver code
int main()
{
// Get the prime numbers
SieveOfEratosthenes();
// Get n
long n = 31;
// Check if n is Euclid Number
if (isEuclid(n))
cout << "YES\n";
else
cout << "NO\n";
// Get n
n = 42;
// Check if n is Euclid Number
if (isEuclid(n))
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
Java
// Java program to check Euclid Number
import java.util.*;
class GFG {
static final int MAX = 10000;
static Vector arr = new Vector();
// Function to get the prime numbers
static void SieveOfEratosthenes()
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
boolean[] prime = new boolean[MAX];
for (int i = 0; i < MAX; i++)
prime[i] = true;
for (int p = 2; p * p < MAX; p++) {
// If prime[p] is not changed, then it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i < MAX; i += p)
prime[i] = false;
}
}
// store all prime numbers
// to vector 'arr'
for (int p = 2; p < MAX; p++)
if (prime[p])
arr.add(p);
}
// Function to check the number for Euclid Number
static boolean isEuclid(long n)
{
long product = 1;
int i = 0;
while (product < n) {
// Multiply next prime number
// and check if product + 1 = n
// holds or not
product = product * arr.get(i);
if (product + 1 == n)
return true;
i++;
}
return false;
}
public static void main(String[] args)
{
// Get the prime numbers
SieveOfEratosthenes();
// Get n
long n = 31;
// Check if n is Euclid Number
if (isEuclid(n))
System.out.println("YES");
else
System.out.println("NO");
// Get n
n = 42;
// Check if n is Euclid Number
if (isEuclid(n))
System.out.println("YES");
else
System.out.println("NO");
}
}
Python 3
# Python 3 program to check
# Euclid Number
MAX = 10000
arr = []
# Function to generate prime numbers
def SieveOfEratosthenes():
# Create a boolean array "prime[0..n]"
# and initialize all entries it as
# true. A value in prime[i] will
# finally be false if i is Not a
# prime, else true.
prime = [True] * MAX
p = 2
while p * p < MAX :
# If prime[p] is not changed,
# then it is a prime
if (prime[p] == True):
# Update all multiples of p
for i in range(p * 2, MAX, p):
prime[i] = False
p += 1
# store all prime numbers
# to vector 'arr'
for p in range(2, MAX):
if (prime[p]):
arr.append(p)
# Function to check the number
# for Euclid Number
def isEuclid(n):
product = 1
i = 0
while (product < n) :
# Multiply next prime number
# and check if product + 1 = n
# holds or not
product = product * arr[i]
if (product + 1 == n):
return True
i += 1
return False
# Driver code
if __name__ == "__main__":
# Get the prime numbers
SieveOfEratosthenes()
# Get n
n = 31
# Check if n is Euclid Number
if (isEuclid(n)):
print("YES")
else:
print("NO")
# Get n
n = 42
# Check if n is Euclid Number
if (isEuclid(n)):
print("YES")
else:
print("NO")
# This code is contributed
# by ChitraNayal
C#
// C# program to check Euclid Number
using System;
using System.Collections.Generic;
class GFG
{
static readonly int MAX = 10000;
static List arr = new List();
// Function to get the prime numbers
static void SieveOfEratosthenes()
{
// Create a boolean array
// "prime[0..n]" and initialize
// all entries it as true.
// A value in prime[i] will
// finally be false if i is
// Not a prime, else true.
bool[] prime = new bool[MAX];
for (int i = 0; i < MAX; i++)
prime[i] = true;
for (int p = 2; p * p < MAX; p++)
{
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * 2; i < MAX; i += p)
prime[i] = false;
}
}
// store all prime numbers
// to vector 'arr'
for (int p = 2; p < MAX; p++)
if (prime[p])
arr.Add(p);
}
// Function to check the number for Euclid Number
static bool isEuclid(long n)
{
long product = 1;
int i = 0;
while (product < n)
{
// Multiply next prime number
// and check if product + 1 = n
// holds or not
product = product * arr[i];
if (product + 1 == n)
return true;
i++;
}
return false;
}
// Driver code
public static void Main(String[] args)
{
// Get the prime numbers
SieveOfEratosthenes();
// Get n
long n = 31;
// Check if n is Euclid Number
if (isEuclid(n))
Console.WriteLine("YES");
else
Console.WriteLine("NO");
// Get n
n = 42;
// Check if n is Euclid Number
if (isEuclid(n))
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
}
// This code has been contributed by 29AjayKumar
Javascript
C++
// CPP program to check Euclid Number
#include
using namespace std;
#define MAX 10000
unordered_set s;
// Function to generate the Prime numbers
// and store their products
void SieveOfEratosthenes()
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
bool prime[MAX];
memset(prime, true, sizeof(prime));
for (int p = 2; p * p < MAX; p++) {
// If prime[p] is not changed, then it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i < MAX; i += p)
prime[i] = false;
}
}
// store prefix product of prime numbers
// to unordered_set 's'
long long int product = 1;
for (int p = 2; p < MAX; p++) {
if (prime[p]) {
// update product by multipying
// next prime
product = product * p;
// insert 'produc+1' to set
s.insert(product + 1);
}
}
}
// Function to check the number for Euclid Number
bool isEuclid(long n)
{
// Check if number exist in
// unordered set or not
// If exist, return true
if (s.find(n) != s.end())
return true;
else
return false;
}
// Driver code
int main()
{
// Get the prime numbers
SieveOfEratosthenes();
// Get n
long n = 31;
// Check if n is Euclid Number
if (isEuclid(n))
cout << "YES\n";
else
cout << "NO\n";
// Get n
n = 42;
// Check if n is Euclid Number
if (isEuclid(n))
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
Java
// Java program to check Euclid Number
import java.util.*;
class GFG
{
static int MAX = 10000;
static HashSet s = new HashSet();
// Function to generate the Prime numbers
// and store their products
static void SieveOfEratosthenes()
{
// Create a boolean array "prime[0..n]" and
// initialize all entries it as true.
// A value in prime[i] will finally be false
// if i is Not a prime, else true.
boolean []prime = new boolean[MAX];
Arrays.fill(prime, true);
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p < MAX; p++)
{
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * 2; i < MAX; i += p)
prime[i] = false;
}
}
// store prefix product of prime numbers
// to unordered_set 's'
int product = 1;
for (int p = 2; p < MAX; p++)
{
if (prime[p])
{
// update product by multipying
// next prime
product = product * p;
// insert 'produc+1' to set
s.add(product + 1);
}
}
}
// Function to check the number for Euclid Number
static boolean isEuclid(int n)
{
// Check if number exist in
// unordered set or not
// If exist, return true
if (s.contains(n))
return true;
else
return false;
}
// Driver code
public static void main(String[] args)
{
// Get the prime numbers
SieveOfEratosthenes();
// Get n
int n = 31;
// Check if n is Euclid Number
if (isEuclid(n))
System.out.println("Yes");
else
System.out.println("No");
// Get n
n = 42;
// Check if n is Euclid Number
if (isEuclid(n))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to check Euclid Number
MAX = 10000
s = set()
# Function to generate the Prime numbers
# and store their products
def SieveOfEratosthenes():
# Create a boolean array "prime[0..n]"
# and initialize all entries it as true.
# A value in prime[i] will finally be
# false if i is Not a prime, else true.
prime = [True] * (MAX)
prime[0], prime[1] = False, False
for p in range(2, 100):
# If prime[p] is not changed,
# then it is a prime
if prime[p] == True:
# Update all multiples of p
for i in range(p * 2, MAX, p):
prime[i] = False
# store prefix product of prime numbers
# to unordered_set 's'
product = 1
for p in range(2, MAX):
if prime[p] == True:
# update product by multipying
# next prime
product = product * p
# insert 'produc+1' to set
s.add(product + 1)
# Function to check the number
# for Euclid Number
def isEuclid(n):
# Check if number exist in
# unordered set or not
# If exist, return true
if n in s:
return True
else:
return False
# Driver code
if __name__ == "__main__":
# Get the prime numbers
SieveOfEratosthenes()
# Get n
n = 31
# Check if n is Euclid Number
if isEuclid(n) == True:
print("YES")
else:
print("NO")
# Get n
n = 42
# Check if n is Euclid Number
if isEuclid(n) == True:
print("YES")
else:
print("NO")
# This code is contributed by Rituraj Jain
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
static int MAX = 10000;
static HashSet s = new HashSet();
// Function to generate the Prime numbers
// and store their products
static void SieveOfEratosthenes()
{
// Create a boolean array "prime[0..n]" and
// initialize all entries it as true.
// A value in prime[i] will finally be false
// if i is Not a prime, else true.
Boolean []prime = new Boolean[MAX];
for (int p = 0; p < MAX; p++)
prime[p] = true;
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p < MAX; p++)
{
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * 2; i < MAX; i += p)
prime[i] = false;
}
}
// store prefix product of prime numbers
// to unordered_set 's'
int product = 1;
for (int p = 2; p < MAX; p++)
{
if (prime[p])
{
// update product by multipying
// next prime
product = product * p;
// insert 'produc+1' to set
s.Add(product + 1);
}
}
}
// Function to check the number
// for Euclid Number
static Boolean isEuclid(int n)
{
// Check if number exist in
// unordered set or not
// If exist, return true
if (s.Contains(n))
return true;
else
return false;
}
// Driver code
public static void Main(String[] args)
{
// Get the prime numbers
SieveOfEratosthenes();
// Get n
int n = 31;
// Check if n is Euclid Number
if (isEuclid(n))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
// Get n
n = 42;
// Check if n is Euclid Number
if (isEuclid(n))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by Princi Singh
输出:
YES
NO
注意:以上方法对于每个查询(每N个)将采用O(P n #) ,即否。要乘以素数的整数,以检查n是否为欧几里德数。
高效方法:
- 使用Eratosthenes筛网生成该范围内的所有素数。
- 计算质数的前缀乘积,最大范围应避免使用哈希表重新计算乘积。
- 如果乘积+1 = n,则n是欧几里德数。否则不行。
下面是上述方法的实现:
C++
// CPP program to check Euclid Number
#include
using namespace std;
#define MAX 10000
unordered_set s;
// Function to generate the Prime numbers
// and store their products
void SieveOfEratosthenes()
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
bool prime[MAX];
memset(prime, true, sizeof(prime));
for (int p = 2; p * p < MAX; p++) {
// If prime[p] is not changed, then it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i < MAX; i += p)
prime[i] = false;
}
}
// store prefix product of prime numbers
// to unordered_set 's'
long long int product = 1;
for (int p = 2; p < MAX; p++) {
if (prime[p]) {
// update product by multipying
// next prime
product = product * p;
// insert 'produc+1' to set
s.insert(product + 1);
}
}
}
// Function to check the number for Euclid Number
bool isEuclid(long n)
{
// Check if number exist in
// unordered set or not
// If exist, return true
if (s.find(n) != s.end())
return true;
else
return false;
}
// Driver code
int main()
{
// Get the prime numbers
SieveOfEratosthenes();
// Get n
long n = 31;
// Check if n is Euclid Number
if (isEuclid(n))
cout << "YES\n";
else
cout << "NO\n";
// Get n
n = 42;
// Check if n is Euclid Number
if (isEuclid(n))
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
Java
// Java program to check Euclid Number
import java.util.*;
class GFG
{
static int MAX = 10000;
static HashSet s = new HashSet();
// Function to generate the Prime numbers
// and store their products
static void SieveOfEratosthenes()
{
// Create a boolean array "prime[0..n]" and
// initialize all entries it as true.
// A value in prime[i] will finally be false
// if i is Not a prime, else true.
boolean []prime = new boolean[MAX];
Arrays.fill(prime, true);
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p < MAX; p++)
{
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * 2; i < MAX; i += p)
prime[i] = false;
}
}
// store prefix product of prime numbers
// to unordered_set 's'
int product = 1;
for (int p = 2; p < MAX; p++)
{
if (prime[p])
{
// update product by multipying
// next prime
product = product * p;
// insert 'produc+1' to set
s.add(product + 1);
}
}
}
// Function to check the number for Euclid Number
static boolean isEuclid(int n)
{
// Check if number exist in
// unordered set or not
// If exist, return true
if (s.contains(n))
return true;
else
return false;
}
// Driver code
public static void main(String[] args)
{
// Get the prime numbers
SieveOfEratosthenes();
// Get n
int n = 31;
// Check if n is Euclid Number
if (isEuclid(n))
System.out.println("Yes");
else
System.out.println("No");
// Get n
n = 42;
// Check if n is Euclid Number
if (isEuclid(n))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to check Euclid Number
MAX = 10000
s = set()
# Function to generate the Prime numbers
# and store their products
def SieveOfEratosthenes():
# Create a boolean array "prime[0..n]"
# and initialize all entries it as true.
# A value in prime[i] will finally be
# false if i is Not a prime, else true.
prime = [True] * (MAX)
prime[0], prime[1] = False, False
for p in range(2, 100):
# If prime[p] is not changed,
# then it is a prime
if prime[p] == True:
# Update all multiples of p
for i in range(p * 2, MAX, p):
prime[i] = False
# store prefix product of prime numbers
# to unordered_set 's'
product = 1
for p in range(2, MAX):
if prime[p] == True:
# update product by multipying
# next prime
product = product * p
# insert 'produc+1' to set
s.add(product + 1)
# Function to check the number
# for Euclid Number
def isEuclid(n):
# Check if number exist in
# unordered set or not
# If exist, return true
if n in s:
return True
else:
return False
# Driver code
if __name__ == "__main__":
# Get the prime numbers
SieveOfEratosthenes()
# Get n
n = 31
# Check if n is Euclid Number
if isEuclid(n) == True:
print("YES")
else:
print("NO")
# Get n
n = 42
# Check if n is Euclid Number
if isEuclid(n) == True:
print("YES")
else:
print("NO")
# This code is contributed by Rituraj Jain
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
static int MAX = 10000;
static HashSet s = new HashSet();
// Function to generate the Prime numbers
// and store their products
static void SieveOfEratosthenes()
{
// Create a boolean array "prime[0..n]" and
// initialize all entries it as true.
// A value in prime[i] will finally be false
// if i is Not a prime, else true.
Boolean []prime = new Boolean[MAX];
for (int p = 0; p < MAX; p++)
prime[p] = true;
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p < MAX; p++)
{
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * 2; i < MAX; i += p)
prime[i] = false;
}
}
// store prefix product of prime numbers
// to unordered_set 's'
int product = 1;
for (int p = 2; p < MAX; p++)
{
if (prime[p])
{
// update product by multipying
// next prime
product = product * p;
// insert 'produc+1' to set
s.Add(product + 1);
}
}
}
// Function to check the number
// for Euclid Number
static Boolean isEuclid(int n)
{
// Check if number exist in
// unordered set or not
// If exist, return true
if (s.Contains(n))
return true;
else
return false;
}
// Driver code
public static void Main(String[] args)
{
// Get the prime numbers
SieveOfEratosthenes();
// Get n
int n = 31;
// Check if n is Euclid Number
if (isEuclid(n))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
// Get n
n = 42;
// Check if n is Euclid Number
if (isEuclid(n))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by Princi Singh
输出:
YES
NO
注意:以上方法将花费O(1)时间来回答查询。