September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -1 +1,15 @@
primeFacsExp :: Integer -> [(Integer, Int)]
powerMod
:: (Integral a, Integral b)
=> a -> a -> b -> a
powerMod m _ 0 = 1
powerMod m x n
| n > 0 = f x_ (n - 1) x_
where
x_ = x `rem` m
f _ 0 y = y
f a d y = g a d
where
g b i
| even i = g (b * b `rem` m) (i `quot` 2)
| otherwise = f b (i - 1) (b * y `rem` m)
powerMod m _ _ = error "powerMod: negative exponent"

View file

@ -1,9 +1,17 @@
powerMod :: (Integral a, Integral b) => a -> a -> b -> a
powerMod m _ 0 = 1
powerMod m x n | n > 0 = f x' (n-1) x' where
x' = x `rem` m
f _ 0 y = y
f a d y = g a d where
g b i | even i = g (b*b `rem` m) (i `quot` 2)
| otherwise = f b (i-1) (b*y `rem` m)
powerMod m _ _ = error "powerMod: negative exponent"
import Data.List (foldl1') --'
foldl1_ = foldl1' --'
multOrder a m
| gcd a m /= 1 = error "Arguments not coprime"
| otherwise = foldl1_ lcm $ map (multOrder_ a) $ primeFacsExp m
multOrder_ a (p, k) = r
where
pk = p ^ k
t = (p - 1) * p ^ (k - 1) -- totient \Phi(p^k)
r = product $ map find_qd $ primeFacsExp t
find_qd (q, e) = q ^ d
where
x = powerMod pk a (t `div` (q ^ e))
d = length $ takeWhile (/= 1) $ iterate (\y -> powerMod pk y q) x