Skip to main content
saved a byte by using exponentially more time and space
Source Link
algorithmshark
  • 8.9k
  • 3
  • 35
  • 57

J, 1312 bytes

Single-argument function taking n and producing the first n terms. Try it online!Try it online!

$($1+2|I1+2|I.)^:_~] 
1 2 2 1 1 2 1 2 2 1 (some prefix) 0 1 1 2 2 3 4 5 5 6 7 7 8 8 9 (use I.) 0 1 1 0 0 1 0 1 1 0 1 1 0 0 1 (mod 2) 1 2 2 1 1 2 1 2 2 1 2 2 1 1 2 (add 1)  

The other ingredient we need is the dyadic verb $, where x$y truncates or cyclically extends y to have length x. In this way, we are able to both turn a small seed (literally anything would do so we reuse n) into enough to start the sequence, as well as to only generate the first n terms at every step. This $I. combo is great for sequences like this.

If we perform this operation until it settles down (^:_1+2|I.) over and over starting from 10, it looks something like this:

10 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 2 1 2 1 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 1 2 1 1 2 2 1 2 2 1 1 ... 1 2 2 1 1 2 1 2 2 1 2 1 1 2 2 ... 1 2 2 1 1 2 1 2 2 1 2 2 1 1 2 ... 

Notice how we get more and more correct terms each time, and after a while the first n terms are fixed. The number of iterations it takes to settle down is hard to describe precisely, but it looks to be roughly logarithmic in n, so if we just run it n times (^:]) it should be fine. Check(Check out these other OEIS sequences for more info: generation lengths, partial sums.)

Once we're done that, all we have to do is take the first n terms using $. The construction $v for any verb v is an example of a hook, and giving it n as argument will execute n $ (v n).

Here is the old 13-byte version which is far less wasteful of time and space: ($1+2|I.)^:_~. It truncates the input at every step, so we can run exactly as many times as is needed to settle, instead of linearly many times.

J, 13 bytes

Single-argument function taking n and producing the first n terms. Try it online!

($1+2|I.)^:_~ 
1 2 2 1 1 2 1 2 2 1 (some prefix) 0 1 1 2 2 3 4 5 5 6 7 7 8 8 9 (use I.) 0 1 1 0 0 1 0 1 1 0 1 1 0 0 1 (mod 2) 1 2 2 1 1 2 1 2 2 1 2 2 1 1 2 (add 1) 

The other ingredient we need is the dyadic verb $, where x$y truncates or cyclically extends y to have length x. In this way, we are able to both turn a small seed (literally anything would do so we reuse n) into enough to start the sequence, as well as to only generate the first n terms at every step. This $I. combo is great for sequences like this.

If we perform this operation until it settles down (^:_) starting from 10, it looks something like this:

10 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 2 1 2 1 2 1 2 2 1 2 2 1 2 2 1 1 2 2 1 1 2 1 1 2 2 1 2 2 1 1 2 1 2 2 1 

Notice how we get more and more correct terms each time. The number of iterations it takes to settle down is hard to describe precisely, but it looks to be roughly logarithmic in n. Check out these other OEIS sequences for more info: generation lengths, partial sums.

J, 12 bytes

Single-argument function taking n and producing the first n terms. Try it online!

$(1+2|I.)^:] 
1 2 2 1 1 2 1 2 2 1 (some prefix) 0 1 1 2 2 3 4 5 5 6 7 7 8 8 9 (use I.) 0 1 1 0 0 1 0 1 1 0 1 1 0 0 1 (mod 2) 1 2 2 1 1 2 1 2 2 1 2 2 1 1 2 (add 1)  

If we perform this operation (1+2|I.) over and over starting from 10, it looks something like this:

10 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 2 1 2 1 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 1 2 1 1 2 2 1 2 2 1 1 ... 1 2 2 1 1 2 1 2 2 1 2 1 1 2 2 ... 1 2 2 1 1 2 1 2 2 1 2 2 1 1 2 ... 

Notice how we get more and more correct terms each time, and after a while the first n terms are fixed. The number of iterations it takes to settle down is hard to describe precisely, but it looks to be roughly logarithmic in n, so if we just run it n times (^:]) it should be fine. (Check out these other OEIS sequences for more info: generation lengths, partial sums.)

Once we're done that, all we have to do is take the first n terms using $. The construction $v for any verb v is an example of a hook, and giving it n as argument will execute n $ (v n).

Here is the old 13-byte version which is far less wasteful of time and space: ($1+2|I.)^:_~. It truncates the input at every step, so we can run exactly as many times as is needed to settle, instead of linearly many times.

added 55 characters in body
Source Link
algorithmshark
  • 8.9k
  • 3
  • 35
  • 57

J, 1413 bytes

Single-argument function taking n and producing the first n terms. Try it online!Try it online!

($1+2|I.)^:_&1_~ 

Just sqrucingsprucing up my old answer to the old question.

I. is a verb which takes an array of numbers and spits out a list of indices, so that if the k-th item in the array is n, then the index k appears n times. We'll use it to bootstrap the Kolakowski sequence up from an initial seed. Each step will proceed as follows:

1 2 2 1 1 2 1 2 2 1 (some prefix) 0 1 1 2 2 3 4 5 5 6 7 7 8 8 9 (use I.) 0 1 1 0 0 1 0 1 1 0 1 1 0 0 1 (mod 2) 1 2 2 1 1 2 1 2 2 1 2 2 1 1 2 (add 1) 

