LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#51 February 18 2016

Joe
Member

Re: [Exercise] Fibonacci sequence

Still working with Go:

package main

import (
	"fmt"
	"math/big"
)

func fibonacci(n int) *big.Int {
	if n < 2 {
		return big.NewInt(int64(n))
	}

	a, b := big.NewInt(0), big.NewInt(1)
	c := big.NewInt(0)

	for i := 1; i < n; i++ {
		c.Set(a)
		a.Set(b)
		b.Add(b, c)
	}

	return b

}

func main() {
	fmt.Printf("%s\n", fibonacci(100).String())
}

I confirm that the result is 354224848179261915075.

PS: The Go bigNum library is a mess and the lack of operator overloading is annoying.

Offline

Board footer