You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
519 B
Forth
22 lines
519 B
Forth
// Modules
|
|
open System
|
|
|
|
// Recursive definition of Fibonacci numbers
|
|
let rec fib n =
|
|
match n with
|
|
| 0 -> 0
|
|
| 1 -> 1
|
|
| _ -> fib (n-1) + fib (n-2)
|
|
|
|
let rec fibSmart n =
|
|
match n with
|
|
| n when n < 0 -> failwith (sprintf "Error: %d is less than 0" n)
|
|
| (0|1) -> n
|
|
| _ -> fibSmart (n-1) + fibSmart (n-2)
|
|
|
|
// Read from stdin and print result
|
|
printfn "Inserisci l'indice del numero di fibonacci che vuoi calcolare:\n"
|
|
let i = Console.ReadLine() |> int
|
|
printfn "%A" (fib (i))
|
|
printfn "%A" (fibSmart (i))
|