Fold and Squash
Description:
Based on the Smale horseshoe map.
Summary
Return the number of cycles of the binary representation of an integer under the fold and squash
mapping (defined below).
Details
The fold and squash
map is defined as the following transformation of a binary pattern:
Take a binary pattern and split it in half (add a
0
to the beginning if there are an odd number of digits).11001001 --> (1100, 1001)
Fold the first half back onto the second half as follows (remember that folding it back reverses the order):
0011 1001
Squash the two parts together, with the upper digits occupying the place to the left of the lower digits, as follows:
01001011
At the end of every iteration, you should have the same number of bits as you started with; in other words, do not remove leading zeros.
The number of cycles is defined as the number of iterations of this map that are required to return to the original binary representation. For example the number of cycles of 11001001
is 4
:
Start bits: 11001001
Iteration 1: 01001011
Iteration 2: 01001101
Iteration 3: 01011001
Iteration 4: 11001001
Write a function that takes an integer as an input and returns the number of cycles of the binary representation under this map.
cycles(201) -> 4
All inputs will be positive integers.
Similar Kata:
Stats:
Created | Oct 15, 2015 |
Warriors Trained | 54 |
Total Skips | 8 |
Total Code Submissions | 180 |
Total Times Completed | 17 |
JavaScript Completions | 17 |
Haskell Completions | 2 |
Total Stars | 3 |
% of votes with a positive feedback rating | 54% of 12 |
Total "Very Satisfied" Votes | 5 |
Total "Somewhat Satisfied" Votes | 3 |
Total "Not Satisfied" Votes | 4 |
Total Rank Assessments | 12 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 6 kyu |
Lowest Assessed Rank | 8 kyu |