-
Notifications
You must be signed in to change notification settings - Fork 0
/
godelian-toolkit.fsx
42 lines (30 loc) · 1.06 KB
/
godelian-toolkit.fsx
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
////////////////////////////////////////////////////////////////////////
/// The Gödelian Toolkit
////////////////////////////////////////////////////////////////////////
module GodelianTooklit
let sqrt (z: bigint) : bigint =
if z < 0I then
invalidArg "z" "Cannot compute the square root of a negative number"
elif z = 0I then
0I
else
let rec newtonRaphson (x: bigint) : bigint =
let nextX = (x + z / x) / 2I
if nextX >= x then x else newtonRaphson nextX
newtonRaphson z
let encodePair (z: bigint) : bigint * bigint =
let m = sqrt (z)
let m2 = m * m
if z - m2 < m then (z - m2, m) else (m, m2 + 2I * m - z)
let (|Pair|) = encodePair
let decodePair (p: bigint * bigint) : bigint =
let (x, y) = p
let m = max x y
m * m + m + x - y
let combineChoices functionList =
let length = bigint (List.length functionList)
let rec chooseFunction n =
let (d, r) = bigint.DivRem(n, length)
let f = functionList.[int (r)]
f chooseFunction d
chooseFunction