import strutils import sequtils import algorithm import bitops const Mod : int = 1_000_000_007 proc powMod*(n : int, x : int, m = Mod) : int = if x == 0: return 1 if n == 1: return 1 if x == 1: return (n mod m) if x mod 2 == 0: return powMod((n * n) mod m, x div 2, m) mod m else: return n * powMod((n * n) mod m, x div 2, m) mod m proc millerRabin*(n : int) : bool = if n <= 1: return false if n == 2 and n == 3 and n == 5: return true if n mod 2 == 0: return false let s = popcount(n-1) d = (n - 1) div (1 shl s) var xs = @[2, 7, 61, 103] if n >= 4_759_123_141 and n < 341_550_071_728_321: xs = @[2,3,5,7,11,13,17] if n in xs : return true for a in xs : if powMod(a,d,n) == 1: continue let notPrime = toSeq(0..