• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding the sum of a 'BigInteger' array  RSS feed

 
Harry Orson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently taking a text file of 100, 50-digit numbers, and reading them in java. I then add each of the 100, 50-digit integers into an array using BigInteger.
My current code:


How can i then add each value of the BigInteger array to make one integer?
 
Nguyen Tuyen
Greenhorn
Posts: 24
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using add method, refer to this tutorial

Example:
 
Harry Orson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nguyen Tuyen wrote:Using add method, refer to this tutorial

Example:


This does not solve the issue. I have an error: "BigInteger.add(BigInteger) line: Not available". Guessing this means your method is not compatible with BigIntegers?
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what BigInteger you are using, but the one in java.math has an add(BigInteger) method.

What is giving you that error?
 
Campbell Ritchie
Sheriff
Posts: 53774
128
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That tutorial doesn't tell us anything we can't find from the documentation. It is unhelpful by not saying that Big‍Integer is immutable, and appears to have copied all its code much of its text verbatim from the Oracle documentation. I do not believe that assigning a BigInteger to 0 will compile, since you cannot box BigIntegers. Try BigInteger.ZERO instead. But sum = sum.add(myArray[i]); looks correct.
I would suggest that you would be better off using a different loop from the ordinary for loop.
* * * * * * * * * * * * * * * * *
I would also suggest that we could use a Stream object instead of the loop. Let us assume we already have a BigInteger[]. Now the most versatile way to get a Stream out of an array is the method of the Arrays class. You can reduce the Stream to a single value with the reduce() method. I have chosen that version of reduce() which takes a, “identity” argument. Fortunately the documentation for these newer Java8 features is well‑written, and it provides a pair of examples for summating Integers. You can do similar things with BigIntegers. What you do is supply an identity, which will be the (default) value you return if no operations ever occur, or an initial value. I would have though 0 is the likeliest default, and there is a 0 already defined in BigInteger, So let's use that as our left operand in reduce:-
... .reduce(BigInteger.ZERO, ...)
Now, what about the right operand? It says it requires a BinaryFunction. Now we don't want an anonymous class, which will involve about ten lines of code, mostly boilerplate, but that would be possible. I tried it on Eclipse, which helpfully gave me a skeleton implementation when I entered new(ctrl‑space), so I filled in the single line needing changing (=line 7) and I got this:-I think t and u were default names chosen by Eclipse. It is customary to use very short identifiers in λs etc.
Now, if you look in the Java™ Tutorials, you find you can reduce that to a λ, and later I shall try reducing that to a method reference. The compiler “knows” which interface I am trying to implement (BinaryOperator), and which method I am implementing (since that interface only has one ”abstract” method, it has to be apply()). Now I can delete quite a lot of that code:-
new BinaryOperator<BigInteger>() { @Override public BigInteger apply(BigInteger t, BigInteger u) { return t.add(u); } }
That leaves us with:-
(BigInteger t, BigInteger u) t.add(u)
That won't compile until we replace the return keyword with what is called the arrow token, which I believe is read, “goes to”:-
(BigInteger t, BigInteger u) -> t.add(u)
Now, the compiler “knows” that we are dealing with BigIntegers throughout, so can we delete the types? Let's try it:-
(t, u) -> t.add(u)
And can I replace that λ with a method reference, simply writing BigInteger::add? We't try that too we now have four versions of the code:-Note that I have indented the code differently: all the dot operators align vertically. You shou‍ld find all four versions of my code will compile and run with the same result, once you have sorted out the imports, but I think experienced programmers would only use versions 3 and 4.
 
Campbell Ritchie
Sheriff
Posts: 53774
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:I don't know what BigInteger you are using . . .
Does that mean the OP has written his own “Big‍Integer” class?
 
Campbell Ritchie
Sheriff
Posts: 53774
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earlier today, I wrote:. . . Big‍Integer is immutable . . .
Please search for Joshua Bloch's confession about BigI‍nteger, Big‍Decimal and immutability.
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Dave Tolls wrote:I don't know what BigInteger you are using . . .
Does that mean the OP has written his own “Big‍Integer” class?


No idea, but that error is not one I've seen before:
"BigInteger.add(BigInteger) line: Not available"

That doesn't look like a compilation error, or runtime problem, hence my question of where that message is coming from.
 
Harry Orson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nguyen Tuyen wrote:Using add method, refer to this tutorial

Example:

After some further searching i found you method to be perfectly fine. The issue was my part of the code...
The cause of the issue was this line:
The line: 
I must have forgotten that I am searching for BigIntegers within the text file. This method searches for just integers..quite annoying that I didn't see something so simple buy it is what it is.
Thank you for your solution!
 
Harry Orson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Dave Tolls wrote:I don't know what BigInteger you are using . . .
Does that mean the OP has written his own “Big‍Integer” class?

Thank you so much for the extremely detailed explanation! I wasn't expecting such a reply!
As for your code, i have taken note. I was not aware of methods to reduce the code so I shall try to implement it in my new solution.
 
Campbell Ritchie
Sheriff
Posts: 53774
128
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harry Orson wrote:. . .
I must have forgotten that I am searching for BigIntegers within the text file. This method searches for just integers.. . . .
I think the correct way to do it would be something like this:-You said it searches only for integers, which isn't quite right. There are four similar words:-
  • integer (small i). A whole number (usually including negative numbers). This is a mathematical abstraction rather than a Java® implementation.
  • int. A Java® primitive represenating a two's complement integer in 32 bits. It has a restricted range −2³¹...2³¹−1
  • Integer (Capital I). A Java® class representing an int, with the same range.
  • BigInteger. A Java® class representing integer numbers with a range limited by the computer's memory and the largest size permissible of an array.
  • Be specific about what you say; ints, Integers and BigIntegers all have a range which is aa subset of the range of integers. integers (small i) have an infinite range.
    Scanner is designed to look for built‑in Java® types, so it has hasNexXXX and nextXXX methods for quite a lot of them.
    If you enter something which can be a BigInteger, the Scanner will continue to read; as soon as it encounters something not a BigInteger, i.e. anything you can't write as an integer, it will stop reading because the loop will terminate. Remember that anything which is an int will also be a BigInteger.
    The strange try ensures the Scanner is correctly closed; never use that for System.in.
    Once you have the List, you can do the summation on that List.

    I would suggest you look at different sorts of loop; the plain simple for loop might not be the best in all circumstances.
     
    Harry Orson
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Harry Orson wrote:. . .
    I must have forgotten that I am searching for BigIntegers within the text file. This method searches for just integers....
    I think the correct way to do it would be something like this:-

    Yes as soon as I noticed the error I changed it to a BigInteger, I just didn't post the new code.
    As for the specifying which integer, I had not actually thought about this previously so thank you for pointing that out. I forget the different types so I shall try to be more specific in future.
    Thank you for all the pointers, helps the noobs like me a lot.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!