Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 too big for BigInteger
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cool dude




PostPosted: 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
Sponsor
sponsor
Martin




PostPosted: 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




PostPosted: 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 Confused wats that? or is it just long?
Martin




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: Wed Jun 21, 2006 9:20 am   Post subject: (No 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.
[Gandalf]




PostPosted: Wed Jun 21, 2006 9:41 am   Post subject: (No 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.
Sponsor
Sponsor
Sponsor
sponsor
cool dude




PostPosted: 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




PostPosted: 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 Razz You want to check that num%i==0 .
cool dude




PostPosted: Sun Jun 25, 2006 8:34 pm   Post subject: (No subject)

it says that it is incompatible types Confused
wtd




PostPosted: Sun Jun 25, 2006 8:34 pm   Post subject: (No subject)

Made better. Wink

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




PostPosted: Mon Jun 26, 2006 5:32 pm   Post subject: (No 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
wtd




PostPosted: Mon Jun 26, 2006 5:39 pm   Post subject: (No subject)

code:
BigInteger.ZERO


Is better than:

code:
new BigInteger(0)


That's not the problem, though. Look at the loop counter.
cool dude




PostPosted: Mon Jun 26, 2006 6:17 pm   Post subject: (No subject)

thats how i always made loop counters Confused
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 24 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: