fork download
  1. public class Main {
  2. public static void main(String[] args) {
  3. long time = System.currentTimeMillis();
  4. firstPrimes(1000000);
  5. System.out.println(System.currentTimeMillis() - time);
  6. }
  7. public static int[] firstPrimes(int n) {
  8. int[] primes = new int[n];
  9. int found = 1;
  10. primes[0] = 2;
  11. int num = 3;
  12. while(found < n) {
  13. boolean prime = true;
  14. System.out.println(num);
  15. int sqrt = (int) Math.sqrt(num);
  16. for(int i = 1; i < primes.length && primes[i] <= sqrt; i++) {
  17. if(num % primes[i] == 0) {
  18. prime = false;
  19. break;
  20. }
  21. }
  22. if(prime) {
  23. primes[found++] = num;
  24. }
  25. num += 2;
  26. }
  27. return primes;
  28. }
  29. }
Runtime error #stdin #stdout #stderr 0.06s 380224KB
stdin
Standard input is empty
stdout
3
stderr
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at Main.firstPrimes(Main.java:17)
	at Main.main(Main.java:4)