OCPJP
Komal Arora wrote:If i store it in a double, then how would i calculate the sum?
Matthew Brown wrote:You need to have a look at the BigInteger class. That stores integers of arbitrary size.
That particular Project Euler problem is trivial in languages that have a BigInteger type or similar. You've got to go to much more trouble if you don't have it - think about how you might implement your own BigInteger if you had to!
Komal Arora wrote:Can i manipulate numbers declared as BigIntegers by the simple Arithmetic operators?
Komal Arora wrote:What if i store the value of 2^1000 as a String. Then i convert it to a character array. Then i calculate the sum of all the digits by visitng each element in the char array and adding it. Would this work? or is this really bad programming?
Winston GutkowskiAs Matthew said: sounds perfectly reasonable; and that first bit can be done in one line with BigInteger. And if you have a String, then the second part of that idea is theoretically redundant, because String has a [tt wrote:charAt(int)[/tt] method; although a for-each loop using String.toCharArray() might be more readable.
OCPJP
OCPJP
Komal Arora wrote:As For BigInteger Class. I went through that and all i understood was that it converts Strings into ints of arbitrary length. But we cannot apply usual arithmetic operators on these ints. quite obvious. But then how do i manipulate that big number. I did not understand the methods of that class pretty well! I will try to explore it more.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here