Skip to content

Instantly share code, notes, and snippets.

@prune998
Created June 14, 2021 13:16
Show Gist options
  • Save prune998/3ec2c1ff3cbe8b63b98abee8e4dc17a7 to your computer and use it in GitHub Desktop.
Save prune998/3ec2c1ff3cbe8b63b98abee8e4dc17a7 to your computer and use it in GitHub Desktop.
grains.go
package grains
import "errors"
func Square(i int) (uint64, error) {
// fail fast
if i < 1 || i > 64 {
return 0, errors.New("square is out of range")
}
// use binay shift
var result uint64 = 1 << (i - 1)
return result, nil
}
func Total() uint64 {
// Total is equal to 65 squares -1
result, _ := Square(65)
return result - 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment