## 20 Replies - 1311 Views - Last Post: 06 December 2012 - 10:07 PM

### #1

# very large integer arithmetic implementation

Posted 06 December 2012 - 09:44 AM

##
**Replies To:** very large integer arithmetic implementation

### #2

## Re: very large integer arithmetic implementation

Posted 06 December 2012 - 10:11 AM

### #3

## Re: very large integer arithmetic implementation

Posted 06 December 2012 - 10:20 AM

### #4

## Re: very large integer arithmetic implementation

Posted 06 December 2012 - 10:35 AM

int carry = 0; for(int i = num1.length; --i >= 0; ) { num3[i] = num2[i] + num1[i] + carry; carry = num3[i] / 10; num3[i] %= 10; }

or if you keep them as bin

int carry = 0; for(int i = num1.length; --i >= 0; ) { num3[i] = num2[i] + num1[i] + carry; carry = num3[i] / 2; num3[i] %= 2; }

### #5

## Re: very large integer arithmetic implementation

Posted 06 December 2012 - 10:43 AM

### #6

## Re: very large integer arithmetic implementation

Posted 06 December 2012 - 10:57 AM

I gave you the code to perform a add in both situation, if you keep it as decimal or if you keep it as binary. They are bot really easy

### #7

## Re: very large integer arithmetic implementation

Posted 06 December 2012 - 11:13 AM

### #8

## Re: very large integer arithmetic implementation

Posted 06 December 2012 - 11:29 AM

Wanting to implement the shift and rotate would by itseleft justify the ninary approach.

For the multiplication, the easiest way, you will have to do as we did when we where kids, (trick: declare an array for every row />)

01011101 < num1 * 11001100 < num2 ------------ 00000000 < num3 00000000 < num4 01011101 01011101 .... .-------------- later on the sum of all the rows starting at num3

Fun project />

This post has been edited by **pbl**: 06 December 2012 - 12:20 PM

Reason for edit:: Fixed code tags

### #9

## Re: very large integer arithmetic implementation

Posted 06 December 2012 - 12:17 PM

### #10

## Re: very large integer arithmetic implementation

Posted 06 December 2012 - 01:01 PM

You can also use a List<Integer> for this, which means you don't have to care how big it gets. For binary, you could even play with List<Boolean>.

### #11

## Re: very large integer arithmetic implementation

Posted 06 December 2012 - 01:14 PM

overload the get() method. If the index received as parameter is >= size() simply return 0

### #12

## Re: very large integer arithmetic implementation

Posted 06 December 2012 - 02:30 PM

### #13

## Re: very large integer arithmetic implementation

Posted 06 December 2012 - 02:51 PM

baavgai, on 06 December 2012 - 01:01 PM, said:

You can also use a List<Integer> for this, which means you don't have to care how big it gets. For binary, you could even play with List<Boolean>.

just wondering, what is the benefit of this? Also does List<Integer> grow as u add more elements to it?

### #14

## Re: very large integer arithmetic implementation

Posted 06 December 2012 - 04:35 PM

### #15

## Re: very large integer arithmetic implementation

Posted 06 December 2012 - 04:46 PM

mastapro, on 06 December 2012 - 04:51 PM, said:

Yep. That is part of the benefit.

You mightn't entirely see the benefit of going backwards until you actually attack the problem.

Consider pbl's code, going a natural direction. Then, consider the reverse.

int carry = 0, pos = 0; while(pos<num1.size() && num2.size()) { int value = num2[pos] + num1[pos] + carry; num3[pos++] = value % base; carry = value / base; } // a couple extra whiles if (carry!=0) { num3[pos++] = carry; }

That last bit of logic can be used to extend num3 if it's a list. Going the other direction, you'll need to shift some numbers around.

This post has been edited by **baavgai**: 06 December 2012 - 04:46 PM