wu :: forums (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi)
riddles >> cs >> Base 13 subtraction
(Message started by: minniekp on Aug 17th, 2007, 1:59am)

Title: Base 13 subtraction
Post by minniekp on Aug 17th, 2007, 1:59am
How do you subtract two numbers in base 13 given below:

(A023AC5B)
-(129B5321) to the base 13

This was asked in Adobe written test.

Thanks

Title: Re: Base 13 subtraction
Post by inexorable on Aug 17th, 2007, 2:54am
8A55593A                
can't it be answered by just doing by hand?
the carry will be 13 as its base 13

Title: Re: Base 13 subtraction
Post by towr on Aug 17th, 2007, 3:12am
Simple way is to convert both to integers, subtract, and return to base 13.

Another way is to turn it into an addition, for the number you want to subtract, change every digit to 13- that digit. Add the two numbers base-13 and ermm, add 1 extra for good measure? And mind the overflow.
There's some details to bare in mind (for which I don't have time now); but basically adapt what you'd do for binary.

Title: Re: Base 13 subtraction
Post by Grimbal on Aug 17th, 2007, 5:13am
OK, here is how it's done.  Remember A=10, B=11, etc.  Start from the right.

B-1 = A.
5-2 = 3
C-3 = 12-3 = 9
A-5 = 10-5 = 5

so far so good.

3-B = 3-11 = uh... it gets negative
 so add 13 to 3 and remember the carry
 16-11 = 5

2-1(carry)-9 = ...
 add 13 to 2 = 15, remember carry
 15-1-9 = 5

0-1-2 = ...
 add 13 to 0 = 13, remember carry
 13-1-2 = 10 = A

A-1-1 = 8

put it together: 8A55593A

Title: Re: Base 13 subtraction
Post by towr on Aug 17th, 2007, 7:17am
Here's my approach worked out:

A023AC5B
-129B5321
--------- (modulo 10000000)
A023AC5B
+BA3179AB (12's/C's complement -> 129B5321+BA3179AB = CCCCCCCC)
+     1 (because CCCCCCCC + 1 = 10000000)
---------
18A55593A
--------- mod 10000000
8A55593A

Title: Re: Base 13 subtraction
Post by Grimbal on Aug 17th, 2007, 7:43am
Of course, when you add BA3179AB+1, in fact you add BA3179AB.CCC...
Because CCCCCCCC.CCC... = 0 (mod 100000000)
;)



Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board