Miller Rabin primality test

This commit is contained in:
2025-12-24 13:45:44 +01:00
parent d32a7d0f81
commit 0aa0fa2aa3
4 changed files with 41 additions and 10 deletions

View File

@@ -1,14 +1,6 @@
module TonelliShanks (tonelliShanks) where
import ModularArithmeticUtils (modExp, modMul, legendre)
-- Factor p-1 as q * 2^s, where q is odd
factorOutTwos :: Integer -> (Integer, Integer)
factorOutTwos n = go n 0
where
go x s
| even x = go (x `div` 2) (s + 1)
| otherwise = (x, s)
import ModularArithmeticUtils (modExp, modMul, legendre, factorOutTwos)
-- Tonelli-Shanks algorithm: find x such that x^2 = n (mod p)
-- Returns Just x if it exists, Nothing otherwise.