Created
December 3, 2019 13:18
-
-
Save sillyfellow/524014c5c9275fb9da1dba04db645b28 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
// VanEck generates the first n elements in the Van-Eck sequence | |
// https://oeis.org/A181391 | |
// Thanks to: https://www.youtube.com/watch?v=etMJxB-igrc | |
func VanEck(limit int64) []int64 { | |
// prepare the basics. The output, and the lookup | |
sequence := make([]int64, limit, limit) | |
lastSeen := map[int64]int64{} | |
// first in the sequence is a zero | |
// NOTE: van eck can start at any other number too. | |
// But we are traditionalists :) | |
sequence[0] = 0 | |
var i int64 | |
for i = 1; i < limit; i++ { | |
// check the last item, and see when it was see before. | |
prev := sequence[i-1] | |
index, seen := lastSeen[prev] | |
// update the when was seen (note: we update this only after we've | |
// checked the previous occurrence ) | |
lastSeen[prev] = i - 1 | |
// if it was seen earlier, we know who many steps did it take | |
var steps int64 | |
if seen { | |
steps = i - 1 - index | |
} | |
// now, we know the i-th item | |
sequence[i] = steps | |
} | |
return sequence | |
} | |
func main() { | |
fmt.Printf("%v\n", VanEck(2000)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment