exercism/go/grains/grains.go

31 lines
634 B
Go

package grains
import "errors"
// Square just takes 1 * 2^sq
func Square(sq int) (uint64, error) {
if sq > 0 && sq < 65 {
return (1 << uint(sq-1)), nil
}
return 0, errors.New("Invalid Square Requested")
}
// TotalForSquare calculates the total number
// for all squares up to sq (cause why not)
func TotalForSquare(sq int) (uint64, error) {
if sq < 0 || sq > 64 {
return 0, errors.New("Invalid Square Requested")
}
var ret uint64
for i := 0; i <= sq; i++ {
ret = (ret << 1) | 1
}
return ret, nil
}
// Total calculates the total for the entire board
func Total() uint64 {
ret, _ := TotalForSquare(64)
return ret
}