-4

I have looked at the other examples of these questions asked on this website, but they are all quite more complex than I know how to deal with and use. Basically, how do I sum large numbers, or even store large numbers to sum them later, without the use the of BigInteger class? The numbers are so large they won't even fit into long.

The idea I have now is to use a character array, but I have no idea how to to addition with super large numbers even if I am able to store the numbers in a character array. Any help would be great. Thank you.

Subtopic
  • 71
  • 1
  • 11
  • you should use use BigInterger they are meant to be for big numbers and internally they use most efficient way of handling the big numbers. if you want to know implementation details then there are many in open source you can find... – sumeet kumar Nov 07 '15 at 01:30
  • Adding numbers together hasn't changed since you studied elementary school mathematics. – Dawood ibn Kareem Nov 07 '15 at 03:54
  • Yea, the point is not to use the BigInteger class. – Subtopic Nov 07 '15 at 05:29
  • I realize now that the solution is that simple, I was just having a hard time thinking about how to deal with this kind of issue. But I appreciate your helpfulness. – Subtopic Nov 07 '15 at 05:36

2 Answers2

2

Suppose you store a huge number in an array of longs. Then all you need to do is to add each pair up, detect whether there is overflow, then add one to the next pair. This is exactly like how we actually do addition by hand. Suppose we want to compute:

 5879
+6843

What we do is we add 9 and 3 together, it overflows and becomes 2. We then add 7 and 4, and add one from the overflow to get 2. We again overflow. Next we add 8 and 8 + 1 for overflow to get 7, again with overflow. Finally we add 5 and 6 + 1 for overflow to get 2 with overflow. We thus get 12722. Now imagine each of those digits are a long in your array.

To detect overflow: if you have two positive numbers, when added together they produce a negative number only when they overflowed. So you just need to check whether result is less than 0 to detect whether you need to add one to the next pair.

Flying_Banana
  • 2,864
  • 2
  • 22
  • 38
  • 1
    This is probably the point of the assignment, to teach the student how to work things out like this rather than using a library to do the work for them. I remember an assignment from my CS days where we had to compute factorials in a similar manner. The point of the exercise was to make us figure out how to do math like we'd do it on paper. – Mark Sholund Nov 07 '15 at 02:17
  • When I had any of factorial writing questions, I give them the [Binet's formula](http://www.artofproblemsolving.com/wiki/index.php/Binet's_Formula) ;) too bad they didn't ask that sort in the exams... – Flying_Banana Nov 07 '15 at 02:45
  • Thanks Flying_Banana, really helpful:) – Subtopic Nov 07 '15 at 05:34
2

The answer you want:

If you're not going with a Big- class, I suggest you use a String with some parsing, preferably scientific-notation to avoid long String "bibles", and keep your code structure working on logic rather than string as much as possible (because String is slow and heavy in comparison).

EDIT-NOTE: For an example of what I mean by keeping your structure working on logic rather than string, see Flying_Banana's answer.


The right answer:

Learn to use the Big- classes. Whenever possible, don't code easy; do code right!

CosmicGiant
  • 6,275
  • 5
  • 43
  • 58
  • I wish I could learn to use the Big class, except I am not allowed to. That is why I needed to ask this question. – Subtopic Nov 07 '15 at 05:32