How to resolve the algorithm Kolakoski sequence step by step in the Arturo programming language
How to resolve the algorithm Kolakoski sequence step by step in the Arturo programming language
Table of Contents
Problem Statement
The Kolakoski sequence is an infinite sequence of natural numbers, (excluding zero); with the property that: This is not a Kolakoski sequence: Its sequence of run counts, (sometimes called a run length encoding, (RLE); but a true RLE also gives the character that each run encodes), is calculated like this: The above gives the RLE of: The original sequence is different from its RLE in this case. It would be the same for a true Kolakoski sequence. Lets start with the two numbers (1, 2) that we will cycle through; i.e. they will be used in this order: 1,2,1,2,1,2,.... We will arrange that the k'th item of s states how many times the last item of sshould appear at the end of s. We started s with 1 and therefore s[k] states that it should appear only the 1 time. ... Note that the RLE of 1, 2, 2, 1, 1, ... begins 1, 2, 2 which is the beginning of the original sequence. The generation algorithm ensures that this will always be the case. (There are rules on generating Kolakoski sequences from this method that are broken by the last example)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Kolakoski sequence step by step in the Arturo programming language
Source code in the arturo programming language
kolakoski: function [a, length][
result: array.of: length 0
i: new 0
k: new 0
loop.forever a 'x [
result\[i]: x
if result\[k] > 1 [
loop 1..dec result\[k] 'j [
inc 'i
if i = length -> return result
result\[i]: result\[i-1]
]
]
inc 'i
if i = length -> return result
inc 'k
]
return result
]
possibleKolakoski?: function [seq][
prev: seq\0
count: new 1
rle: new []
loop 1..dec size seq 'i [
if? seq\[i] = prev -> inc 'count
else [
'rle ++ count
count: new 1
prev: seq\[i]
]
]
loop.with:'i rle 'val [
if val <> seq\[i] -> return false
]
return true
]
Seqs: [[1 2] [2 1] [1 3 1 2] [1 3 2 1]]
Lens: [20 20 30 30]
loop couple Seqs Lens 'c [
generated: kolakoski c\0 c\1
print ["First" c\1 "members of the sequence generated by" c\0 ":"]
print generated
print ["Possible Kolakoski sequence?" possibleKolakoski? generated]
print ""
]
You may also check:How to resolve the algorithm Factorial step by step in the FOCAL programming language
You may also check:How to resolve the algorithm XML/Input step by step in the Pike programming language
You may also check:How to resolve the algorithm Comments step by step in the ERRE programming language
You may also check:How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the Julia programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the ACL2 programming language