Author |
Message |
cool dude

|
Posted: Tue Jun 27, 2006 9:06 pm Post subject: exponents |
|
|
how would you make an exponent with BigInteger.
i want to get the answer of 2 ^1000 so i need to know how to make this using BigInteger. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
rizzix
|
Posted: Tue Jun 27, 2006 11:37 pm Post subject: (No subject) |
|
|
|
|
|
|
|
 |
wtd
|
Posted: Wed Jun 28, 2006 12:07 am Post subject: (No subject) |
|
|
I can't help myself. Post #4500 is:
 |
|
|
|
|
 |
rizzix
|
Posted: Wed Jun 28, 2006 1:36 pm Post subject: (No subject) |
|
|
or
However you like it. |
|
|
|
|
 |
cool dude

|
Posted: Wed Jun 28, 2006 7:50 pm Post subject: (No subject) |
|
|
rizzix wrote:
it says cannot resolve symbol constructor BigInteger(int)
Can someone please make a tutorial on BigInteger! i would be really grateful. as you can tell from all my posts i am not understanding how to use BigInteger. would you rather me post 100 posts about BigInteger |
|
|
|
|
 |
McKenzie

|
Posted: Wed Jun 28, 2006 9:17 pm Post subject: (No subject) |
|
|
Two problems.
1. You can't make a BigInteger with an int, use "2" (seems foolish, I agree)
2. The pow does take an int not a BigInteger (you would think it would be overloaded) |
|
|
|
|
 |
Martin

|
Posted: Wed Jun 28, 2006 10:13 pm Post subject: (No subject) |
|
|
cool dude wrote: rizzix wrote:
it says cannot resolve symbol constructor BigInteger(int)
Can someone please make a tutorial on BigInteger! i would be really grateful. as you can tell from all my posts i am not understanding how to use BigInteger. would you rather me post 100 posts about BigInteger
Well, here's one way you could do it:
The best way to learn how to use it is just to read the API docs (click on the keywords in the syntax highlighted java code). |
|
|
|
|
 |
rizzix
|
Posted: Thu Jun 29, 2006 1:22 am Post subject: (No subject) |
|
|
Sorry, my mistake. Yes you have to use a string and pow only accepts ints (primitives).
You don't need a tutorial on BigInteger. The problem here is that you're confusing the BigInteger object with a java primitive. There is a distinction between the two. Objects behave differently and primitives behave differently. If you keep that in mind, you'll make less mistakes.
Secondly, you must take a good look at the method signarurs in the Java API docs. Make sure you are passing the right "typed" argumets to the respective methods. That's all.  |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
cool dude

|
Posted: Thu Jun 29, 2006 10:22 am Post subject: (No subject) |
|
|
k thanks. also how would i convert BigInteger to String? i tried this:
code: | String sresult = BigInteger.toString(result); |
and how would i get the length of the BigInteger? |
|
|
|
|
 |
cool dude

|
Posted: Thu Jun 29, 2006 10:38 am Post subject: (No subject) |
|
|
k i figured out how to convert it to string and how to get the length of that string. the only problem now is when i try and run my program it says:
Quote:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
...
this is my code:
code: |
import java.math.*;
public class Problem16{
public static void main (String[] args){
int sum = 0;
BigInteger result = new BigInteger("2").pow(1000);
String sresult = result.toString();
int n = sresult.length();
for (int i = 1; i < n; i ++){
sum = sum + Integer.parseInt((sresult.substring(i,1)));
}
System.out.println (sum);
}
} |
|
|
|
|
|
 |
wtd
|
Posted: Thu Jun 29, 2006 11:20 am Post subject: (No subject) |
|
|
Check out the documentation for the substring method in the String class, and the static parseInt method in the Integer class. |
|
|
|
|
 |
cool dude

|
Posted: Thu Jun 29, 2006 9:46 pm Post subject: (No subject) |
|
|
i changed my code a little around and tried different variations but its still the same error! i did read the documentation for the substring method and i know how it works. i also know from other languages and its pretty much the same. i'm not sure where my problem is though. you keep telling me to read the documentations and i do read them but i can not pinpoint the problem.
Here's my newer code which still doesn't work
code: |
import java.math.*;
public class Problem16{
public static void main (String[] args){
int sum = 0;
BigInteger result = new BigInteger("2").pow(1000);
String sresult = result.toString();
for (int i = 0; i < sresult.length(); i ++){
sum = sum + Integer.parseInt(sresult.substring(i,1));
}
System.out.println (sum);
}
} |
|
|
|
|
|
 |
wtd
|
Posted: Thu Jun 29, 2006 11:20 pm Post subject: (No subject) |
|
|
I suggested investigating two methods.
Do you know how Integer's static parseInt method works? |
|
|
|
|
 |
r.3volved
|
Posted: Fri Jun 30, 2006 9:46 am Post subject: (No subject) |
|
|
First thing I noticed is your for loop. Not much of a problem, but the correct syntax would be:
for(int i = 0; i != sresult.length(); ++i)
it seems to choke when the substring hits a 0 (ZERO)
code: |
import java.math.*;
public class Problem16
{
public static void main (String[] args)
{
int sum = 0;
BigInteger result = new BigInteger("2").pow(1000);
String sresult = result.toString();
for (int i = 0; i != sresult.length(); ++i)
{
sum += Integer.parseInt(sresult.substring(i,i+1));
}
System.out.println(sum);
}
}
|
Try that...debugging the code killed my netbeans
but I also noticed your substring was incorrect |
|
|
|
|
 |
cool dude

|
Posted: Fri Jun 30, 2006 10:04 am Post subject: (No subject) |
|
|
thanks for the help. 2 questions
1) what is the difference between your counted loop and mine? they perform the same thing.
2) i'm now totally confused on the substring because i thought the second digit meant how many letters to take, thus i always put 1. but why i + 1? so if i = 5 then you would take 6 numbers? i only want one number not 6. i don't get how that works  |
|
|
|
|
 |
|