1234567891011121314151617181920212223242526272829303132333435363738394041 |
- package kata
- import (
- "fmt"
- )
- type state byte
- /*
- The sequence is documented as only being tested for up to 1E6, i.e. 24 bits.
- It is assumed to ignore leading zeroes.
- A faster solution might use the 2-automaton mechanism described in
- https://www.emis.de//journals/SLC/opapers/s30allouche.pdf
- */
- func BaumSweet(ch chan<- int) {
- // Handle n == 0 as binary empty string, par kata discussion.
- ch <- 1
- N_LOOP:
- for n := 1; n < 1E4+1; n++ {
- // Adding external 1s does not changes the result, and ensures the potential
- // final 0 sequence is measured without a special case.
- s := []byte(fmt.Sprintf("1%b1", n))
- // Runes will be '0' and '1' by construction, so always single-byte.
- segmentLength := 0
- for i := 0; i < len(s); i++ {
- if s[i] == '1' {
- if segmentLength%2 == 1 {
- ch <- 0
- continue N_LOOP
- } else {
- // Do nothing
- }
- } else {
- segmentLength++
- }
- }
- ch <- 1
- }
- }
|