タスク概要
Sum of Nth decimal places
コード実装例
TIPS
- 例外処理含む評価パターンを追加
- 素数5に着目し高速化
import pprint, sys, time
import sympy
def core(arg, acc=True):
if acc:
prime_factors = {n: sympy.factorint(n) for n in range(2, arg) if n % 5 == 0}
ret = [sum([prime_factors[v][5] for v in prime_factors if 5 in prime_factors[v]])]
else:
prime_factors = {n: sympy.factorint(n) for n in range(2, arg)}
ret = [sum([prime_factors[v][n] for v in prime_factors if n in prime_factors[v]]) for n in [2, 5]]
return ret
def app(*args):
ret = []
for arg in args:
st = time.time()
try:
r = core(arg)
except Exception as e:
r = e
ret.append([arg, round(time.time() - st, 6), r])
return ret
print(sys.version)
pprint.pprint(app(
# basic examples
2, 12, 10000,
# additional examples
4,
# exceptional examples
"例外入力"
))
実行結果
各種補足情報出力(入力値FB、デバッグログ等)含む
3.8.10 (default, Jun 2 2021, 10:49:15)
[GCC 9.4.0]
[[2, 5e-06, [0]],
[12, 2.6e-05, [2]],
[10000, 0.009006, [2495]],
[4, 5e-06, [0]],
['例外入力', 4e-06, TypeError("'str' object cannot be interpreted as an integer")]]
補遺
残課題
- 実行速度の改善(枝刈り、データ構造の見直し等)