-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibmatrix.ml
More file actions
77 lines (49 loc) · 2.28 KB
/
Copy pathfibmatrix.ml
File metadata and controls
77 lines (49 loc) · 2.28 KB
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
(***********************************************************************
Developed by Caetán Tojeiro Carpente (caetantojeiro95@gmail.com)
Copyleft (C) 2017 Caetán Tojeiro Carpente
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/agpl-3.0.html>
************************************************************************)
open Num;;
let multM (a1,b1,c1) (a2,b2,c2) =
let b1b2 = b1*/b2
in (a1*/a2 +/ b1b2, a1*/b2 +/ b1*/c2, b1b2 +/ c1*/c2)
let rec elevar m n =
if n=0 then (Int 1, Int 0, Int 1)
else let m2 = elevar m (n/2)
in
if(n mod 2) = 0 then multM m2 m2
else multM m (multM m2 m2)
(*Se comprueba que el número con el que trabajar no es negativo.
En caso afirmativo se opera con él*)
let fib n =
let (_,r,_) = (elevar (Int 1, Int 1, Int 0) n)
in
if n>=0 then string_of_num r
else ("ERROR: " ^ (string_of_int n) ^ " es un número negativo");;
(*Se comprueba que solo se ha introducido un argumento, si no es así,
se muestra un mensaje de error y un ejemplo de sintaxis correcta y se sale del programa*)
let () =
if (Array.length Sys.argv -1 <> 1) then
(print_endline ("ERROR: el número de argumentos no es correcto. Un ejemplo de sintaxis correcta es: ./fib 100");
exit 0;)
(*Si la sintáxis del comando es correcta se procede a leer el argumento*)
let argumento = Sys.argv.(1);;
(*Se comprueba que se ha introducido un número y este es, al menos, un número entero.
Si no se introduce un número, o no es entero, se muestra un error y se abandona el programa*)
try
(int_of_string argumento);
with
| _ -> print_endline ("ERROR: número válido. Por favor, introduzca un NÚMERO NATURAL o el NÚMERO CERO (0).");
exit 0
let numero = (int_of_string argumento);;
let result = fib numero;;
print_endline (result);;