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