The other ingredient we need is the dyadic verb $, where x$y truncates or cyclically extends y to have length x. In this way, we are able to both turn a small seed (&1literally anything would do so we reuse n) into enough to start the sequence, as well as to only generate the first nn terms at every step. This $I. combo is great for sequences like this.

If we perform this operation until it settles down (^:_) starting from 10, it looks something like this:

110 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 2 1 2 1 2 1 2 2 1 2 2 1 2 2 1 1 2 2 1 1 2 1 1 2 2 1 2 2 1 1 2 1 2 2 1 

Notice how we get more and more correct terms each time. The number of iterations it takes to settle down is hard to describe precisely, but it looks to be roughly logarithmic in n. Check out these other OEIS sequences for more info: generation lengths, partial sums.

J, 14 bytes

Single-argument function taking n and producing the first n terms. Try it online!

($1+2|I.)^:_&1 

Just sqrucing up my old answer to the old question.

I. is a verb which takes an array of numbers and spits out a list of indices, so that if the k-th item in the array is n, then the index k appears n times. We'll use it to bootstrap the Kolakowski sequence up from an initial seed. Each step will proceed as follows:

1 2 2 1 1 2 1 2 2 1 (some prefix) 0 1 1 2 2 3 4 5 5 6 7 7 8 8 9 (use I.) 0 1 1 0 0 1 0 1 1 0 1 1 0 0 1 (mod 2) 1 2 2 1 1 2 1 2 2 1 2 2 1 1 2 (add 1) 

The other ingredient we need is the dyadic verb $, where x$y truncates or cyclically extends y to have length x. In this way, we are able to both turn a small seed (&1) into enough to start the sequence, as well as to only generate the first n terms at every step. This $I. combo is great for sequences like this.

If we perform this operation until it settles down (^:_), it looks something like this:

1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 2 1 2 1 2 1 2 2 1 2 2 1 2 2 1 1 2 2 1 1 2 1 1 2 2 1 2 2 1 1 2 1 2 2 1 

Notice how we get more and more correct terms each time. The number of iterations it takes to settle down is hard to describe precisely, but it looks to be roughly logarithmic in n. Check out these other OEIS sequences for more info: generation lengths, partial sums.

J, 13 bytes

Single-argument function taking n and producing the first n terms. Try it online!

($1+2|I.)^:_~ 

Just sprucing up my old answer to the old question.

I. is a verb which takes an array of numbers and spits out a list of indices, so that if the k-th item in the array is n, then the index k appears n times. We'll use it to bootstrap the Kolakowski sequence up from an initial seed. Each step will proceed as follows:

1 2 2 1 1 2 1 2 2 1 (some prefix) 0 1 1 2 2 3 4 5 5 6 7 7 8 8 9 (use I.) 0 1 1 0 0 1 0 1 1 0 1 1 0 0 1 (mod 2) 1 2 2 1 1 2 1 2 2 1 2 2 1 1 2 (add 1) 

The other ingredient we need is the dyadic verb $, where x$y truncates or cyclically extends y to have length x. In this way, we are able to both turn a small seed (literally anything would do so we reuse n) into enough to start the sequence, as well as to only generate the first n terms at every step. This $I. combo is great for sequences like this.

If we perform this operation until it settles down (^:_) starting from 10, it looks something like this:

10 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 2 1 2 1 2 1 2 2 1 2 2 1 2 2 1 1 2 2 1 1 2 1 1 2 2 1 2 2 1 1 2 1 2 2 1 

Notice how we get more and more correct terms each time. The number of iterations it takes to settle down is hard to describe precisely, but it looks to be roughly logarithmic in n. Check out these other OEIS sequences for more info: generation lengths, partial sums.

Source Link
algorithmshark
  • 8.9k
  • 3
  • 35
  • 57

J, 14 bytes

Single-argument function taking n and producing the first n terms. Try it online!

($1+2|I.)^:_&1 

Just sqrucing up my old answer to the old question.

I. is a verb which takes an array of numbers and spits out a list of indices, so that if the k-th item in the array is n, then the index k appears n times. We'll use it to bootstrap the Kolakowski sequence up from an initial seed. Each step will proceed as follows:

1 2 2 1 1 2 1 2 2 1 (some prefix) 0 1 1 2 2 3 4 5 5 6 7 7 8 8 9 (use I.) 0 1 1 0 0 1 0 1 1 0 1 1 0 0 1 (mod 2) 1 2 2 1 1 2 1 2 2 1 2 2 1 1 2 (add 1) 

The other ingredient we need is the dyadic verb $, where x$y truncates or cyclically extends y to have length x. In this way, we are able to both turn a small seed (&1) into enough to start the sequence, as well as to only generate the first n terms at every step. This $I. combo is great for sequences like this.

If we perform this operation until it settles down (^:_), it looks something like this:

1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 2 1 2 1 2 1 2 2 1 2 2 1 2 2 1 1 2 2 1 1 2 1 1 2 2 1 2 2 1 1 2 1 2 2 1 

Notice how we get more and more correct terms each time. The number of iterations it takes to settle down is hard to describe precisely, but it looks to be roughly logarithmic in n. Check out these other OEIS sequences for more info: generation lengths, partial sums.