epicours/Algo/Séminaire/Exercices sémaines.md

166 lines
3.4 KiB
Markdown

## Exercise 2.2 (Power)
```Ocaml
(*First version ; 6 multiplications*)
# let power28(x) =
let x2 = x + x in
let x4 = x2*x2 in
let x8 = x4*x4 in
let x16 = x8*x8 in
x16*x8*x4 ;;
(*Second version ; 27 multiplications*)
# let power28(x) = x*x*x*x*x*x...*x ;;
(*Third version ; 11 multiplications*)
# let power28(x) =
let sq(x) = x*x in
let pow4(x) = sq(sq(x)) in
pow4(pow4(x))*sq(pow4(x))*pow4(x);;
(*Fourth version ; 7 multiplications*)
# let power28(x)=
let sq(x) = x+x in
let p4=sq(sq(x)) in
sq(sq(p4))*sq(p4)*p4;;
```
## Exercise 2.3
```Ocaml
# (*my verison*)
# let mirror(n) = let diz = n/10 and uni = n mod 10 in uni*10 + diz;;
val mirror : int -> int = <fun>
# (*teatcher version*)
# let mirror n = 10 *(n mod 10)+n/10;;
val mirror : int -> int = <fun>
```
```Ocaml
# let abba(n) = n*100 + mirror(n) ;;
val abba: int -> int = <fun>
```
```Ocaml
# let stammer(n) = abba(mirror(n)) * 10 000 + abba(n) ;;
val stammer: int -> int = <fun>
```
## Exercice 2.6
```Ocaml
let sec_of_time h m s =
h*3600 + m*60 + s ;;
let time_of_sec s =
let hours = s/3600 in let minutes = (s - hours*3600)/60 in let seconds = s - hours *3600 - minutes * 60 in (hours, minutes, seconds);;
let add_times h1 m1 s1 h2 m2 s2 =
let sec1 = sec_of_time h1 m1 s1 and sec2 = sec_of_time h2 m2 s2 in let resultsec = sec1 + sec2 in time_of_sec resultsec ;;
```
## Exercise 3.1
```Ocaml
let add a b =
if a > b then
a-b + a/b
else
b-a + b/a
let test x y = (if x> y then x else y ) *(x+y);;
let f a b c =
let g x y = (x+y)*(x-y) in
if a > b then
if b > c then (a+b)*(a-b) else (c+a) *(a-c)
else
if a > c then (a+b)*(a-b) else (b+c) * (b-c);;
let f a b c =
(if a > b && if b > c then
a + b else c + a
else if a > c then a + b else b + c)*
(if a > b && b > c then
a - b else a - c
else if a > c then a-b else b-c);;
```
### Exercise 3.2
```OCaml
(*logical and*)
# let and_if a b =
if a then
b
else false;;
val and_if: bool -> bool -> bool = <fun>
(*logical or*)
# let or_if a b =
if a then true
else b;;
val or_if: bool -> bool -> bool = <fun>
(*logical implication*)
# let imply_if a b =
if a then b
else true;;
val imply_if: bool -> bool -> bool = <fun>
(*logical exclusive or*)
let xor a b =
if a then
if b then false else true
else
if b then true else false
(*logical equivalence = a b*)
let equiv a b =
if a then b
else
if b then false else true (*not b*)
```
### Exercise 3.3
```Ocaml
# let max2 number1 number2 = if number1 > number2 then number1 else number2
val max2 : 'a -> 'a -> 'a = <fun>
# let min2 number1 number2 = if number1 > number2 then number2 else number1
val min2 : 'a -> 'a -> 'a = <fun>
let max3 x y z =
let high = if x > y then x else y in
if high > z then
high
else z;;
val max3 : 'a -> 'a -> 'a -> 'a = <fun>
let min3 x y z =
let low = if x < y then x else y in
if low < z then
low
else z;;
val min3 : 'a -> 'a -> 'a -> 'a = <fun>
# let middle3 x y z =
x + y + z - min3 x y z - max x y z ;;
val middle3 = int -> int -> int = <fun>
let max4 number1 number2 number3 number4 =
let nb1 = max2(number1 number2) and nb2 = max2(number3 number4) in max2(nb1 nb2)
let min4 number1 number2 number3 number4 = let nb1 = min2(number1 number2) and nb2 = min2(number3 number4) in min2(nb1 nb2)
```
### Exercise 3.4
```Ocaml
let highest_square_sum x1 x2 x3 = let bigger = max3(x1 x2 x3) and middle = middle3(x1 x2 x3) in (bigger*bigger, middle*middle)
```