summaryrefslogtreecommitdiff
path: root/e.icl
blob: e9b420721e139b5498092144847b816a70a8a816 (plain) (blame)
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
module e

/*
Approximation of the number e.

Result: A list containing the first NrDigits digits of e = [2,7,1,8,2,8,1,8,2,8,...].
*/

(<) infix 4 :: !Int !Int -> Bool
(<) a b = code inline {
	ltI
}

(+) infixl 6 :: !Int !Int -> Int
(+) a b = code inline {
	addI
}

(-) infixl 6 :: !Int !Int -> Int
(-) a b = code inline {
	subI
}

(*) infixl 7 :: !Int !Int -> Int
(*) a b = code inline {
	mulI
}

(/) infixl 7 :: !Int !Int -> Int
(/) a b = code inline {
	divI
}

(==) infix 4 :: !Int !Int -> Bool
(==) a b = code inline {
	eqI
}

(rem) infix 7 :: !Int !Int -> Int
(rem) a b = code inline {
	remI
}

take :: Int [a] -> [a]
take 0 _      = []
take n [x:xs] = [x:take (n-1) xs]

hd :: [a] -> a
hd [x:xs] = x

tl :: [a] -> [a]
tl [_:xs] = xs

//import StdEnv

NrDigits :== 200	// The number of digits of the approximation of e

//	Approximating e:

Approx_e::[Int]
Approx_e =	[2:Expan ones] where ones= [1:ones]

//	Expan expects an infinite list of ones and returns an infinite
//	list containing the digits of the fraction of e ([7,1,8,2,8,...]).

Expan::[Int] -> [Int]
Expan f	= [hd ten:Expan	(tl ten)]
	where 
		ten = Ten 2 f

		Ten::Int [Int] -> [Int]
		Ten c [p:q]	| Safe k c	= 	[k / c,  k rem c  + a1 : b1]
								= 	[(k + a1) / c, (k + a1) rem c : b1]
		where
			a1	=	hd ten
			b1	=	tl ten
			ten	=	Ten (c+1) q
			k	=	10 * p

Safe::Int Int -> Bool
Safe k c =   k/c  ==  (k + 9)/c 

/*
The Start rule	returns the first NrDigits elements of the
				list of digits returned by the function
				'Approx_e' by means of the function take.
*/

Start::[Int]
Start = take NrDigits Approx_e