View Single Post
  #4 (permalink)  
Old 10-16-2008, 04:37 AM
eNine eNine is offline
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 ; }
Reply With Quote