0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AOJトライに関する知識知見の記録共有:Volume0-0054

Posted at

タスク概要

Sum of Nth decimal places

コード実装例

TIPS

  1. 例外処理含む評価パターンを追加
  2. 素数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")]]

補遺

残課題

  1. 実行速度の改善(枝刈り、データ構造の見直し等)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?