0
0

More than 1 year has passed since last update.

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

Posted at

タスク概要

Goldbach's Conjecture

コード実装例

TIPS

  1. 例外処理含む評価パターンを追加
import pprint, sys, time
import sympy

def core(arg, adv=True, n_eg=3):
    n = arg
    if adv:
        ip = [[i, n - i] for i in sympy.primerange(2, int(n/2) + 1) if sympy.isprime(n - i)]
    else:
        ip = [[i, n - i] for i in range(n) if sympy.isprime(i) and sympy.isprime(n - i) and i <= n - i]
    ret = [len(ip), ip[:n_eg]]
    return ret

def app(*args):
    ret = []
    for arg in args:
        s = []
        for adv in [False, True]:
            st = time.time()
            try:
                r = core(arg, adv=adv)
            except Exception as e:
                r = e
            s.append([adv, round(time.time() - st, 6), r])
        ret.append([arg, s])
    return ret

print(sys.version)
pprint.pprint(app(
    # basic examples
    10,
    11,
    # additional examples
    50000,
    50001,
    # exceptional examples
    "例外入力"
))

実行結果

各種補足情報出力(入力値FB、デバッグログ等)含む

3.8.10 (default, Sep 28 2021, 16:10:42) 
[GCC 9.3.0]
[[10,
  [[False, 1.9e-05, [2, [[3, 7], [5, 5]]]],
   [True, 0.000298, [2, [[3, 7], [5, 5]]]]]],
 [11, [[False, 1e-05, [0, []]], [True, 1.2e-05, [0, []]]]],
 [50000,
  [[False, 0.059492, [450, [[7, 49993], [43, 49957], [61, 49939]]]],
   [True, 0.018928, [450, [[7, 49993], [43, 49957], [61, 49939]]]]]],
 [50001,
  [[False, 0.05523, [1, [[2, 49999]]]], [True, 0.014913, [1, [[2, 49999]]]]]],
 ['例外入力',
  [[False,
    5e-06,
    TypeError("'str' object cannot be interpreted as an integer")],
   [True,
    4e-06,
    TypeError("unsupported operand type(s) for /: 'str' and 'int'")]]]]

補遺

残課題(Pull Request絶賛募集中!)

  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