結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー こまるこまる
提出日時 2020-12-01 12:57:30
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
RE  
実行時間 -
コード長 748 bytes
コンパイル時間 180 ms
コンパイル使用メモリ 10,948 KB
実行使用メモリ 8,020 KB
最終ジャッジ日時 2023-08-11 23:28:44
合計ジャッジ時間 966 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

def miller_rabin(n, check):
  d, s = n - 1, 0
  while d % 2 == 0:
    d >>= 1
    s += 1
  for a in check:
    if n <= a:
      return True
    a = pow(a, d, n)
    if a == 1:
      continue
    r = 1
    while a != n - 1:
      if r == s:
        return False
      a = a * a % n
      r += 1
  return True

def is_prime32(n):
  miller_rabin(n, [2, 7, 61])
def is_prime64(n):
  miller_rabin(n, [2, 3, 5, 7, 325, 9375, 28178, 450775, 9780504, 1795265022])


def is_prime(n):
  if n <= 1:
    return False
  if n <= 3:
    return True
  if n % 2 == 0:
    return False
  if n < 4759123141:
    return is_prime32(n)
  if n < 18446744073709551615:
    return is_prime64(n)

for i in range(int(input())):
  x = int(input())
  print(x, int(is_prime(x)))
0