Guest User

Untitled

a guest
Apr 26th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. def primes(long upto) {
  2. def primes=[];
  3. // We only search primes below sqrt, as a result of algrebraic well knwon results
  4. long max = Math.max(Math.sqrt(upto), 1L);
  5. for(tested in 2..max) {
  6. long maxTested = Math.sqrt(tested);
  7. boolean isPrime = true;
  8. for(i in primes) {
  9. if(isPrime) {
  10. if(i<=maxTested) {
  11. isPrime = tested%i!=0;
  12. } else {
  13. break;
  14. }
  15. } else
  16. break;
  17. }
  18. if(isPrime) {
  19. if(primes.size()%1000==0)
  20. println "found new prime $tested (the "+primes.size()+"th)";
  21. primes << tested;
  22.  
  23. } else {
  24. if(tested%10000==0) {
  25. println "reached $tested";
  26. }
  27. }
  28. }
  29. return primes;
  30. }
  31.  
  32. long start = System.currentTimeMillis();
  33. NUMBER = 131900005;
  34. def p = primes(NUMBER);
  35. // println "found primes "+p.join(", ");
  36. primeDividors = p.findAll {n -> NUMBER%n==0};
  37. println primeDividors.join(", ");
  38. long end = System.currentTimeMillis();
  39. println "duration "+((end-start)/1000.0)+" s";
Add Comment
Please, Sign In to add comment