Consider the following process:
Take some non-negative integer N.
e.g. N =
571
Express it in binary with no leading zeroes. (Zero itself is the only exception, becoming
0
.)e.g.
571
=1000111011
in binaryBreak apart consecutive runs of ones and zeroes in this binary representation.
e.g.
1000111011
→1
,000
,111
,0
,11
Sort the runs from longest to shortest.
e.g.
1
,000
,111
,0
,11
→000
,111
,11
,1
,0
Overwrite all the digits in each run with alternating
1
's and0
's, always starting with1
's.e.g.
000
,111
,11
,1
,0
→111
,000
,11
,0
,1
Concatenate the result to get a new binary number.
e.g.
111
,000
,11
,0
,1
→1110001101
=909
in decimal
When you plot the values produced by this process you get a pretty neat graph:
And it's hopefully apparent why I'm calling the resulting sequence the Temple Skyline sequence:
Challenge
Write a program or function that takes in a non-negative integer N and prints or returns the corresponding Temple Skyline sequence number. Your input and output should both be in decimal.
e.g. If the input is 571
the output should be 909
.
The shortest code in bytes wins.
For reference, here are the terms in the sequence from N = 0 to 20:
0 1
1 1
2 2
3 3
4 6
5 5
6 6
7 7
8 14
9 13
10 10
11 13
12 12
13 13
14 14
15 15
16 30
17 29
18 26
19 25
20 26