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?
code:

BigInteger num = BigInteger.valueOf(317584931803);


it cleary says integer number too large. long long Confused wats that? or is it just 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. Confused

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.

Author:  [Gandalf] [ Wed Jun 21, 2006 9:41 am ]
Post subject: 

You searched the internet, yet you didn't check the Java documentation? Rolling Eyes Hey look, it's also the first thing that Google pops up: here.

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.

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?

Author:  Hikaru79 [ Sun Jun 25, 2006 7:06 pm ]
Post 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 Razz You want to check that num%i==0 .

Author:  cool dude [ Sun Jun 25, 2006 8:34 pm ]
Post subject: 

it says that it is incompatible types Confused

Author:  wtd [ Sun Jun 25, 2006 8:34 pm ]
Post subject: 

Made better. Wink

code:
num.mod(BigInteger.valueOf(i)).compareTo(BigInteger.ZERO)

Author:  cool dude [ Mon Jun 26, 2006 5:32 pm ]
Post subject: 

wtd wrote:
Made better. Wink

code:
num.mod(BigInteger.valueOf(i)).compareTo(BigInteger.ZERO)


how is it better? and it still says incompatible types

Author:  wtd [ Mon Jun 26, 2006 5:39 pm ]
Post subject: 

code:
BigInteger.ZERO


Is better than:

code:
new BigInteger(0)


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 Confused

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.
code:
if (num.mod(BigInteger.valueOf(i)).compareTo(BigInteger.ZERO) == 0) {

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:

code:

compareTo(BigInteger.ZERO)


doesn't that compare to zero already? why do u need to restate == 0 if it already compares it to zero Confused

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 Confused if anyone could make a tutorial that would be great. i tried doing the counted loop but it says:
Quote:
operator < cannot be applied to BigInteger


code:

for(int i = 1; (num.compareTo(BigInteger.valueOf(i)) < num) ; i++){

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.


: