Woodall素数是素数,也是Woodall数。
查找小于N的Woodall素数
给定数字N ,打印所有小于或等于N的Woodall素数。
例如:
Input: N = 10
Output: 7
Input: N = 500
Output: 7, 23, 383
方法:这个想法是使用Eratosthenes筛子来检查数字是质数还是效率不高。然后,对从1到N的整数进行迭代,并对每个数字检查是否为质数,是否为Woodall数。如果数字是素数也是Woodall数,则它是Woodall素数。
下面是上述算法的实现:
Python3
# Python3 implementation to print all Woodall
# primes smaller than or equal to n.
# Function to check if a number
# N is Woodall
def isWoodall(x) :
# If number is even, return false.
if (x % 2 == 0) :
return False
# If x is 1, return true.
if (x == 1) :
return True
x = x + 1 # Add 1 to make x even
# While x is divisible by 2
p = 0
while (x % 2 == 0) :
# Divide x by 2
x = x / 2
# Count the power
p = p + 1
# If at any point power and
# x became equal, return true.
if (p == x) :
return True
return False
# Function to generate all primes and checking
# whether number is Woodall or not
def printWoodallPrimesLessThanN(n):
# 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] * (n + 1);
p = 2;
while (p * p <= n):
# If prime[p] is not changed,
# then it is a prime
if (prime[p]):
# Update all multiples of p
for i in range(p * 2, n + 1, p):
prime[i] = False;
p += 1;
# Print all Woodall prime numbers
for p in range(2, n + 1):
# checking whether the given number
# is prime Woodall or not
if (prime[p] and isWoodall(p)):
print(p, end = " ");
# Driver Code
n = 1000;
printWoodallPrimesLessThanN(n)
输出:
7 23 383