有人可以在Python上解决此问题吗?
如果一个正整数m可以写为p + q,其中p> 0,q> 0且p和q均为质数,则可以将其划分为素数。
编写一个Python函数,将整数m作为输入,如果m可以被分割为素数,则返回True,否则返回False。
对此进行了尝试,但不适用于所有测试用例,例如对于3432,它应该返回True,而它返回False。
def partition(num):
primelist=[]
for i in range(2,num + 1):
for p in range(2,i):
if (i % p) == 0:
break
else:
primelist.append(i)
for x in primelist:
y= num-x
for z in range(2,y):
if y%z == 0:
return False
return True
答案 0 :(得分:1)
错误在于第二个for循环中。您正在遍历可能的素数x
,然后希望检查y = num - x
也是素数。
您的逻辑错误是,在第二个for循环中,如果循环y = num - x
中的第一个元素不是素数,它将返回False
,而不检查任何其他可能的值。
您可以通过将return False
语句移出一个循环来更正此问题,但是由于您已经生成了一个小于num
,primelist
(并且自{{1} },(如果存在素数y = num - x
,它将在此列表中),您只需检查该列表的成员身份即可:
y
注意:我建议您使脚本的逻辑更具模块化,将素数列表生成器分离为自己的函数:
for x in primelist:
y= num-x
# Note: num = x + y, thus need only check y prime
if y in primelist:
return True
# If no such y is prime, not possible
else:
return False
答案 1 :(得分:1)
另一种方法, 最初,我们存储直到 m 的所有素数元素并检查总和等于 m
的素数对def primepartition(a):
l=[2]#since 'a' should be >=2 for below loops, we took here 2(1st prime).
for i in range(2,a):
flag=0
for j in range(2,i):
if i%j==0:
flag=0
break
else:
flag=1
if flag==1:
l.append(i)
for i in l:
for j in l:
if i+j==a:
return True
return False
答案 2 :(得分:1)
n=int(input("Enter any number: "))
list=[]
for num in range(0,n + 1):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
list.append(num)
if (n<= 1):
print("False")
#print("It is not positive ")
else:
for i in list:
y = num -i
if (y in list):
print("True")
#print(y,"+",i,"=",n)
#print(i,"+",y,"=",n)
#print("The number can be expressed as the sum of two prime numbers.")
break
else:
print("False")
#print("The number can not be expressed as the sum of two prime numbers.")
答案 3 :(得分:0)
我得到的最终解决方案:
def primepartition(m):
primelist=[]
if m<0:
return False
else:
for i in range(2,m + 1):
for p in range(2,i):
if (i % p) == 0:
break
else:
primelist.append(i)
for x in primelist:
y= m-x
if y in primelist:
return True
return False
答案 4 :(得分:0)
您的代码略有变化:
def primepartition0(m):
primelist=[]
if m<0:
return False
else:
for i in range(2,m + 1):
for p in range(2,i):
if (i % p) == 0:
break
else:
primelist.append(i)
for x in primelist:
for y in primelist:
if x != y and x+y == m:
return True
return False
答案 5 :(得分:0)
下面给出的代码有望为您提供正确的输出。
def factors(n):
factorslist = []
for i in range(1, n+1, 1):
if n % i == 0:
factorslist.append(i)
return(factorslist)
def prime(n):
if factors(n) == [1, n] and n > 1:
return(True)
def primelist(n):
primenolist = []
for i in range(1, n+1, 1):
if prime(i) == True:
primenolist.append(i)
return(primenolist)
def primepartition(m):
if m > 0:
primenolist = primelist(m)
checklist = []
for p in primenolist:
q = m - p
if q in primenolist and p > 0 and q > 0:
checklist.append((p,q))
if len(checklist) > 0:
return(True)
else:
return(False)
else:
return(False)
答案 6 :(得分:0)
另一种尝试减少所需代码量的方法:
def primepartition(m):
if m > 3:
for number in range(m // 2, m - 1):
difference = m - number
for psuedoprime in range(2, int(number ** 0.5) + 1):
if number % psuedoprime == 0 or difference > psuedoprime and difference % psuedoprime == 0:
break
else: # no break
return number, difference # as good a non-False result as any other...
return False
答案 7 :(得分:0)
如果您不需要生成实际的素数,而只需要测试是否存在一对素数 p 和 q 使得 p+q == N,您可以根据哥德巴赫猜想使这变得非常简单。所有偶数都可以表示为两个素数之和。因此,如果数字是偶数,则返回 True 并检查 N-2 是否是奇数的素数(因为 2 是唯一的偶数素数,并且这是从奇数开始时会产生另一个奇数的唯一素数)。这将归结为仅针对奇数的 N-2 的单个素数测试。
def primePart(N):
return N%2==0 or all((N-2)%p for p in range(3,int(N**0.5)+1,2))
primePart(3432) # True
primePart(37+2) # True
primePart(13+41) # True
primePart(123) # False
如果您想实际找到一对加起来为 N 的素数,您可以生成最多为 N 的素数并返回第一个素数 >= N/2 其中 N - 素数是已经找到的素数之一:>
def findPQ(N):
if not primePart(N): return
if N%2: return 2,N-2
isPrime = [0]+[1]*N
for p in range(3,N,2):
if not isPrime[p]: continue
if 2*p>=N and isPrime[N-p]: return p,N-p
isPrime[p*p::p] = [0]*len(isPrime[p*p::p])
输出:
findPQ(3432) # (1723, 1709)
findPQ(12345678) # (6172879, 6172799)
要超过 10^9,您将需要一个比 Eratosthenes 筛法更快的内存效率更高的算法。这可以通过跳过素数倍数的字典来实现:
def findPQ(N):
if not primePart(N): return
if N%2: return 2,N-2
skip,primes = {},{2}
for p in range(3,N,2):
if p in skip:
prime = skip.pop(p)
mult = p + 2*prime
while mult in skip: mult += 2*prime
if mult <= N: skip[mult] = prime
else:
if 2*p>=N and N-p in primes: return p,N-p
if p*p<=N: skip[p*p]=p
if 2*p<=N: primes.add(p)
输出(需要一段时间但不会破坏内存空间):
findPQ(1234567890) # (617283983, 617283907)
答案 8 :(得分:0)
def factors(n):
factlist = []
for i in range(1,n+1):
# Since factors of 2 cannot be primes, we ignore them.
if n%i==0 and i%2!=0:
factlist.append(i)
return factlist
def isprime(n):
return(factors(n)==[1,n])
def preimesupto(n):
primelist = []
if n>=2:
primelist.append(2)
for i in range(n):
if isprime(i):
primelist.append(i)
return primelist
def primepartition(n):
if n<0:
return False
primelist = preimesupto(n)
for i in primelist:
j = n-i
if j in primelist:
return True
else:
return False
答案 9 :(得分:-2)
def checkprime(number):
fact=1
for r in range(2,number):
if number%r==0:
fact=fact+1
return(fact<2)
def primepartition(m):
for i in range(2,m):
flag=0
if checkprime(i) and checkprime(m-i)==True:
flag=1
break
return(flag==1)
def matched(s):
list_of_string=list(s)
for y in range(len(list_of_string)):
if list_of_string[y]=='(':
for z in range(y,len(list_of_string)):
if list_of_string[z]==')':
list_of_string[y]='@'
list_of_string[z]='@'
break
return('('not in list_of_string and ')'not in list_of_string)
def rotatelist(l,k):
if k>len(l):
k=int(k%len(l))
return(l[k:]+l[0:k])