Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to negate base -2 numbers?

Tags:

java

math

I was given a Codility test lately and I was wondering how can I negate -2 base numbers?

For example the array [1,0,0,1,1] represents 9 in base -2:

-2 bases:

1,-2,4,-8,16

1 + (-8) + 16 = 9

[1,0,0,1,1]

Negative 9 in base -2 is:

-2 bases:

1,-2,4,-8

1 + (-2) + -8 = -9

[1,1,0,1]

I'm in the dark regarding the question. There must be some intuitive solution for this. Do you have any hints?

like image 651
Adam Arold Avatar asked Oct 10 '15 17:10

Adam Arold


People also ask

What is the negation of a number?

The negative version of a positive number is referred to as its negation. For example, −3 is the negation of the positive number 3. The sum of a number and its negation is equal to zero: 3 + (−3) = 0.

Can you have a negative base number?

A negative base (or negative radix) may be used to construct a non-standard positional numeral system. Like other place-value systems, each position holds multiples of the appropriate power of the system's base; but that base is negative—that is to say, the base b is equal to −r for some natural number r (r ≥ 2).

How do you change the base of a number?

Decimal to Other Base System Step 1 − Divide the decimal number to be converted by the value of the new base. Step 2 − Get the remainder from Step 1 as the rightmost digit (least significant digit) of new base number. Step 3 − Divide the quotient of the previous divide by the new base.


2 Answers

In base −2, a 1 at position i means (−2)i.

So, a [1,1] in positions [i,i+1] means (−2)i + (−2)i+1 = (−2)i + (−2)(−2)i = (1 + −2)(−2)i = −(−2)i.

So you can negate any occurrence of a [1,0] by changing it to a [1,1], and vice versa.

Any other occurrences of 0, of course, can be left intact: −0 = 0.

So in your example, we split [1,0,0,1,1] into [{1,0}, {0}, {1,1}], negate each part to get [{1,1}, {0}, {1,0}], i.e., [1,1,0,1,0], and remove the unnecessary high 0, producing [1,1,0,1].

like image 84
ruakh Avatar answered Oct 19 '22 01:10

ruakh


Let's try a few examples:

     (16 -8  4 -2  1)
 1 =   0  0  0  0  1
-1 =   0  0  0  1  1
 2 =   0  0  1  1  0
-2 =   0  0  0  1  0
 3 =   0  0  1  1  1
-3 =   0  1  1  0  1
 4 =   0  0  1  0  0
-4 =   0  1  1  0  0
 5 =   0  0  1  0  1
-5 =   0  1  1  1  1

We can try to define this mathematically:

Given input I(b) (where B is the bit number),

  1. I = ∑(-2)bI(b) -- definition of base -2)
  2. O = -I -- what we're trying to solve for
  3. O = -∑(-2)bI(b) -- substitution
  4. O = ∑-(-2)bI(b) -- distribution
  5. -(-2)b = (-2)b + (-2)b+1
  6. O = ∑((-2)b + (-2)b+1)I(b) -- substitution
  7. O = ∑((-2)bI(b) + (-2)b+1I(b)) -- substitution
  8. O = ∑(-2)bI(b) + ∑(-2)b+1I(b)
  9. O(b) = I(b) + I(b-1)

Now, this leaves the possibility that O(b) is 0, 1, or 2, since I(b) is always 0 or 1.

If O(b) is a 2, that is a "carry", Let's look at a few examples of carries:

       (16 -8  4 -2  1)   (16 -8  4 -2  1)
 1+1 =   0  0  0  0  2  =   0  0  1  1  0
-2-2 =   0  0  0  2  0  =   0  1  1  0  0
 4+4 =   0  0  2  0  0  =   1  1  0  0  0

for each b, starting at 0, if O(b) >= 2, subtract 2 from O(b) and increment O(b+1) and O(b+2). Do this until you reach your maximum B.

Hopefully this explains it in enough detail.

like image 38
Daniel Avatar answered Oct 19 '22 01:10

Daniel