私はプログラミングが初めてで、このウェブサイトも初めてです。だから申し訳ありませんが、私のコードがばかだと、私はあなたの時間を無駄にしています。私はProject Euler Problem 12を解決しようとしています。インターネットの助けを借りて、私はアルゴリズムを思いついて、Pythonでコードを書いた。私はそれを一般化して、500だけでなくすべての数字で動作するようにしました。最初は適切な出力を得ることに問題がありましたが、私はそれを修正したと思ったときにプログラムが永遠に実行されたために悪化しました。小さな入力でもPythonコードを実行するのはいつまでかかりますか?
L = int(input("L="))
def number_of_divisors(n):
global divisors
global count
global p
divisors = 1
count = 0
if n%2 == 0:
while n%2 == 0:
count += 1
n=n/2
divisors = divisors * count
count = 0
p = 3
while n != 1:
while n%p == 0:
count +=1
n = n/p
p += 2
divisors *= (count + 1)
return divisors
def the_first_triangular_number_with_more_than_L_divisors(L):
global total_divisors
global n
total_divisors , n = 1 , 1
while total_divisors <= L:
s = number_of_divisors(n+1)
total_divisors *= s
total_divisors = s
n += 1
return (n*(n+1))/2
x = the_first_triangular_number_with_more_than_L_divisors(L)
print(x)
は、なぜあなたはすぐにそれから* = S' 'total_divisorsをやっています'total_divisors = s'ですか? – user2357112
なぜ、n番目の三角形の数ではなく、nの除数の数をテストしていますか?あなたは本当にあなたの仕事を慎重に校正する必要があります。 – user2357112
私はそこに別の変数を使用するのを忘れていたと思います。私はtotal_divisors = sを書きました。なぜなら、sはn + 1の値であり、次のステップではnの値になります。 – user6708727