[英]How can i make my code run faster for large values(upto 100 million)?
Okay so background: I'm solving a question which requires me to find a number 'n' such that n-9, n-3, n+3, n+9 are consecutive prime numbers and n-8, n-4, n+4, n+8 are practical numbers and then I have to add the first four n that satisfy this condition.好的,背景:我正在解决一个问题,该问题需要我找到一个数字“n”,使得 n-9、n-3、n+3、n+9 是连续的素数,而 n-8、n-4、 n+4, n+8 是实际数字,然后我必须添加满足此条件的前四个 n。
The problem: Whether the logic of the code is correct or incorrect is irrelevant here because my code crashes before it reaches 100 million.问题:代码的逻辑正确与否在这里无关紧要,因为我的代码在达到1亿之前就崩溃了。 I can't even check the output of the code, it works fine for 1million but doesn't scale to well for larger numbers.
我什至无法检查代码的 output,它适用于 100 万,但不能很好地扩展到更大的数字。
What i did: I used the sieve of erath... to get the prime numbers up to 100 million which we will call M. And since practical numbers are divisible by 6 or by 4, I created another set to store those numbers and from that list i then created a set that contained the numbers that satisfy this condition: 'n-8, n-4, n+4, n+8 are practical numbers' which we will call N. Finally I iterate through each element, a, in N and check whether a - 9, a - 3, a + 3, a + 9 are part of the prime number set.我做了什么:我使用 erath 的筛子... 将质数提高到 1 亿,我们将其称为 M。由于实际数字可以被 6 或 4 整除,我创建了另一个集合来存储这些数字并从然后我创建了一个包含满足此条件的数字的集合:'n-8,n-4,n+4,n+8 是实际数字',我们将其称为 N。最后,我遍历每个元素,a , 在 N 中并检查 a - 9, a - 3, a + 3, a + 9 是否是素数集的一部分。
If anyone has any tips on how i can speed this up or any better algorithms it would be greatly appreciated如果有人对我如何加快速度或任何更好的算法有任何提示,将不胜感激
code代码
def SieveOfEratosthenes(n):
m = set()
prime = [True for i in range(n + 1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 1
prime[0]= False
prime[1]= False
for p in range(n + 1):
if prime[p]:
m.add(p)
return m
#creates set that stores multiples of 4 and 6
def ps1(n):
s = set()
for i in range(1, n+1):
if i%4 == 0 and i%6 == 0:
s.add(i)
return s
#checks whether any number satisfies n -8, n-4, n+4, n+8 must be practical and stores it in a set
def ps2(l):
q = set()
for i in l:
if ((i-8) in l) and ((i-4) in l) and ((i+4) in l) and ((i+8) in l):
q.add(i)
return q
#using the numbers stored in the prev set, i check the last condition n-9, n-3, n+3, n+9 must be in the
prime list
def TotalSieve(k, l):
q = set()
inc = 0
for i in k:
if inc != 4:
if ((i-9) in l) and ((i-3) in l) and ((i+3) in l) and ((i+9) in l):
inc = inc + 1
q.add(i)
else:
print("Found 4")
return q
# driver program
if __name__=='__main__':
n = 1000000000
m = SieveOfEratosthenes(n)
p = ps1(n)
p = ps2(p)
f = TotalSieve(p, m)
elem1 = f.pop()
elem2 = f.pop()
elem3 = f.pop()
elem4 = f.pop()
#add the first four numbers that satisfy the conditions
tot = elem1 + elem2 + elem3 + elem4
print(tot)
First, ps1
is wrong.首先,
ps1
是错误的。 The test should say or
, not and
.测试应该说
or
,而不是and
。
Next, if n
is divisible by 4, all n-8, n-4, n+4, n+8
are also divisible by 4. If n
is not divisible by 4, none of them are divisible by 4, and some of them are also not divisible by 4. Which means you are only interested in n
being a multiple of 4.接下来,如果
n
能被 4 整除,则所有n-8, n-4, n+4, n+8
也能被 4 整除。如果n
不能被 4 整除,则它们都不能被 4 整除,还有一些它们也不能被 4 整除。这意味着您只对n
是 4 的倍数感兴趣。
Finally, I know that this problem implies some serious number-theoretical homework.最后,我知道这个问题意味着一些严肃的数论作业。 Brute force wouldn't do it.
蛮力是做不到的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.