Author |
Message |
cool dude
|
Posted: Mon Jun 19, 2006 7:37 pm Post subject: too big for BigInteger |
|
|
i'm trying to make a program to solve a math problem which is
Quote:
What is the largest prime factor of the number 317584931803?
the problem i'm having is that even BigInteger says its too big of a number so is there any alternatives? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Martin
|
Posted: Mon Jun 19, 2006 8:43 pm Post subject: (No subject) |
|
|
I think there's something wrong with your algorithm, or how you're using BigInt. You could just use a long long though, the number is small enough. |
|
|
|
|
|
cool dude
|
Posted: Mon Jun 19, 2006 9:07 pm Post subject: (No subject) |
|
|
did u actually try it?
code: |
BigInteger num = BigInteger.valueOf(317584931803); |
it cleary says integer number too large. long long wats that? or is it just long? |
|
|
|
|
|
Martin
|
Posted: Mon Jun 19, 2006 10:00 pm Post subject: (No subject) |
|
|
A long long is a 64 bit integer. A long is just an int.
But your problem is that you're declaring BigInt wrong. You're passing it 317584931803 as an integer, which is too big for an integer. Pass it as a string instead and you'll be fine. |
|
|
|
|
|
cool dude
|
Posted: Tue Jun 20, 2006 5:33 pm Post subject: (No subject) |
|
|
thanks using it as a string works. another question. wat syntax do u use with BigInteger to check if it is equal to something or greater/smaller than something? |
|
|
|
|
|
rizzix
|
Posted: Tue Jun 20, 2006 6:21 pm Post subject: (No subject) |
|
|
If you read the docs you'll notice that BigInteger implements Comparable<BigInteger>.
Thus you can easily compare two BigIntegers.
For more info, look up Comparable. |
|
|
|
|
|
cool dude
|
Posted: Wed Jun 21, 2006 9:20 am Post subject: (No subject) |
|
|
i searched on the internet but it doesn't show anything relevant.
this is the line of code i need to compareTo
code: |
if (num.remainder (BigInteger.valueOf(i) == 0)) |
obviously i can't use the == operator because it doesn't work with BigInteger. |
|
|
|
|
|
[Gandalf]
|
Posted: Wed Jun 21, 2006 9:41 am Post subject: (No subject) |
|
|
You searched the internet, yet you didn't check the Java documentation? Hey look, it's also the first thing that Google pops up: here. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
cool dude
|
Posted: Wed Jun 21, 2006 3:11 pm Post subject: (No subject) |
|
|
k thanks i figured it out. the only problem i'm getting is that i'm not getting any output.
code: |
import java.math.*;
public class Problem3{
public static void main(String[] args){
BigInteger num = new BigInteger("317584931803");
for(int i = 1; i <= 317584931803; i++){
if (num.compareTo(num.mod (BigInteger.valueOf(i))) == 0){
System.out.println(i);
}
}
}
} |
the question was:
Quote:
Problem 3
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 317584931803?
|
|
|
|
|
|
Hikaru79
|
Posted: Sun Jun 25, 2006 7:06 pm Post subject: (No subject) |
|
|
Methinks you need to rethink your algorithm.
code: | if (num.compareTo(num.mod (BigInteger.valueOf(i))) == 0){ |
This will only return true if: num == num%i. This is clearly not what you need. I think what you're looking for is:
code: | if ((num.mod (BigInteger.valueOf(i)).compareTo(BigInteger.valueOf(0))) |
I'm probably missing brackets there, but that's the idea You want to check that num%i==0 . |
|
|
|
|
|
cool dude
|
Posted: Sun Jun 25, 2006 8:34 pm Post subject: (No subject) |
|
|
it says that it is incompatible types |
|
|
|
|
|
wtd
|
Posted: Sun Jun 25, 2006 8:34 pm Post subject: (No subject) |
|
|
Made better.
code: | num.mod(BigInteger.valueOf(i)).compareTo(BigInteger.ZERO) |
|
|
|
|
|
|
cool dude
|
Posted: Mon Jun 26, 2006 5:32 pm Post subject: (No subject) |
|
|
wtd wrote: Made better.
code: | num.mod(BigInteger.valueOf(i)).compareTo(BigInteger.ZERO) |
how is it better? and it still says incompatible types |
|
|
|
|
|
wtd
|
Posted: Mon Jun 26, 2006 5:39 pm Post subject: (No subject) |
|
|
Is better than:
That's not the problem, though. Look at the loop counter. |
|
|
|
|
|
cool dude
|
Posted: Mon Jun 26, 2006 6:17 pm Post subject: (No subject) |
|
|
thats how i always made loop counters |
|
|
|
|
|
|