This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with BigInteger  RSS feed

 
Chaturaka Gunatilaka
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When I try to run this code the following error message is coming up.
"The operator + is undefined for the argument type(s) BigInteger,double."
How can I solve this?
Thank you.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use methods of the BigInteger class to add something to a BigInteger; "+" won't work.
 
Chaturaka Gunatilaka
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give me the code of that?I've tried but failed to solve.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chaturaka Gunatilaka wrote:Can you give me the code of that?I've tried but failed to solve.


That's not how it works. If you show your work and TellTheDetails(←click) of what went wrong, people will be happy to guide you toward solving your own problem.
 
Chaturaka Gunatilaka
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Chaturaka Gunatilaka wrote:Can you give me the code of that?I've tried but failed to solve.


That's not how it works. If you show your work and TellTheDetails(←click) of what went wrong, people will be happy to guide you toward solving your own problem.


ok then.

import java.math.BigInteger;
public class AWords{ public static void main(String[]args){
BigInteger sum=null; //BigInteger sum=new BigInteger(null);
for(int i=1;i<=1000;i++){
sum = sum.add(Math.pow(i, i));
System.out.println(sum);
}
}
}
So this is what I've done.but I got an error saying
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method add(BigInteger) in the type BigInteger is not applicable for the arguments (double) at AWords.main(AWords.java:5)
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message is telling you exactly what's wrong. Math.pow() returns a double, but BigInteger's methods (not surprisingly) don't deal with doubles. BigInteger has its own pow() method that you should use. And in any case, you can't fit 1000^1000 into a double anyway.

You've got another problem:



The first time through the loop, what do you think is going to happen when you do sum.add? What is the value of sum the first time you do that?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:The first time through the loop, what do you think is going to happen when you do sum.add? What is the value of sum the first time you do that?

@Chaturaka - Hint: If your 'sum' field was an int instead of a BigInteger, what would you initialize it to?

Winston
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ought not to use Math.pow, because a double will probably run out of range before you reach the 1000th power, unless your value is 0 or ±1.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!