Time for an 2014 update…

This commit is contained in:
Ingy döt Net 2014-01-17 05:32:22 +00:00
parent 372c577f83
commit 09687c4926
2520 changed files with 34227 additions and 7318 deletions

View file

@ -2,8 +2,10 @@ package main
import "fmt"
func hs(n int, seq *[]int) {
s := append((*seq)[:0], n)
// 1st arg is the number to generate the sequence for.
// 2nd arg is a slice to recycle, to reduce garbage.
func hs(n int, recycle []int) []int {
s := append(recycle[:0], n)
for n > 1 {
if n&1 == 0 {
n = n / 2
@ -12,21 +14,18 @@ func hs(n int, seq *[]int) {
}
s = append(s, n)
}
*seq = s
return s
}
func main() {
var seq []int
hs(27, &seq)
seq := hs(27, nil)
fmt.Printf("hs(27): %d elements: [%d %d %d %d ... %d %d %d %d]\n",
len(seq), seq[0], seq[1], seq[2], seq[3],
seq[len(seq)-4], seq[len(seq)-3], seq[len(seq)-2], seq[len(seq)-1])
var maxN, maxLen int
for n := 1; n < 100000; n++ {
// reusing seq reduces garbage
hs(n, &seq)
seq = hs(n, seq)
if len(seq) > maxLen {
maxN = n
maxLen = len(seq)