-
Notifications
You must be signed in to change notification settings - Fork 0
/
td6.ml
230 lines (79 loc) · 2.58 KB
/
td6.ml
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
(* let rec (<|>) a b = if a=b then [] else a::((a+1)<|> b) ;;
let rec records cmp u =
match u with
|[]-> []
|x::r->
let rec aux cmp u courant =
match u with
|[]->[]
|x::r when cmp x courant >0 -> x::( aux cmp r x)
|x::r->aux cmp r courant
in aux cmp u x;;
let rec records2 cmp u = match u with
| [] -> []
| [a] -> [a]
| a :: b :: q ->
if cmp b a > 0 then a :: records2 cmp (b :: q)
else records2 cmp (a :: q)
let rec pgcd a b =
let rec aux a b x =
if b=0 then a,x else
aux b (a mod b) (x+1)
in aux a b 0;;
let phi n =
let rec aux k n = if k=0 then 0
else let a,b= aux (k-1) n,(snd (pgcd n k)) in if a>b then a else b
in aux n n ;;
let records_euclide borne =
let a = List.map (fun i -> (i,phi i)) ( 2<|>borne) in
records (fun x y -> (snd x - snd y)) a;;
let rec f n = if n = 1 then 0 else if n mod 2 = 0 then 1+ f(n/2) else 1+f(n+1) ;;
let records_bis borne =
let a = List.map (fun i -> f i) ( 2<|>borne) in
records (fun x y -> ( x - y)) a;;
(*6.9*)
let rec f m n =
if n=0 then 1
else if m = 1 then 2*n
else f (m-1) (f m (n-1)) ;;
*)
let rec (<|>) a b = if a=b then [] else a::( (a+1)<|> b) ;;
let records cmp u =
let rec aux u x= match u with
|[]->[]
|a::r-> if cmp a x >0 then a::( aux r a) else aux r x
in match u with
|[]->[]
|x::r->x::(aux r x) ;;
let rec f a b = if b =0 then 0 else 1+f b (a mod b) ;;
let phi n = let rec aux n k = if n = 0 then 0 else
if k=0 then f n 0 else let a,b= f n k , aux n (k-1) in
if a>b then a else b
in aux n (n-1) ;;
let records_euclide n =
let a = 0<|>n in
let rec aux a = match a with
|[]->[]
|x::r-> (x,phi x)::(aux r) in
records (fun (x,y) (m,p) ->y-p) (aux a) ;;
(* fibonacci*)
let rec ff n =
if n = 1 then 0 else
if n mod 2 = 0 then 1+ ff ( n/2)
else 1+ ff (n+1) ;;
let records_ff n =
let a = 1<|>n in
let rec aux a = match a with
|[]->[]
|x::r-> (x,ff x)::(aux r) in
records (fun (x,y) (m,p) ->y-p) (aux a) ;;
let rec fack m n =
if n = 0 then 1 else
if m = 1 then 2*n else
fack (m-1) (fack m (n-1)) ;;
(*
let rec power_mod a b n = if b = 0 then 1 else a*(product_mod a (b-1) n) mod n
let ack4 n modu =
let rec aux u = if u = 0 then 13 else
in power_mod
*)