結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
siman
|
| 提出日時 | 2020-01-14 18:38:00 |
| 言語 | Ruby (3.4.1) |
| 結果 |
AC
|
| 実行時間 | 4,956 ms / 9,973 ms |
| コード長 | 852 bytes |
| コンパイル時間 | 36 ms |
| コンパイル使用メモリ | 7,296 KB |
| 実行使用メモリ | 19,200 KB |
| 最終ジャッジ日時 | 2024-11-16 23:21:23 |
| 合計ジャッジ時間 | 11,793 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
コンパイルメッセージ
Syntax OK
ソースコード
require 'openssl'
def modular_exponentiation(base, n, mod)
res = 1
while n > 0
if n[0] == 1
res = (res * base) % mod
end
base = (base ** 2) % mod
n >>= 1
end
res
end
def witness(a, n)
v = n - 1
t = 0
while v % 2 == 0
t += 1
v /= 2
end
u = n / (2 ** t)
x = modular_exponentiation(a, u, n)
t.times do
bx = x
x = (x ** 2) % n
if x == 1 && bx != 1 && bx != n - 1
return true
end
end
return true if x != 1
false
end
def miller_rabin(n, s = 10)
return false if n <= 1
return true if n == 2
return false if n.even?
s.times do
a = rand(n - 1) + 1
return false if witness(a, n)
end
true
end
N = gets.to_i
N.times do
x = gets.to_i
bn = OpenSSL::BN.new(x)
if bn.prime?
puts [x, 1].join(' ')
else
puts [x, 0].join(' ')
end
end
siman