Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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";
Add Comment
Please, Sign In to add comment