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. int sqrt = (int) Math.sqrt(num);
  15. for(int i = 1; i < primes.length && primes[i] <= sqrt; i++) {
  16. if(num % primes[i] == 0) {
  17. prime = false;
  18. break;
  19. }
  20. }
  21. if(prime) {
  22. primes[found++] = num;
  23. }
  24. num += 2;
  25. }
  26. return primes;
  27. }
  28. }
Runtime error #stdin #stdout #stderr 0.07s 380160KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at Main.firstPrimes(Main.java:16)
	at Main.main(Main.java:4)