r/dailyprogrammer • u/oskar_s • Jun 06 '12
[6/6/2012] Challenge #61 [easy]
The number 19 is can be represented in binary as 10011. Lets define the operation of "rotating a number" as taking the last binary digit of that number and moving it so it becomes the first binary digit, and moving the other digits one step forward. I.e. if you rotate 10011, you get 11001 (i.e. 25), because the 1 that was in the last position has now moved to the first position. If you rotate it again, you get 11100 (i.e. 28).
If you rotate it again, something curious happens: you get 01110, which is the same as 1110 (i.e. 14) since leading zeroes don't count in a binary representation. That is to say, when you rotate it this time, the zero disappears. If you rotate it once more, you get 0111, which is the same as 111 (i.e. 7). Again, the zero has disappeared.
After that, the number remains the same regardless of how much you rotate it, since the binary number representation of 7 only has 1's in it.
This gives us a sequence of numbers. Starting with 19 and then rotating it step by step until we get a number with only 1's in the binary representation, we get
19 -> 25 -> 28 -> 14 -> 7Lets call this a "binary rotation sequence" for 19. Here are the binary rotation sequences for the numbers 69, 205 and 357, with the numbers written first in decimal and then in binary to show what is going on:
69 -> 98 -> 49 -> 56 -> 28 -> 14 -> 7
1000101 -> 1100010 -> 110001 -> 111000 -> 11100 -> 1110 -> 111
205 -> 230 -> 115 -> 121 -> 124 -> 62 -> 31
11001101 -> 11100110 -> 1110011 -> 1111001 -> 1111100 -> 111110 -> 11111
357 -> 434 -> 217 -> 236 -> 118 -> 59 -> 61 -> 62 -> 31
101100101 -> 110110010 -> 11011001 -> 11101100 -> 1110110 -> 111011 -> 111101 -> 111110 -> 11111Write a program that given a number will print out the binary rotation sequence for that number (you only need to print out the sequence in decimal).
What is the binary rotation sequence for 54321?
3
u/ashashwat Jun 07 '12 edited Jun 07 '12
This problem can be solved without resorting to converting to and fro into binary. It is a mathematical sequence, where Tn+1 = (Tn & 1) * (2 ^ [(log2 Tn)]) + (Tn / 2) unless Tn + 1 == (2 ^ [(log2 (Tn + 1))]).
Logic: Whenever a number terminates, it only consist of 1. Which means a number in the form of 2n - 1 is the final number. We can add one to the number and check whether it is power of 2 for terminating condition.
Now all we need to do is,
Example, in case of 69 [1000101],
rightmost_bit = 69 & 1 = 1 [ 1000101 & 1 ]
shift_right_by_1 = 69 >> 1 = 34 [1000101 >> 1 = 100010 ]
closest_power_of_two = 2 ^ floor[log (69, base=2)] = 64 [ 1000000 ]
Next digit will be, (rightmost_bit * closest_power_of_two) + shift_right_by_1 = [64 * 1 + 34 = 98]
In Haskell,
Output,
3
u/bh3 Jun 09 '12
C:
4
u/xjtian Jun 06 '12
Python:
Result:
2
u/UnreasonableSteve Jun 07 '12
Jeez, got enough python up in here?
Output is the same as everyone else's :P. I'm fairly surprised PHP doesnt have any rotation builtins, actually.
2
u/exor674 Jun 06 '12
C++ with GCC builtins:
Results: ( reformatted )
1
u/SwimmingPastaDevil 0 0 Jun 06 '12 edited Jun 06 '12
Output:
1
u/tehstone Jun 06 '12
1
u/Arthree Jun 06 '12
Wow, so much Python in here today...
Autohotkey_L:
Answer:
1
u/emcoffey3 0 0 Jun 06 '12
C#
Snippet from my Main()...
...yields the following output:
1
u/iluminumx Jun 06 '12
1
u/[deleted] Jun 07 '12
Pretty basic perl. Getting back into the groove...
1
u/0x68656c6c6f Jun 07 '12
Java:
1
u/ixid 0 0 Jun 07 '12
A templated version in D:
1
u/Xadoo 0 0 Jun 07 '12 edited Jun 07 '12
As you can probably tell, I'm still really new with Ruby(<10 hours). Any pointers?
Output
1
u/funny_falcon Jun 07 '12
No arrays or strings, just number arithmetic (upto 64 bit integers) (Ruby version):
1
u/beltorak Jun 07 '12 edited Jun 07 '12
Language: Bash
Time: ~10 mins
Code: http://www.syntax-highlighting.com/p/show?id=4953 (I like syntax highlighting)
Answer:
ugh - i fail at spoiler tags; does every sub have its own syntax??
1
u/robin-gvx 0 2 Jun 07 '12
1
u/[deleted] Jun 07 '12
very verbose C++:
1
u/[deleted] Jun 07 '12
Ruby:
1
u/Duglum Jun 07 '12
Javascript with recursion:
1
u/tsigma6 0 0 Jun 07 '12 edited Jun 07 '12
Less strings and more binary arithmetic!
C++
*Edited for formatting and improvement.
1
u/Medicalizawhat Jun 08 '12
Ruby:
1
u/cdelahousse Jun 11 '12
Javascript w/recursion and bit operations.
1
u/pbl24 Jun 11 '12
Learning Python. Likely not very Pythonic. Generates correct result, however.
Output
1
u/MintyPhoenix Jun 06 '12
Straightforward in Python:
1
u/[deleted] Jun 06 '12
Python:
1
u/rueldotme Jun 06 '12
Python:
Result: