一定数以下的圆素数
Circular primes under a certain number
我写了一些代码来查找某个数字的循环素数,它自己工作得很好-
import sympy as sp
PrimeCount=0
i=0
Remainder=0
PrimeSum=0
N=input("Enter an integer number=")
length=len(N)
n=int(N)
while(i<length):
Remainder=int(n % 10)
n=int(n / 10)
n=int((Remainder * (10 ** (length - 1)) + n))
PrimeCount=(sp.isprime(n))
if PrimeCount==False:
PrimeCount=0
else:
PrimeCount=1
PrimeSum=PrimeSum+PrimeCount
i+=1
if(PrimeSum==length):
print(N,"is a circular prime")
else:
print(N,"is not a circular prime")
然而,当我实现相同的代码以便它为我提供输入数字下的所有循环素数时,它没有按预期工作,因为它一直说唯一的循环素数是 2 和 11。我真的不知道为什么这对我不起作用。这是我遇到问题的代码-
import sympy as sp
PrimceCount=0
i=0
Remainder=0
PrimeSum=0
mx=int(input())
SecondList=[]
Circ=[]
FirsList=list(range(0,(mx+1)))
for n1 in FirsList:
if sp.isprime(n1)==True:
SecondList.append(n1)
print(SecondList)
for n2 in SecondList:
length=len(str(n2))
n1=(n2)
while(i<length):
Remainder=int(n1 % 10)
n1=int(n1 / 10)
n1=int((Remainder * (10 ** (length - 1)) + n1))
PrimeCount=(sp.isprime(n1))
if PrimeCount==False:
PrimeCount=0
else:
PrimeCount=1
PrimeSum=PrimeSum+PrimeCount
i+=1
if(PrimeSum==length):
Circ.append(n2)
enum=len(Circ)
print('Circ=',Circ)
print('There are',enum,'circular primes below', mx)
这里有一些要使用的代码:
def cyclic_permutations_of_string(s: str) -> list[str]:
return [
s[i:] + s[:i]
for i in range(len(s))
]
def cyclic_permutations_of_number(n: int) -> list[int]:
return [int(p) for p in cyclic_permutations_of_string(str(n))]
def is_prime(n: int) -> bool:
i = 2
while i**2 <= n:
if n % i == 0:
return False
i += 1
return True
def is_circular_prime(n: int) -> bool:
return all(is_prime(k) for k in cyclic_permutations_of_number(n))
您需要为检查的每个新质数重置 i
和 primeSum
:
for n2 in SecondList:
PrimeSum=0 # Reset PrimeSum
length=len(str(n2))
n1=(n2)
i=0 # Reset i
while(i<length):
Remainder=int(n1 % 10)
n1=int(n1 / 10)
n1=int((Remainder * (10 ** (length - 1)) + n1))
PrimeCount=(sp.isprime(n1))
if PrimeCount==False:
PrimeCount=0
else:
PrimeCount=1
PrimeSum=PrimeSum+PrimeCount
i+=1
if(PrimeSum==length):
Circ.append(n2)
我写了一些代码来查找某个数字的循环素数,它自己工作得很好-
import sympy as sp
PrimeCount=0
i=0
Remainder=0
PrimeSum=0
N=input("Enter an integer number=")
length=len(N)
n=int(N)
while(i<length):
Remainder=int(n % 10)
n=int(n / 10)
n=int((Remainder * (10 ** (length - 1)) + n))
PrimeCount=(sp.isprime(n))
if PrimeCount==False:
PrimeCount=0
else:
PrimeCount=1
PrimeSum=PrimeSum+PrimeCount
i+=1
if(PrimeSum==length):
print(N,"is a circular prime")
else:
print(N,"is not a circular prime")
然而,当我实现相同的代码以便它为我提供输入数字下的所有循环素数时,它没有按预期工作,因为它一直说唯一的循环素数是 2 和 11。我真的不知道为什么这对我不起作用。这是我遇到问题的代码-
import sympy as sp
PrimceCount=0
i=0
Remainder=0
PrimeSum=0
mx=int(input())
SecondList=[]
Circ=[]
FirsList=list(range(0,(mx+1)))
for n1 in FirsList:
if sp.isprime(n1)==True:
SecondList.append(n1)
print(SecondList)
for n2 in SecondList:
length=len(str(n2))
n1=(n2)
while(i<length):
Remainder=int(n1 % 10)
n1=int(n1 / 10)
n1=int((Remainder * (10 ** (length - 1)) + n1))
PrimeCount=(sp.isprime(n1))
if PrimeCount==False:
PrimeCount=0
else:
PrimeCount=1
PrimeSum=PrimeSum+PrimeCount
i+=1
if(PrimeSum==length):
Circ.append(n2)
enum=len(Circ)
print('Circ=',Circ)
print('There are',enum,'circular primes below', mx)
这里有一些要使用的代码:
def cyclic_permutations_of_string(s: str) -> list[str]:
return [
s[i:] + s[:i]
for i in range(len(s))
]
def cyclic_permutations_of_number(n: int) -> list[int]:
return [int(p) for p in cyclic_permutations_of_string(str(n))]
def is_prime(n: int) -> bool:
i = 2
while i**2 <= n:
if n % i == 0:
return False
i += 1
return True
def is_circular_prime(n: int) -> bool:
return all(is_prime(k) for k in cyclic_permutations_of_number(n))
您需要为检查的每个新质数重置 i
和 primeSum
:
for n2 in SecondList:
PrimeSum=0 # Reset PrimeSum
length=len(str(n2))
n1=(n2)
i=0 # Reset i
while(i<length):
Remainder=int(n1 % 10)
n1=int(n1 / 10)
n1=int((Remainder * (10 ** (length - 1)) + n1))
PrimeCount=(sp.isprime(n1))
if PrimeCount==False:
PrimeCount=0
else:
PrimeCount=1
PrimeSum=PrimeSum+PrimeCount
i+=1
if(PrimeSum==length):
Circ.append(n2)