fork(5) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. /* Testowanie przy ilu liczbach warto tworzyć sito Eratostenesa
  4.  */
  5. public class Main
  6. {
  7. public static void main(String[] args)
  8. {
  9. int n = 2000000;
  10. if(args.length>0)
  11. {
  12. try
  13. {
  14. n = Integer.parseInt(args[0]);
  15. }
  16. catch(Exception e)
  17. {
  18. }
  19. }
  20. boolean[] notPrimes = new boolean[n+1];
  21. long start = System.nanoTime();
  22. for(int i=2;i*i<=n;i++)
  23. {
  24. if (notPrimes[i])
  25. continue;
  26. for (int j=2*i;j<=n;j+=i)
  27. notPrimes[j] = true;
  28. }
  29. long time = System.nanoTime()-start;
  30. System.out.printf("Tworzenie sita (w ms): %10.3f \n",time/1000000.0);
  31. start = System.nanoTime();
  32. long sum = 0;
  33. for(int i=2;i<n;i++)
  34. {
  35. if(!notPrimes[i])
  36. {
  37. sum+=i;
  38. }
  39. }
  40. time = System.nanoTime()-start;
  41. System.out.printf("Czas sumowania (w ms): %10.3f \n",time/1000000.0);
  42. System.out.println(sum);
  43. }
  44. }
Success #stdin #stdout 0.1s 381312KB
stdin
Standard input is empty
stdout
Tworzenie sita (w ms):     19.345 
Czas sumowania (w ms):      9.167 
142913828922