So I modified a program to search for the 10001th prime, and it won't return. Here's the code:


Code:
import java.util.*;

public class MathUtil{
   
   public static Long gcd(Long x, Long y){
      
      while(x != y){
         
         if(x> y){
            
            x-= y;
            
         }
         else{
            
            y-= x;
            
         }
         
      }
      return x;
      
   }
   
   public static Long lcm(Long x, Long y){
      
      x= Math.abs(x);
      y= Math.abs(y);
      
      while(MathUtil.gcd(x, y)!=1) x= MathUtil.gcd(x, y);
      return x*y;
      
   }
   public static TreeSet<Long> getPrimes(int length){
      
      TreeSet<Long> primes= new TreeSet<Long>();
      primes.add(new Long(2));
      for(Long i=new Long(3); i<length; i++){
         
         boolean save= true;
         
         for(Long j: primes){
            
            if(MathUtil.gcd(i,j)==1){
               
               continue;
               
            }
            else{
               
               save= false;
               break;

               
            }
            
         }
         if(save) primes.add(i);
         
         
      }
      return primes;
      
   }
   
}

I run it with:

Code:

import java.util.TreeSet;


public class test {

   
   public static void main(String[] args) {
      
      TreeSet<Long> output= MathUtil.getPrimes(10001);
      System.out.println(output.last());
         
   }

}


Any ideas? NOTE: I didn't comment 'cause it's pretty self-explanitory. Please ask if you don't get it. And I DON'T want the answer.
seana11 wrote:

Code:
    while(x != y){

That line right there doesn't do what you think it does. Since both x and y are object references, that comparison tests whether x and y refer to the same object (I believe this is what's happening, though I may be wrong).

What you want to do instead is this:

Code:
    while (!x.equals(y)){

Also, this is not a very efficient way to calculate primes. I would use the Sieve of Eratosthenes algorithm instead.

Last, this doesn't actually calculate the 10001th prime number. It calculates the largest prime number that is less than or equal to 10001 (and since 10001 is not prime, that prime number is less than 10001).
Thanks, I changed it and also the < to a <= so it would return all of them. I got 9973.

EDIT

OOps
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement