2
私はnに乗、番目の保番号(数字、元の番号(例:625 = 390625、390625パーセント1000年= 625で終わりを解決するためのプログラムを書きました)。貧しいコード、初年度コンプサイのため申し訳ありません長い実行時間
import time
def green(n):
start_time = time.time()
f = 3
if n==1:
return 1
elif n==2:
return 5
elif n==3:
return 6
n1 = "5"
n2 = "6"
tempn2 = "0"
tempn1 = "0"
x = 1
while f!=n+1:
if int(n1) > int(n2):
tempn2 = str(x) + n2
while int(pow(int(tempn2), 2, 10**(len(tempn2)))) != int(tempn2):
tempn2 = str(x) + n2
x+=1
x=1
f+=1
n2 = tempn2
if f==n+1:
break
else:
tempn1 = str(x) + n1
while int(pow(int(tempn1), 2, 10**len(tempn1))) != int(tempn1):
tempn1 = str(x) + n1
x+=1
x=1
f+=1
n1 = tempn1
print("--- %s seconds ---" % (time.time() - start_time))
return min(int(n1), int(n2))
。
私はで目5000 が必要12秒未満の実行時間にする。現在のコードは約45秒かかります。
pow関数を使用して、10のべき乗を計算して計算を高速化できます。 'pow(n、2、10)'はn平方根の最後の桁を返します –
@DanielGee残念なことに実行時の変更はありませんが、コードを凝縮しました。 – Rxted
この質問は、https://codereview.stackexchange.com – ti7