Only complete nthCircularPrime. assume all other methods are implemented correct
ID: 3912746 • Letter: O
Question
Only complete nthCircularPrime. assume all other methods are implemented correctly. Do not use string indexing, lists, or recursion in this assignment. Python languange
2. nthCircularPrime [70 pts] A circular prime is an integer number with the property that any rotation of that number's digits is prime. In this case, rotation refers to cycling the digits of a number; for example, the rotations of 1234 are 1234, 2341, 3412, and 4123. You can read more about this on the Wikipedia page. Single-digit primes are all circular, of course. To find the nth circular prime, you'll need to write isPrime and three other functions: 1. rotateNumber [20 pts] number's digits by one would turn the number 1234 to 4123. 2. isCircularPrime [30 pts] This function takes a non-negative integer number, x, and determines whether that number is a circular prime. To do this, you'll need to check whether every rotation of the number is prime. 3. nthCircularPrime [20 pts] This function takes a non-negative integer number n, and returns the nth circular prime.Explanation / Answer
def isPrime(n):
if (n < 2):
return False
if (n == 2):
return True
if (n % 2 == 0):
return False
maxFactor = round(n**0.5)
for factor in range(3,maxFactor+1,2):
if (n % factor == 0):
return False
return True
def digCount(x):
count = 0
while (x >= 10):
x //= 10
count += 1
return count
def rotateNumber(x):
numDigits = digCount(x)
lastDigit = x % 10
first = (x - lastDigit) / 10
last = lastDigit * 10**numDigits
return last + first
def isCircularPrime(x):
for i in range(digCount(x)+1):
#print(x)
if (not isPrime(x)):
return False
x = rotateNumber(x)
return True
def nthCircularPrime(n):
if n >= 0:
num = 2
count = 0;
while count < n:
num = num + 1
if isCircularPrime(num):
#print(count,num)
count = count + 1
return num
else:
print("Invalid argument")
return -1
#print(isPrime(32))
#print(isCircularPrime(23))
#print(rotateNumber(23))
print(nthCircularPrime(4))
print(nthCircularPrime(5))
print(nthCircularPrime(11))
print(nthCircularPrime(15))
print(nthCircularPrime(25))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.