vault backup: 2023-09-15 16:17:18
This commit is contained in:
@ -119,6 +119,12 @@ Exemple with egypt (4.10) vs multiply (4.6):
|
||||
The best algorithm in term of complexity is the parameter that is constant/linear or logarithmic. If you have an exponential algorithm, you can put it in trash :)
|
||||
|
||||
**Exemple with fibonacci algorithm**
|
||||
```Ocaml
|
||||
# let rec fibo = function
|
||||
0|1 -> 1
|
||||
| n -> fibo (n-1) + fibo(n-2);;
|
||||
```
|
||||
|
||||
|
||||
| |res|how (for human) ?|How (for function) ?|
|
||||
|:----:|:----:|:----:|:----:|
|
||||
@ -133,4 +139,14 @@ The best algorithm in term of complexity is the parameter that is constant/linea
|
||||
|
||||
|
||||
<center><img src="https://imgur.com/6OWREOm.png" height=400 width=auto/></center>
|
||||
This function is not optimize because the number of calls is growing exponentially
|
||||
This function is not optimize because the number of calls is growing exponentially.
|
||||
A good function will be:
|
||||
```Ocaml
|
||||
# let fibo n =
|
||||
let rec fib i fi fi_1 =
|
||||
if n <= i then
|
||||
fi
|
||||
else
|
||||
fib (i+1)(fi+fi_1)fi
|
||||
in fib 1 1 1
|
||||
```
|
||||
|
Reference in New Issue
Block a user