Draft

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:

  1. 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)
    
  2. Fold the first half back onto the second half as follows (remember that folding it back reverses the order):

    0011
    1001
    
  3. 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.

Fundamentals
Algorithms
Mathematics

More By Author:

Check out these other kata created by em-12

Stats:

CreatedOct 15, 2015
Warriors Trained54
Total Skips8
Total Code Submissions180
Total Times Completed17
JavaScript Completions17
Haskell Completions2
Total Stars3
% of votes with a positive feedback rating54% of 12
Total "Very Satisfied" Votes5
Total "Somewhat Satisfied" Votes3
Total "Not Satisfied" Votes4
Total Rank Assessments12
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • em-12 Avatar
  • Thoms Avatar
Ad