結果

問題 No.8030 ミラー・ラビン素数判定法のテスト
ユーザー こまる
提出日時 2020-12-01 13:00:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 466 ms / 9,973 ms
コード長 785 bytes
コンパイル時間 389 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 77,056 KB
最終ジャッジ日時 2024-11-16 23:34:21
合計ジャッジ時間 2,536 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

import os
import sys

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):
  return miller_rabin(n, [2, 7, 61])

def is_prime64(n):
  return 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