diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
| commit | 967be9e750221ab2ab783f95df79bb26d290a45e (patch) | |
| tree | 6802900a5e975f9f68b169f0f503f040056d6952 /tigrou/fibo | |
Diffstat (limited to 'tigrou/fibo')
| -rw-r--r-- | tigrou/fibo/fibo.tig | 24 | ||||
| -rw-r--r-- | tigrou/fibo/fibo.tih | 18 |
2 files changed, 42 insertions, 0 deletions
diff --git a/tigrou/fibo/fibo.tig b/tigrou/fibo/fibo.tig new file mode 100644 index 0000000..afba6cf --- /dev/null +++ b/tigrou/fibo/fibo.tig @@ -0,0 +1,24 @@ +let + import "fibo.tih" + function test_fibo(n : int, expected: int) = + ( + print("Testing fibo("); + print_int(n); + print(")...\nExpected : "); + print_int(expected); + print("\ngot : "); + print_int(fibo(n)); + print("\n\n") + ) +in + test_fibo(-1, -1); + test_fibo(0, 0); + test_fibo(1, 1); + test_fibo(2, 1); + test_fibo(5, 5); + test_fibo(10, 55); + + /* You may fail these if you use recursion. Go iterative. */ + test_fibo(42, 267914296); + test_fibo(45, 1134903170) +end diff --git a/tigrou/fibo/fibo.tih b/tigrou/fibo/fibo.tih new file mode 100644 index 0000000..72123a6 --- /dev/null +++ b/tigrou/fibo/fibo.tih @@ -0,0 +1,18 @@ +function fibo(n : int) : int = + if n < 0 then + -1 + else + if n <= 1 then + n + else + let + var curr := 0 + var prev1 := 1 + var prev2 := 0 + in + (for i := 2 to n do + (curr := prev1 + prev2; + prev2 := prev1; + prev1 := curr)); + curr + end |
