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
package main

import (
	"fmt"
	"flag"
)

func fib(n int) uint64 {
	if n < 2 {
		return 1
	}
	return (fib(n-1) + fib(n-2))
}

func fib2(n int) uint64 {
	if n < 2 {
		return 1
	}
	var n1 uint64 = 1
	var n2 uint64 = 1
	for i := 2; i < n; i++ {
		t := n2
		n2 += n1
		n1 = t
	}
	return n2
}

func main() {
	var n int
	var f bool
	flag.IntVar(&n, "n", 10, "number to calc")
	flag.BoolVar(&f, "f", true, "func to use(default=fast)")
	flag.Parse()
	var x uint64 = 0
	if f {
		x = fib2(n)
	} else {
		x = fib(n)
	}
	fmt.Printf("fib[%d]=%d\n", n, x)
}