Computer Science Canada too big for BigInteger |
Author: | cool dude [ 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? |
Author: | Martin [ Mon Jun 19, 2006 8:43 pm ] |
Post 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. |
Author: | cool dude [ Mon Jun 19, 2006 9:07 pm ] | ||
Post subject: | |||
did u actually try it?
it cleary says integer number too large. long long ![]() |
Author: | Martin [ Mon Jun 19, 2006 10:00 pm ] |
Post 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. |
Author: | cool dude [ Tue Jun 20, 2006 5:33 pm ] |
Post 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? |
Author: | rizzix [ Tue Jun 20, 2006 6:21 pm ] |
Post 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. |
Author: | cool dude [ Wed Jun 21, 2006 9:20 am ] | ||
Post subject: | |||
i searched on the internet but it doesn't show anything relevant. ![]() this is the line of code i need to compareTo
obviously i can't use the == operator because it doesn't work with BigInteger. |
Author: | [Gandalf] [ Wed Jun 21, 2006 9:41 am ] |
Post subject: | |
You searched the internet, yet you didn't check the Java documentation? ![]() |
Author: | cool dude [ Wed Jun 21, 2006 3:11 pm ] | ||
Post subject: | |||
k thanks i figured it out. the only problem i'm getting is that i'm not getting any output.
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? |
Author: | Hikaru79 [ Sun Jun 25, 2006 7:06 pm ] | ||||
Post subject: | |||||
Methinks you need to rethink your algorithm.
This will only return true if: num == num%i. This is clearly not what you need. I think what you're looking for is:
I'm probably missing brackets there, but that's the idea ![]() |
Author: | cool dude [ Sun Jun 25, 2006 8:34 pm ] |
Post subject: | |
it says that it is incompatible types ![]() |
Author: | wtd [ Sun Jun 25, 2006 8:34 pm ] | ||
Post subject: | |||
Made better. ![]()
|
Author: | cool dude [ Mon Jun 26, 2006 5:32 pm ] | ||
Post subject: | |||
wtd wrote: Made better.
![]()
how is it better? and it still says incompatible types |
Author: | wtd [ Mon Jun 26, 2006 5:39 pm ] | ||||
Post subject: | |||||
Is better than:
That's not the problem, though. Look at the loop counter. |
Author: | cool dude [ Mon Jun 26, 2006 6:17 pm ] |
Post subject: | |
thats how i always made loop counters ![]() |
Author: | zylum [ Mon Jun 26, 2006 6:27 pm ] |
Post subject: | |
for(int i = 1; i <= 317584931803; i++){ 317584931803 isnt exactly an integer... how is i, which is an integer type, supposed to go all the way up to that? |
Author: | cool dude [ Mon Jun 26, 2006 6:32 pm ] |
Post subject: | |
even if i decrease the number it still says incompatible types. try it |
Author: | HellblazerX [ Mon Jun 26, 2006 7:35 pm ] | ||
Post subject: | |||
You just forgot to do any comparing inside that if statement. You called compareTo (), but you never actually used or compared the value that it returned, so that's why its giving you that error.
And zylum's right. That number is too big for an int. Why not use BigIntegers instead in the for loop? |
Author: | cool dude [ Mon Jun 26, 2006 8:32 pm ] | ||
Post subject: | |||
thanks. the thing that i don't understand is that when u say:
doesn't that compare to zero already? why do u need to restate == 0 if it already compares it to zero ![]() |
Author: | wtd [ Mon Jun 26, 2006 9:55 pm ] |
Post subject: | |
Look up the documentation for compareTo. http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Comparable.html |
Author: | cool dude [ Tue Jun 27, 2006 8:26 pm ] | ||
Post subject: | |||
i'm trying to make my counted loop be able to go to a huge number, thus i am trying to use BigInteger again. i dunno why but BigInteger just doesn't seem to stick to my head ![]() Quote: operator < cannot be applied to BigInteger
|
Author: | wtd [ Tue Jun 27, 2006 10:49 pm ] |
Post subject: | |
Java objects do not support operator overloading. Have you tried reading through a basic introduction to Java, to get a grasp on the fundamentals? I ask because the things you keep encountering problems with are pretty basic. |
Author: | cool dude [ Wed Jun 28, 2006 7:42 pm ] |
Post subject: | |
wtd wrote: Java objects do not support operator overloading.
Have you tried reading through a basic introduction to Java, to get a grasp on the fundamentals? I ask because the things you keep encountering problems with are pretty basic. i did read through the beginning parts so far. then it gets a lot more complicated and i want to practice this stuff before i move on to make sure i fully understand it. also no where in your tutorial did you explain how to use BigIntegers. also i still don't understand how i would make a counted loop to go up to such a large number? |
Author: | wtd [ Wed Jun 28, 2006 10:38 pm ] |
Post subject: | |
cool dude wrote: also no where in your tutorial did you explain how to use BigIntegers.
For very good reason. There are 3000+ classes. If I tried to write tutorials for all of them, I'd die of old age before finishing. What I can do is explain the way objects and classes work, and the rules they play by. Then, knowing that, you can figure out the classes you need to use. |