Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-21-2008, 08:36 PM
Member
 
Join Date: Feb 2008
Posts: 8
perito is on a distinguished road
Finding Largest Prime Factor
Im trying to solve this problem
Quote:
The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?
My code is this:
Code:
import java.math.BigInteger; public class LargestPrimeFactor { public static boolean isPrime(BigInteger nbr) { boolean isNPrime=true; BigInteger i = BigInteger.valueOf(2); while (i.compareTo(nbr.divide(BigInteger.valueOf(2))) < 0) { if ((nbr.compareTo(i) != 0) && (nbr.mod(i).equals(BigInteger.ZERO))) { isNPrime = false; break; } i=i.add(BigInteger.ONE); } if (isNPrime == true) return true; else return false; } public static void main(String[] args) { String strNum = "600851475143"; BigInteger num = BigInteger.valueOf(Long.parseLong(strNum)); BigInteger i = BigInteger.valueOf(Long.parseLong(strNum)-1); BigInteger answer=BigInteger.ZERO; while(i.compareTo(BigInteger.ONE)>0) { if (num.mod(i).equals(BigInteger.ZERO) && isPrime(i)==true) { answer=i; break; } i=i.subtract(BigInteger.ONE); } System.out.println(answer); } }
I think this code will work but it will take FOREVER to work, I waited about an hour and got no results...
anyone have any alternative code or any idea to get the right answer?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-24-2008, 07:33 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.util.*; public class FindingPrimes { public static void main(String[] args) { System.out.printf("int max value = %d%n" + "long max value = %d%n" + "given value = %d%n", Integer.MAX_VALUE, Long.MAX_VALUE, 600851475143L); System.out.println(); // Generate and store the first 1000 prime numbers. boolean print = false; long[] primes = new long[1000]; int count = 1; int index = 0; if(print) System.out.printf(" %5d, ", 2); primes[index++] = 2; for(int i = 1; i < 7920; i++) { for(int j = 2; j < i; j++) { if(i % j == 0) break; if(j == i-1) { primes[index++] = i; if(print) { System.out.printf(" %5d, ", i); if(++count > 9) { count = 0; System.out.println(); } } } } } // Try to factor the given value with these primes. long value = 600851475143L; System.out.println("------ factor " + value + " ------"); List<Long> factors = new ArrayList<Long>(); for(int i = 0; i < primes.length; i++) { if(value % primes[i] == 0) { System.out.printf("primes[%3d] = %4d%n", i, primes[i]); factors.add(Long.valueOf(primes[i])); } } System.out.printf("%s%n", factors); long product = 1; for(int i = 0; i < factors.size(); i++) { product *= factors.get(i).longValue(); } System.out.printf("product = %d%n", product); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-26-2008, 08:04 AM
Member
 
Join Date: Mar 2008
Posts: 2
blackhole8746 is on a distinguished road
You seriously waited for an hour?!! I'm a beginner too but if you wait that long do two things:

1- Check your output statements (And no I don't think you're stupid, we all make that one silly mistake)
2- Check your while loops. If any while loop is infinite, or you forgot to increment one of the variables you're using in the loop body the program will not run.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-16-2008, 04:37 AM
Member
 
Join Date: Aug 2007
Location: new yrok
Posts: 3
eNine is on a distinguished road
here are two methods from a class that I wrote...these might be helpful. you'd still need to do a little work...

Code:
public boolean isPrime(long p){ long pAsALong = p; long mySqrt = (long)Math.sqrt(pAsALong ); long divisor = 3; if(p%2 == 2) return true; else if(p%2 == 0) return false; while(divisor <= mySqrt ) { if(pAsALong % divisor == 0) return false; //only check odd numbers divisor+=2; } return true; } //@return array list of longs that are prime factors public ArrayList<Long> listPrimes(long upToWhat){ ArrayList<Long> listOfPrimes = new ArrayList<Long>(20); listOfPrimes.add(2L);//add two //only add odd numbers for(long potentialPrime = 3;potentialPrime<= upToWhat; potentialPrime+=2){ if(isPrime(potentialPrime)) listOfPrimes.add(potentialPrime); } return listOfPrimes ; }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-16-2008, 08:10 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,402
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by perito View Post
I waited about an hour and got no results...
Amazing, why are you stay that much time for a result. If your application take an hour to give an output how can it become useful at all. Best thing to do in such a case is debug the code and see where you hang on. Specially on loops those things can be happen.

Check hardwireds' code. All what you want is there.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Computing prime numbers in Java Java Tip java.lang 0 04-12-2008 10:39 PM
Prime numbers gapper New To Java 3 02-07-2008 12:09 PM
Finding largest and smallest integer mlhazan New To Java 2 01-13-2008 12:30 AM
ArrayList problem (finding largest no) bugger New To Java 3 12-12-2007 02:47 PM
Finding largest no bugger New To Java 11 11-29-2007 02:49 PM


All times are GMT +3. The time now is 04:42 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org