-4

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 ...

  • How are you getting from `0 1` to `3 2` and `3 2` to `6 7`? And _technically_ when working with sequences, the different between `0` and `1` is `1`. Whereas the difference between `5` and `4` is `-1`. You have provided too little information. – fubar Oct 22 '17 at 23:19
  • The diference between `2` and `6` is `4`. There are other values in the list that do not differ by `1`, such as `4` `12`. Provide more numbers of the series. – Isac Oct 22 '17 at 23:32
  • Once you figure out the algorithm, try coding it and we can review the code if you need to. The algorithm part is not PHP related. – Nic3500 Oct 22 '17 at 23:37

1 Answers1

1

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));
Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47