19
In this challenge you will compute numbers from a curious sequence.
Your input is a single decimal nonnegative integer. Reverse the bits in this integer and then square the number to get the required output.
When reversing the bits you must not use any leading zeroes in the input. For example:
26 (base 10) = 11010 (base 2) -> 01011 (base 2) = 11 -> 11*11 = 121
The first 25 inputs/outputs of this sequence:
0: 0
1: 1
2: 1
3: 9
4: 1
5: 25
6: 9
7: 49
8: 1
9: 81
10: 25
11: 169
12: 9
13: 121
14: 49
15: 225
16: 1
17: 289
18: 81
19: 625
20: 25
21: 441
22: 169
23: 841
24: 9
Your solution should work for arbitrarily sized integers. If your language does not have a convenient built-in method of using those, implement your answer as if it does. You are then excused if your answer breaks for large numbers. However, do not use tricks/bounds that only work for a limited domain (such as a lookup table).
Your score is the number of bytes of source code.
-50% bonus if you never convert the number to/from binary. This is not limited to builtins, if you loop over the number bit by bit (either by shifting or masking or any other method), it will also count as conversion. I don't know whether this is actually possible, but it gives an incentive to spot a pattern in the sequence.
Smallest score wins.
6So close – Conor O'Brien – 2015-11-29T18:54:14.680
1If the code calls a method that results in a character string that represents the bits, is that eligible for the bonus? – Brad Gilbert b2gills – 2015-11-29T19:35:55.460
2@BradGilbertb2gills No. – orlp – 2015-11-29T19:39:34.553
I presume that using math to extract the bits also counts as binary conversion? – lirtosiast – 2015-11-29T20:23:27.047
@ThomasKwa Yes. – orlp – 2015-11-29T20:32:57.707
If the code converts to a hex string, then uses magic to reverse bits in each nybble, then reverses all nybbles in the hex string, does that qualify for the bonus? – Digital Trauma – 2015-11-29T21:36:42.370
@DigitalTrauma No. Only a radically different approach (such as a closed algebraic form) qualifies for the bonus. As I've said in the question - I'm not certain whether that's possible at all. – orlp – 2015-11-29T21:49:37.227
Can I use binary conversion if I'm converting a separate result? (For the bonus) – Conor O'Brien – 2015-11-29T23:17:55.713
2Relevant and relevant – Mego – 2015-11-29T23:47:02.243
This challenge is conversion-to-bits away from a Dock Length 5 Fishing solution:
– Arcturus – 2015-11-30T05:16:02.017IrnSP
.I take it partitioning a number into powers of 2 (e.g. using Mathematica's
IntegerPartition
) also counts as conversion to binary? – Martin Ender – 2015-11-30T10:30:51.603@MartinBüttner Yes. – orlp – 2015-11-30T10:57:45.673