Skip to content

Instantly share code, notes, and snippets.

@Riduidel
Created December 24, 2009 10:06
Show Gist options
  • Save Riduidel/263120 to your computer and use it in GitHub Desktop.
Save Riduidel/263120 to your computer and use it in GitHub Desktop.
def primes(long upto) {
def primes=[];
// We only search primes below sqrt, as a result of algrebraic well knwon results
long max = Math.max(Math.sqrt(upto), 1L);
for(tested in 2..max) {
long maxTested = Math.sqrt(tested);
boolean isPrime = true;
for(i in primes) {
if(isPrime) {
if(i<=maxTested) {
isPrime = tested%i!=0;
} else {
break;
}
} else
break;
}
if(isPrime) {
if(primes.size()%1000==0)
println "found new prime $tested (the "+primes.size()+"th)";
primes << tested;
} else {
if(tested%10000==0) {
println "reached $tested";
}
}
}
return primes;
}
long start = System.currentTimeMillis();
NUMBER = 131900005;
def p = primes(NUMBER);
// println "found primes "+p.join(", ");
primeDividors = p.findAll {n -> NUMBER%n==0};
println primeDividors.join(", ");
long end = System.currentTimeMillis();
println "duration "+((end-start)/1000.0)+" s";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment