0
0

More than 1 year has passed since last update.

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

Last updated at Posted at 2021-11-19

タスク概要

Prime Number II

コード実装例

TIPS

  1. 例外処理含む評価パターンを追加
  2. 入力値自体が素数にその旨出力
  3. 出力数をパラメータ変数指定し調整可能に
import pprint, sys
import sympy 

def core(arg, n=3):
    r = [arg] if sympy.isprime(arg) else []
    return [sympy.prevprime(arg)] + r + [sympy.nextprime(arg, i) for i in range(1, n+1)]

def app(*args):
    ret = []
    for arg in args:
        try:
            r = core(arg)
        except Exception as e:
            r = e
        ret.append([arg, r])
    return ret

print(sys.version)
pprint.pprint(app(
    # basic examples
    19,
    3517,
    # additional examples
    7.1,
    # exceptional examples
    "例外入力"
))

実行結果

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

3.8.10 (default, Jun  2 2021, 10:49:15) 
[GCC 9.4.0]
[[19, [17, 19, 23, 29, 31]],
 [3517, [3511, 3517, 3527, 3529, 3533]],
 [7.1, [7, 11, 13, 17]],
 ['例外入力', ValueError('ceiling(例外入力) is not an integer')]]

補遺

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

0
0
1

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