All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > Numbers > Sequences
A Bit of a Problem (Posted on 2004-12-07) Difficulty: 3 of 5
Find the pattern of the following sequence and determine the next few terms:
2, 10, 12, 38, 42, 52, 56, 142, 150, 170

  Submitted by DJ    
Rating: 4.0000 (2 votes)
Solution: (Hide)
The next few terms are: 178, 204, 212, 232, 240, 542, 558

The sequence is comprised of all the numbers which, when expressed in binary, inverting each bit results in the reverse of the original number.

For example, 52 in binary is 110100, which inveretd (ones complement) yields 001011, which is the reverse of the original value.

The following table shows each number, its binary value, and the bitwise inversion of each:

2	10		01
10	1010		0101
12	1100		0011
38	100110		011001
42	101010		010101
52	110100		001011
56	111000		000111
142	10001110		01110001
150	10010110		01101001
170	10101010		01010101
178	10110010		01001101
204	11001100		00110011
212	11010100		00101011
232	11101000		00010111
240	11110000		00001111
542	1000011110	0111100001
558	1000101110	0111010001
598	1001010110	0110101001
614	1001100110	0110011001
666	1010011010	0101100101
682	1010101010	0101010101
722	1011010010	0100101101
738	1011100010	0100011101
796	1100011100	0011100011
812	1100101100	0011010011
852	1101010100	0010101011
868	1101100100	0010011011
920	1110011000	0001100111
936	1110101000	0001010111
976	1111010000	0000101111
992	1111100000	0000011111
And just for kicks, here's the script (Javascript) that generated the above table:
var max = 1000;

for (var i = 1; i <= max; i++) {
   var b = d2b(i);
   if (b == bcr(b)) {
      document.write(i + '\t' + b + '\t' + onescomp(b) + '\n');
   }
}

function log(x, b) {
   return Math.log(x)/Math.log(b);
}

function d2b(n) {
   var r = '';
   var cn = Math.pow(2, Math.floor(log(n,2)));

   while (cn >= 1) {
      if (n >= cn) {
         r += '1';
         n -= cn;
      }
      else {
         r += '0';
      }
      cn /= 2;
   }
   return r;   
}

function b2d(b) {
   var p = 0;
   var r = 0;
   for (var i = b.length; i >= 0; p++) {
      if (b.charAt(--i) == '1')
         r += Math.pow(2, p);
   }
   return r;
}

function onescomp(b) {
   var r = '';
   for (var i = 0; i < b.length; i++) {
      if (b.charAt(i) == '0')
         r += '1';
      else // if (b.charAt(i) == '1')
         r += '0';
   }
   return r;
}

function reverse(x) {
   var r = '';
   for (var i = x.length; i >= 0; r += x.charAt(i--));
   return r;
}

function bcr(x) {
   return reverse(onescomp(x));
}

Comments: ( You must be logged in to post comments.)
  Subject Author Date
Some ThoughtsPuzzle ThoughtsK Sengupta2023-03-18 01:27:56
re(2): Official solutionHugo2004-12-18 23:13:42
Hints/Tipsre: Official solutionDJ2004-12-18 08:36:21
re(2): Official solutionHugo2004-12-17 14:55:52
re: Official solutionnikki2004-12-17 14:36:29
QuestionOfficial solutionHugo2004-12-16 15:23:27
re: Solution (spoiler)Bruce Brantley2004-12-12 11:18:05
Solutionre: HintsBruce Brantley2004-12-12 09:11:21
re(4): HintsLarry2004-12-12 08:27:49
re(3): HintsDJ2004-12-10 23:09:17
re(2): HintsLarry2004-12-10 13:45:22
Solutionre: HintsCharlie2004-12-09 19:57:42
Hints/TipsHintsDJ2004-12-09 18:20:37
Some ThoughtsPossible SolutionReid2004-12-07 20:49:20
Some ThoughtsTwo SolutionsLarry2004-12-07 16:56:40
Solutionre: Solution (spoiler) -- different solutionCharlie2004-12-07 15:47:48
SolutionSolution (spoiler)Larry2004-12-07 15:20:35
One Missing?Hugo2004-12-07 15:16:27
Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (15)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information