A342407 Number of 2's in the first 10^n entries of the Kolakoski sequence (A000002).
0, 5, 51, 498, 5004, 50028, 500014, 4999954, 49999325, 499998777, 5000002329, 49999998413, 499999949299, 4999999991841, 49999999683763, 499999999022579, 5000000005362272, 50000000022520652, 500000000055534895, 5000000000274296550, 50000000000909149240
Offset: 0
Keywords
Examples
The first 10 entries of the Kolakoski sequence (A000002) are 1221121221. From this we see that a(0) = 0, since the first term is not a 2, and a(1)=5 since among the first 10 terms, 5 of them are 2's.
Programs
-
Go
package main import ( "fmt" "math" ) func main() { fmt.Println(a(5)) } func a(n int) int { k := int(math.Pow(10, float64(n))) // get the number of terms of the Kolakoski sequence (A000002) to generate // seq stores the Kolakoski sequence seq := make([]int, 0, k+1) // n+1 because instruction 2 could add one extra var ind, i, toAppend int // ind represents which instruction to follow seq = append(seq, 1, 2, 2) // initial terms ind = 2 // follow third instruction next i += 3 // three numbers already added for i < k { if seq[ind] == 1 { // add one number? if seq[len(seq)-1] == 1 { toAppend = 2 } else { toAppend = 1 } seq = append(seq, toAppend) i++ // we added a number ind++ // next instruction } else { // add two numbers if seq[len(seq)-1] == 1 { toAppend = 2 } else { toAppend = 1 } seq = append(seq, toAppend, toAppend) // append two numbers i += 2 // we added two numbers ind++ } } seq = seq[:k] // trim to k values // now count twos var twos int for _, curr := range seq { if curr == 2 { twos++ } } return twos }
Formula
a(n) = 10^n - A195206(n).
Comments