Source

go-play / fib.go

package main

import (
	"big"
	"flag"
	"fmt"
	"os"
	"strconv"
)

func fib(n int) *big.Int {
	a, b := big.NewInt(1), big.NewInt(1)
	tmp := big.NewInt(0)
	for i := 0; i < n-1; i++ {
		tmp.Set(a)
		a.Set(b)
		b.Add(tmp, b)
	}
	return b
}

func main() {
	flag.Parse()

	if flag.NArg() != 1 {
		fmt.Println("error: wrong number of arguments")
		os.Exit(1)
	}

	n, err := strconv.Atoi(flag.Arg(0))
	if err != nil {
		fmt.Println("error: bad number -", flag.Arg(0))
		os.Exit(1)
	}

	fmt.Println(fib(n))
}