Я работаю над задачей Эйлера №650 в рамках проекта, и она генерирует правильный ответ для меньших значений, но они ищут решение для значений до 20 000. При той скорости, с которой сейчас работает мой код, это займет около миллиарда лет. Как можно улучшить этот код и сделать его эффективнее/быстрее?
Вот ссылка на проблему: https://projecteuler.net/problem=650
И код:
from scipy.special import comb
import math
import time
t0 = time.time()
def B(x):
#takes product of binomial coefficients
product = 1
for i in range(x + 1):
product *= comb(x, i, exact=True)
return product
def D(y):
#Sums all divisors of the product of binom. coefficients
x = B(y)
summation = []
for i in range(1, int(math.sqrt(x)) + 1):
if x % i == 0:
summation.append(i)
summation.append(x // i)
summation = list(dict.fromkeys(summation))
return sum(summation)
def S(z):
"""sums up all the sums of divisors of the product of the binomial
coefficients"""
sigma = []
for i in range(1, z + 1):
sigma.append(D(i))
return sum(sigma)
print(S(20000))
t1 = time.time()
total = t1 - t0
print(total)
Подробнее здесь: https://stackoverflow.com/questions/570 ... -euler-650
Ускорение моего решения для проекта Эйлера #650 ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение