# typevect
=
float
array
type
mat
=
vect
array
type
syst
=
{
m
:
mat;
v
:
vect}
;;
type vect = float array
type mat = vect array
type syst = { m: mat; v: vect }
# letaffiche_systeme
s
=
let
w
=
Array.length
s
.
m.
(0
)
and
h
=
Array.length
s
.
min
let
c
=
h
/
2
in
for
i
=
0
to
h
-
1
do
Printf.printf
"| "
;
for
j
=
0
to
w
-
1
do
Printf.printf
" %8.2f "
s
.
m.
(i).
(j)
done;
Printf.printf
" |"
;
if
i
=
c
then
Printf.printf
" * "
else
Printf.printf
" "
;
Printf.printf
"| x_%-2d |"
i;
if
i
=
c
then
Printf.printf
" = "
else
Printf.printf
" "
;
Printf.printf
"| %8.2f |\n"
s
.
v.
(i)
done;
Printf.printf
"\n"
;;
val affiche_systeme : syst -> unit = <fun>
# letax3
=
{
m
=
[|
[|
1
0
.
0
;
7
.
0
;
8
.
1
;
7
.
2
|]
;
[|
7
.
0
8
;
5
.
0
4
;
6
.
0
;
5
.
0
|]
;
[|
8
.
0
;
5
.
9
8
;
9
.
8
9
;
9
.
0
|]
;
[|
6
.
9
9
;
4
.
9
9
;
9
.
0
;
9
.
9
8
|]
|]
;
v
=
[|
3
2
.
0
;
2
3
.
0
;
3
3
.
0
;
3
1
.
0
|]
}
;;
val ax3 : syst =
{m=
[|[|10; 7; 8.1; 7.2|]; [|7.08; 5.04; 6; 5|]; [|8; 5.98; 9.89; ...|];
...|];
v=...}
# affiche_systemeax3
;;
| 10.00 7.00 8.10 7.20 | | x_0 | | 32.00 |
| 7.08 5.04 6.00 5.00 | | x_1 | | 23.00 |
| 8.00 5.98 9.89 9.00 | * | x_2 | = | 33.00 |
| 6.99 4.99 9.00 9.98 | | x_3 | | 31.00 |
- : unit = ()
# letinterval
order
next
a
b
=
let
rec
aux
a
=
if
not
(order
a
b)
then
[
a]
else
a
::
aux
(next
a)
in
aux
a
;;
val interval : ('a -> 'b -> bool) -> ('a -> 'a) -> 'a -> 'b -> 'a list =
<fun>
# letrec
eras
l
=
match
l
with
[]
->
[]
|
p::q
->
p
::
(eras
(List.filter
(fun
x
->
x
mod
p
<>
0
)q))
;;
val eras : int list -> int list = <fun>
# letera_go
n
=
eras
(interval
(
<
)(fun
x
->
x
+
1
)
2
n)
;;
val era_go : int -> int list = <fun>
# letusage
()
=
Printf.printf
"Usage: premiers n\n"
;;val usage : unit -> unit = <fun>
# letmain
()
=
if
Array.length
(Sys.argv)
<
2
then
usage()
else
let
n
=
int_of_string
Sys.argv
.
(1
)in
if
n
<
2
then
usage()
else
let
r
=
era_go
n
in
List.iter
(fun
x
->
Printf.printf
"%d "
x)
r;
Printf.printf
"\n"
;;
val main : unit -> unit = <fun>
main();;
$ ocamlc -o premiers premiers.mlou
$ ocamlopt -o premiers premiers.mlTest de l'exécutable :
$ premiers Usage: premiers n $ premiers 50 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 $ premiers 100 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
# letanalyse_couleurs
c
=
let
l
=
ref
[]
in
for
i
=
0
to
(Array.length
c)
-
1
do
for
j
=
0
to
(Array.length
c
.
(0
))
-
1
do
if
not(List.mem
c
.
(i).
(j)
!
l)then
l
:=
c
.
(i).
(j)::
!
l
done
;
done
;
List.rev
!
l;;
val analyse_couleurs : 'a array array -> 'a list = <fun>
# letconstruire_table
l
=
Array.of_list
l
;;
val construire_table : 'a list -> 'a array = <fun>
# exceptionFind
of
int;;
exception Find of int
# lettrouve_indice
c
t
=
let
aux
()
=
for
i
=
0
to
Array.length
t
do
if
c
=
t
.
(i)then
raise
(Find
i)
done
;
raise
Not_found
in
try
aux
()
with
Find
i
->
i
;;
val trouve_indice : 'a -> 'a array -> int = <fun>
# letencode
caa
t
=
if
Array.length
t
>
2
5
5
then
failwith
"trop de couleurs (> 255)"
else
let
h
=
Array.length
caa
and
w
=
Array.length
caa
.
(0
)in
let
s
=
String.create
(h
*
w)
in
let
ns
=
ref
0
in
for
i
=
0
to
h
-
1
do
for
j
=
0
to
w
-
1
do
let
ci
=
trouve_indice
caa
.
(i).
(j)t
in
s
.[!
ns]
<-
char_of_int
ci
;
incr
ns
done
done
;
s
;;
val encode : 'a array array -> 'a array -> string = <fun>
# typeimage_tdc
=
{
tdc
:
Graphics.color
array;
image
:
string;
largeur
:
int;
hauteur
:
int};;
type image_tdc =
{ tdc: Graphics.color array;
image: string;
largeur: int;
hauteur: int }
# letto_image_tdc
caa
=
let
t
=
construire_table
(analyse_couleurs
caa)
in
let
s
=
encode
caa
t
in
{
tdc
=
t;
image
=
s;
largeur
=
Array.length
caa
.
(0
);hauteur
=
Array.length
caa}
;;
val to_image_tdc : Graphics.color array array -> image_tdc = <fun>
# letsauve_image_tdc
im
nom
=
let
oc
=
open_out
nom
in
Marshal.to_channel
oc
im
[]
;;
val sauve_image_tdc : 'a -> string -> unit = <fun>
# letfrom_image_tdc
im
=
let
r
=
Array.create_matrix
im
.
hauteurim
.
largeurGraphics.black
in
let
ns
=
ref
0
in
for
i
=
0
to
im
.
hauteur
-
1
do
for
j
=
0
to
im
.
largeur
-
1
do
r
.
(i).
(j)
<-
im
.
tdc.
(int_of_char
im
.
image.[!
ns]
);
incr
ns
done
done
;
r
;;
val from_image_tdc : image_tdc -> Graphics.color array array = <fun>
# letvisu
name
=
let
ic
=
open_in
name
in
let
im
=
Marshal.from_channel
ic
in
let
caa
=
from_image_tdc
im
in
let
b
=
Graphics.make_image
caa
in
let
size
=
(string_of_int
(Array.length
caa
.
(0
)))
^
"x"
^
(string_of_int
(Array.length
caa))
in
Graphics.open_graph
(
" "
^
size)
;
Graphics.draw_image
b
0
0
;
b
;;
val visu : string -> Graphics.image = <fun>