タスク概要
Prime Number II
コード実装例
TIPS
- 例外処理含む評価パターンを追加
- 入力値自体が素数にその旨出力
- 出力数をパラメータ変数指定し調整可能に
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. 実行速度の改善(枝刈り、データ構造の見直し等)