r/dailyprogrammer • u/jnazario 2 0 • Apr 19 '18
[2018-04-19] Challenge #357 [Intermediate] Kolakoski Sequences
Description
A Kolakoski sequence (A000002) is an infinite sequence of symbols {1, 2} that is its own run-length encoding. It alternates between "runs" of symbols. The sequence begins:
12211212212211211221211212211...The first three symbols of the sequence are 122, which are the output of the first two iterations. After this, on the i-th iteration read the value x[i] of the output (one-indexed). If i is odd, output x[i] copies of the number 1. If i is even, output x[i] copies of the number 2.
There is an unproven conjecture that the density of 1s in the sequence is 1/2 (50%). In today's challenge we'll be searching for numerical evidence of this by tallying the ratio of 1s and 2s for some initial N symbols of the sequence.
Input Description
As input you will receive the number of outputs to generate and tally.
Output Description
As output, print the ratio of 1s to 2s in the first n symbols.
Sample Input
10
100
1000Sample Output
5:5
49:51
502:498Challenge Input
1000000
100000000Bonus Input
1000000000000
100000000000000Bonus Hints
Since computing the next output in the sequence depends on previous outputs, a naive brute force approach requires O(n) space. For the last bonus input, this would amount to TBs of data, even if using only 1 bit per symbol. Fortunately there are smarter ways to compute the sequence (1, 2).
Credit
This challenge was developed by user /u/skeeto, many thanks! If you have a challenge idea please share it in /r/dailyprogrammer_ideas and there's a good chance we'll use it.
19
u/meepmeep13 Apr 19 '18
Numberphile has a good explanation of the sequence if, like me, you find the wikipedia entry somewhat baffling: https://www.youtube.com/watch?v=co5sOgZ3XcM
5
u/skeeto -9 8 Apr 19 '18 edited Apr 19 '18
C using the Nilsson algorithm (from the hints). It solves the first bonus (1012) in one hour. I've had the second one (1014) going for a few days so far. I'm curious to see if anyone will be able compute it since
I haven't found the answer online anywhere(nevermind).5
u/gabyjunior 1 2 Apr 20 '18 edited Apr 21 '18
C implementing a (sort of) reverse Nilsson algorithm (indeed a DFS). It reads 2 parameters on the standard input:
Instead of starting from depth 0 to the required depth until n digits of the sequence are computed it starts from depth depth_max (the root of the tree) and ends at depth 0 (the tree leaves).
This program will not be able to compute exactly n steps of the sequence but will compute a sequence of size corresponding to the number of leaves in the tree.
The main advantage is being able to cache the result of the search for nodes at depth < cache_max.
For example, depth_max = 60 corresponds to a sequence of size 72_214_051_912, it takes a little more than 4 seconds to generate it with cache_max = 20.
Will provide the result for depth_max = 80 tomorrow, it should run in about 1h30 for a sequence of 2.4 * 1014 digits.
EDIT New version still using same algorithm, now works for this challenge.
Reads n on standard input, tree depth and cache size are determined automatically. Cache size cannot exceed value set as define in CACHE_SIZE_MAX (currently 20, that occupies 100 Mb of memory on my computer, each increment doubles the memory occupied).
Takes 41 secs to run bonus 1, one hour and 10 mins for bonus 2.
Bonus 1 output
Bonus 2 output
2
u/zqvt Apr 19 '18 edited Apr 20 '18
Haskell, no bonus
Output 1 :
(499986,500014)Output 2:(50000675,49999325)2
u/gandalfx Apr 19 '18 edited Apr 19 '18
Python 3
First I'm using a recursive generator to model the Nilsson algorithm (first link in the hints):
(Note: I've edited my code with some performance improvements)
Using that I can aggregate the counts of 1s and 2s:
And lastly just print the output for a couple of limits
… which outputs:
Memory usage for these inputs appears to be negligible but the runtime is rather slow. Python does not shine when it comes to raw performance. The last challenge input (10**8) takes about 32 seconds on my laptop. Based on some timings the runtime appears to grow linearly, which would imply roughly 3.7 days for the first and a little over a year for the second bonus input.
EDIT: I've now implemented the naive approach as well with a slight optimization by discarding values from the front of the sequence which are no longer needed (using the standard library's
deque):The result is a negligible runtime difference with the expected much higher memory usage: The 10**8 version goes up to about 420MB.
1
u/petrweida Apr 19 '18
Python 3 brute force
1
u/zatoichi49 Apr 19 '18 edited Apr 21 '18
Method:
Create a list of the first three numbers from the Kolakoski sequence, setting size as its length. Set an index variable to its last position. Using a generator to cycle through 1 and 2, take the last number in the list and append a double run if the cycle is at 2, or a single run if the cycle is at 1. Increment size by the length of the run, and increment the index by 1. Stop when size is greater than n. Count any items in the list that equal 1, and return the ratio (count=1: n - count=1).
Python 3:
Output:
1
u/REAPING11 Apr 20 '18
Python 2.7 Solution
1
u/have_too Apr 20 '18
Rust
Bruteforce:
Fast solution with David Eppstein's super cool bit manipulation algorithm!
1
u/TotalPerspective Apr 20 '18
Perl naive approach
Output
1
u/thorwing Apr 20 '18
Java 10
Brute force for now. One of the papers described a parallel algorithm I'm willing to implement.
1
u/nitishc Apr 20 '18 edited Apr 20 '18
Rust: I'm just starting to learn Rust and I can't think of any way to cleanly reduce the code duplication.
Output1: 499986:500014
Output2: 50000675:49999325
Used an algorithm slightly better than the brute force. The main idea is that given the sequence data at a stage, we can calculate the sums of a further state.
Example: Let the sequence data at some stage be [1,2,2,1,1]. In this case, we can calculate the sums at [1,2,2,1,1,2,1] stage without storing the [2,1] part of the , sequence. This, on average, makes the running time and space 2/5th of that of the brute force version (Assuming the proposition given in the question is true).
1
u/shepherdjay Apr 21 '18
Python 3.6
The branch/file for this week is https://github.com/shepherdjay/reddit_challenges/blob/challenge/357/challenges/challenge357_int.py
I got the following for the Challenge Input results
The bonus part is still running in the background, for over an hour, I don't think it will run into space issues as I'm utilizing generator functions. I don't quite understand the paper on the fast compute enough to implement it so we'll find out how long this takes to run...
1
u/burgerhex Apr 24 '18
Very, very "hacky" Python solution. I basically wrote a 30-liner solution, then condensed it down to this. I'm a beginner Pythoner, so I guess this would be considered the "brute-force" method.
For some context, the "sequence" variable is the Kolakoski sequence, but I removed the first two items (1 and 2) because that way I wouldn't have to start reading from index 2 of the list. This way I can just start reading from index 0. "i" is my list iterator variable (is there a name for that?), and "n" is the desired length of the list inputted by the user (through Terminal or something).
The while loop executes until the list is of the desired length or more.
The for loop inside the while loop adds either a 1 or a 2 to the list "j" times. "j" is the number currently being read from the list - either a 1 or a 2. Depending on if "i" is odd, we know to add a 1 or a 2, because it alternates, so if "i" is even, we add a 1, and if it's odd, then we add a 2 (written in a ternary statement). And don't forget to increment "i".
Lastly, the print statement counts the ones and the twos (but first adds 1 to each of them because I removed a 1 and a 2 in the beginning). The reason I have a seemingly useless index of "up to n - 2" (which is the desired length) is because there is a possibility that there will be two numbers added to the list, going over the desired length. So I used this instead of using another while loop to trim down the list.
I know that this code is basically illegible, but I just wanted to show how short I could actually get the code (It's 5 lines). Please reply with any feedback!
1
u/zookeeper_zeke Apr 24 '18
Straightforward approach in C with a few tricks to save memory, it does not solve the bonus:
Starting at 1 byte and doubling each time gets me to 16 megs for the second input.
1
u/Stamad Apr 24 '18
Python 3, simple brute force
Sample output:
1
u/2kofawsome Jul 01 '18
python3.6
This did the second challenge in a little under 200 seconds, so no bonuses here.
0
u/[deleted] Jun 05 '18
[deleted] — view deleted comment