I wrote some code to find the circular prime numbers of a certain number which works fine on its own-
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")
However when I implement the same code so that it gives me all the circular primes under an inputed number, it not working as intended as it keeps saying the only circular primes are 2 and 11. I genuinely don't know why this isn't working for me. Here's the code I'm having trouble with-
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)
CodePudding user response:
You need to reset i
and primeSum
for every new prime you check:
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)
CodePudding user response:
Here's some code to use:
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))