結果

問題 No.713 素数の和
ユーザー NoviNovi
提出日時 2020-04-07 23:41:08
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 791 bytes
コンパイル時間 232 ms
コンパイル使用メモリ 10,952 KB
実行使用メモリ 8,972 KB
最終ジャッジ日時 2023-09-22 18:01:51
合計ジャッジ時間 1,098 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
8,780 KB
testcase_01 AC 19 ms
8,972 KB
testcase_02 AC 19 ms
8,716 KB
testcase_03 AC 25 ms
8,744 KB
testcase_04 AC 30 ms
8,892 KB
testcase_05 AC 22 ms
8,952 KB
testcase_06 AC 19 ms
8,712 KB
testcase_07 AC 18 ms
8,912 KB
testcase_08 AC 22 ms
8,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import random

def is_prime3(q, k=50):
    q = abs(q)
    # 計算するまでもなく判定できるものははじく
    if q == 2: return True
    if q < 2 or q & 1 == 0: return False

    # n-1=2^s*dとし(但しaは整数、dは奇数)、dを求める
    d = (q - 1) >> 1
    while d & 1 == 0:
        d >>= 1

    # 判定をk回繰り返す
    for i in range (0,k):
        a = random.randint(1, q - 1)
        t = d
        y = pow(a, t, q)
        # [0,s-1]の範囲すべてをチェック
        while t != q - 1 and y != 1 and y != q - 1:
            y = pow(y, 2, q)
            t <<= 1
        if y != q - 1 and t & 1 == 0:
            return False
    return True

n=int(input())
ans=[]
for i in range(1,n+1):
    if is_prime3(i):
        ans.append(i)

print(sum(ans))
0