• Post Reply Bookmark Topic Watch Topic
  • New Topic

BigInteger Sum  RSS feed

 
Jiyoung Kim
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried to add the numbers from the starting point to the end point what I want.



The end point is the pow of 10, so the number will be so huge if I enter the big number.
So, I guessed that I need to use BigInteger to handle it. However, I'm wondering that there is a way to reduce the time to calculate the summation.
Even I entered 9 or 10, the calculation is slow. I researched some documents of BigInteger, but I didn't have an idea how to modify.

Please let me know some better ways to calculate the sum a little bit faster.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jiyoung Kim wrote:
Please let me know some better ways to calculate the sum a little bit faster.


Using mathematics .... ... the sum of an arithmetic series is equals to the sum of the first term plus the last term, multiplied with the number of terms, divided by two.

So ...



Henry
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware of the pow method for simple exponentiation of integers by integer exponents. You might do well to write your own power method, which will use integer arithmetic and give better precision, as long as you don't suffer an overflow error.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!