This repository was archived by the owner on Nov 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNumberTheory.hs
93 lines (80 loc) · 2.69 KB
/
NumberTheory.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
module NumberTheory(factorial, binCoeff, primes, factorize, igcd,
(%%), modAdd, modSub, modMul, modDiv, modPow, modOps,
isPrime) where
import Trace
import QSort
import IntegerMisc
infix 7 %%
default (Integer)
-- Finally, a legitimate use of fac!
factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n-1)
-- Binomial coefficients
binCoeff :: Integer -> Integer -> Integer
binCoeff n k = product [n-k+1 .. n] `quot` factorial k
-- Prime numbers
primes :: [Integer]
primes = map fromInt (sieve [2..])
where sieve (p:xs) = (p::Int) : sieve [x | x<-xs, x `rem` p /= 0]
-- Prime factorization, really slow
factorize :: Integer -> [Integer]
factorize n = f primes -- trial division first
where f (p:ps) = if p*p > n then [n]
else if n `rem` p == 0 then p : factorize (n `quot` p)
else if p > maxTest then sort (factorize' n)
else f ps
-- Trial division has found all small factors, use Fermats algorithm to find the biggest
factorize' :: Integer -> [Integer]
#if 0
factorize' n =
let (sn,r0) = integerSqrt n
x' = 2*sn+1 -- 2x+1
y' = 1 -- 2y+1
r = -r0 -- x^2-y^2-n
done (x',y',r) = r == 0
step (x',y',r) = trace ("ostep "++show(x',y',r)) (until1 (\(x',y',r)->r<=0)
(\(x',y',r)->trace ("istep "++show(x',y',r)) (x',y'+2,r-y'))
(x'+2,y',r+x'))
(x,y,_) = until done step (x',y',r)
f = (x-y) `quot` 2 -- largest factor <= sqrt(n)
m = (x+y+2) `quot` 2
in trace ("start "++show[sn,r0,x',y',r]) ({-factorize' m ++-} [m,f])
until1 f g x = until f g (g x)
#else
factorize' = error "factorize'"
#endif
-- Greatest common divisor
igcd :: Integer -> Integer -> Integer
igcd x y = integerGcd x y
-- Modulo arithmetic
(%%) :: Integer -> Integer -> Integer
x %% y =
let r = x `rem` y
in if r < 0 then r+y else r
modAdd :: Integer -> Integer -> Integer -> Integer
modAdd n x y = (x+y) %% n
modSub :: Integer -> Integer -> Integer -> Integer
modSub n x y = (x-y) %% n
modMul :: Integer -> Integer -> Integer -> Integer
modMul n x y = (x*y) %% n
modDiv :: Integer -> Integer -> Integer -> Integer
modDiv n x y = (x `div` y) %% n
modPow :: Integer -> Integer -> Integer -> Integer
modPow n x y = integerPowMod x y n
-- Do
-- let ((+),(-),(*),(/),(^), mod) = modOps N;;
-- to get modular arithmetic in hbi.
modOps n = (modAdd n, modSub n, modMul n, modDiv n, modPow n, (%% n))
-- Primality tester
maxTest :: Integer
maxTest = 10000 -- limit for naive test
isPrime :: Integer -> Bool
isPrime n = f primes -- algorithm: trial division
where f (p:ps) = if p > maxTest then
isPrime' n
else
p*p > n || n `rem` p /= 0 && f ps
isPrime' x = error "isPrime'"
-- log
-- phi