OFFSET
0,2
FORMULA
a(n) = 10^n - A195206(n).
EXAMPLE
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.
PROG
(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
}
CROSSREFS
KEYWORD
nonn
AUTHOR
Ishan Goel, Mar 11 2021
STATUS
approved