| 1 |
// This program displays the fibonacci sequence |
| 2 |
import os |
| 3 |
|
| 4 |
fn main() { |
| 5 |
// Check for user input |
| 6 |
if os.args.len != 2 { |
| 7 |
println('usage: fibonacci [rank]') |
| 8 |
|
| 9 |
return |
| 10 |
} |
| 11 |
|
| 12 |
// Parse first argument and cast it to int |
| 13 |
|
| 14 |
stop := os.args[1].int() |
| 15 |
// Can only calculate correctly until rank 92 |
| 16 |
if stop > 92 { |
| 17 |
println('rank must be 92 or less') |
| 18 |
return |
| 19 |
} |
| 20 |
|
| 21 |
// Three consecutive terms of the sequence |
| 22 |
mut a := i64(0) |
| 23 |
mut b := i64(0) |
| 24 |
mut c := i64(1) |
| 25 |
println(a + b + c) |
| 26 |
for _ in 0 .. stop { |
| 27 |
// Set a and b to the next term |
| 28 |
a = b |
| 29 |
b = c |
| 30 |
// Compute the new term |
| 31 |
c = a + b |
| 32 |
|
| 33 |
// Print the new term |
| 34 |
println(c) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
// From https://github.com/vlang/v/blob/master/examples/fibonacci.v |