phphamming-distanceerror-correctionhamming-code

How to generate this series?


What type of series it is and how to generate this by php program?

0 1 3 2 6 7 5 4 12 13 15 14 ...

Observation: The successive difference of the entity is 1

Example:

Difference of 0 and 1 is 1

Difference of 3 and 2 is 1

Difference of 6 and 7 is 1

Difference of 5 and 4 is 1

Difference of 12 and 13 is 1

Difference of 15 and 14 is 1

Please help ...


Solution

  • Its a Decimal Equivalent of Gray code up to n. I have written a code to generate the Gray code for any Number, Use this to generate a series. I have used Javascript, but you can choose any language you want.

       Number.toGrayCode = function(n) {
            if (n < 0) {
                throw new RangeError("cannot convert negative numbers to gray code");
            }
            return n ^ (n >>> 1);
        };
        
       for( var i=0;i<=10;i++)
        console.log(Number.toGrayCode(i));