我试图在两个整数之间产生主要的回文(由用户给出),并且无法弄清楚我出错的地方。请有人解释一下吗?
这是我到目前为止所得到的:
#Palindrome test
def palindrome(num):
str(num) == str(num)[::-1]
#Prime test (excl. 3, 5 & 7 because not palindrome)
def prime(num):
abs(num)%2 != 0
abs(num)%3 != 0
abs(num)%5 != 0
abs(num)%7 != 0
N = int(input("Enter the start point N: "))
M = int(input("Enter the end point M: "))
for x in range(N, M):
if palindrome(x) and prime(x):
print(x)
好的,正如所指出的那样 - 上面的代码中存在多个问题。我再次尝试并提出这个(下面),它设法打印出所有的回文,但也包括非素数。我哪里错了?
N = int(input("Enter the start point N: "))
M = int(input("Enter the end point M: "))
def palindrome(num):
return str(num) == str(num)[::-1]
def prime(x):
for x in range(N, M+1):
for y in range(2, x):
if x%y != 0:
return True
for z in range(N, M):
if palindrome(z) and prime(z):
print(z)
感谢目前为止的所有反馈!
答案 0 :(得分:0)
看看是否有效
M-x view-echo-area-messages RET
BTW - 143不是素数,但您的主要测试将返回true。不确定这是否符合设计要求。
编辑:是的我知道主要测试是不正确的。我只是按照他使用它的方式使用了OP的代码。
尝试使用它来检查素数
#Palindrome test
def palindrome(num):
return str(num) == str(num)[::-1]
#Prime test (excl. 3, 5 & 7 because not palindrome)
def prime(num):
if abs(num)%2 != 0 and abs(num)%3 != 0 and abs(num)%5 != 0 and abs(num)%7 != 0:
return True
return False
答案 1 :(得分:0)
我进一步采用了@Banach
代码。对质数的测试进行了更改以适应可由素数本身整除的数字。
来自here的灵感。
主要测试现在适用于更大的数字。
演示代码
#Palindrome test
def palindrome(num):
return str(num) == str(num)[::-1]
#Prime Test
def is_prime(n):
if n == 2 or n == 3: return True
if n < 2 or n%2 == 0: return False
if n < 9: return True
if n%3 == 0: return False
r = int(n**0.5)
f = 5
while f <= r:
if n%f == 0: return False
if n%(f+2) == 0: return False
f +=6
return True
#Get user input
N = int(input("Enter the start point N: "))
M = int(input("Enter the end point M: "))
for x in range(N, M):
#Check for palindrome and prime number
if palindrome(x) and is_prime(x):
#Omitting 2,3,5,7 as per request
if abs(x)%2 != 0 and abs(x)%3 != 0 and abs(x)%5 != 0 and abs(x)%7 != 0:
print(x)
<强>输出强>
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Enter the start point N: 2
Enter the end point M: 20000
11
101
131
151
181
191
313
353
373
383
727
757
787
797
919
929
10301
10501
10601
11311
11411
12421
12721
12821
13331
13831
13931
14341
14741
15451
15551
16061
16361
16561
16661
17471
17971
18181
18481
19391
19891
19991
>>>
答案 2 :(得分:0)
这似乎是一个有趣的问题。如果你有像Miller Selfridge Rabin测试那样的工业强度测试,那么你可以找到一些真实的大例子(尽管获得复合材料的可能性很小):
import random
#The following function finds s and d in
#n-1 = 2^s*d with d odd
def findSD(n):
s = 0
d = n-1
while d % 2 == 0:
s = s + 1
d = d//2
return s,d
def checkBase(a,n):
s,d = findSD(n)
x = pow(a,d,n)
if x == 1 or x == n-1:
return "probable prime"
else:
for i in range(s-1):
x = pow(x,2,n)
if x == 1:
return "composite"
elif x == n-1:
return "probable prime"
#if you get to this stage, -1 not reached despite s-1
#squarings -- so must be composite
return "composite"
def MSR(n,k):
#tests if an odd number is prime
for i in range(k):
a = random.randint(2,n-2)
if checkBase(a,n) == "composite":
return "composite"
#if you get here n has survived k potential witnesses, so
return "probable prime"
def prime(n):
smallPrimes = [2,3,5,7,11,13,17,19]
for p in smallPrimes:
if n == p:
return True
elif n % p == 0:
return False
if MSR(n,20) == "composite":
return False
else:
return True
def randOddPalindrome(n):
digits = [random.choice('13579')]
if n > 1:
m = (n-2)//2
digits.extend(random.choice('0123456789') for i in range(m))
digit = [''] if n%2 == 0 else [random.choice('0123456789')]
digits += (digit + digits[::-1])
return int(''.join(digits))
def findPrimePalindrome(n,trials = 10000):
for i in range(trials):
p = randOddPalindrome(n)
if prime(p): return p
return "No primes found in " + str(trials) + " trials"
例如,
>>> findPrimePalindrome(5)
30803
>>> findPrimePalindrome(6)
'No primes found in 10000 trials'
>>> findPrimePalindrome(101)
10411470210071416936952003803463770311632555360794349706355523611307736430830025963961417001207411401
当我看到n=6
的输出时,我简短地怀疑是一个错误,但后来意识到11是唯一的主要回文,其数字为偶数,因为所有回文数均为偶数数字是11的倍数。