Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

I wrote this simple implementation of the sum of primes for the code eval

Challenge Description:

Write a program which determines the sum of the first 1000 prime numbers.

I would like to know of a better - more efficient - way of implementing the solution.

import java.lang.Math;
public class SumOfPrimes {
 public static boolean isPrime(int n) {
 if (n < 2) {
 return false;
 }
 if (n == 2) {
 return true;
 }
 if (n % 2 == 0) {
 return false;
 }
 for (int i = 3; i <= Math.sqrt((double) n); i += 2) {
 if (n % i == 0) {
 return false;
 }
 }
 return true;
 }
 public static void main(String[] args) {
 long sum = 0;
 for (int i = 1, count = 0; count < 1000; i++) {
 if (isPrime(i)) {
 sum += i;
 count++;
 }
 }
 System.out.print(sum);
 }
}

As for the primality test I only know of these other:

for (int divisor = 2; divisor < n; divisor++) {
 if (n % divisor == 0) {
 return false;
 }
}
return true;

and

for (int divisor = 2; divisor <= n/2; divisor++) {
 if (n % divisor == 0) {
 return false;
 }
}
return true;

I wrote this simple implementation of the sum of primes for the code eval

Challenge Description:

Write a program which determines the sum of the first 1000 prime numbers.

I would like to know of a better - more efficient - way of implementing the solution.

import java.lang.Math;
public class SumOfPrimes {
 public static boolean isPrime(int n) {
 if (n < 2) {
 return false;
 }
 if (n == 2) {
 return true;
 }
 if (n % 2 == 0) {
 return false;
 }
 for (int i = 3; i <= Math.sqrt((double) n); i += 2) {
 if (n % i == 0) {
 return false;
 }
 }
 return true;
 }
 public static void main(String[] args) {
 long sum = 0;
 for (int i = 1, count = 0; count < 1000; i++) {
 if (isPrime(i)) {
 sum += i;
 count++;
 }
 }
 System.out.print(sum);
 }
}

As for the primality test I only know of these other:

for (int divisor = 2; divisor < n; divisor++) {
 if (n % divisor == 0) {
 return false;
 }
}
return true;

and

for (int divisor = 2; divisor <= n/2; divisor++) {
 if (n % divisor == 0) {
 return false;
 }
}
return true;

I wrote this simple implementation of the sum of primes for the code eval

Challenge Description:

Write a program which determines the sum of the first 1000 prime numbers.

I would like to know of a better - more efficient - way of implementing the solution.

import java.lang.Math;
public class SumOfPrimes {
 public static boolean isPrime(int n) {
 if (n < 2) {
 return false;
 }
 if (n == 2) {
 return true;
 }
 if (n % 2 == 0) {
 return false;
 }
 for (int i = 3; i <= Math.sqrt((double) n); i += 2) {
 if (n % i == 0) {
 return false;
 }
 }
 return true;
 }
 public static void main(String[] args) {
 long sum = 0;
 for (int i = 1, count = 0; count < 1000; i++) {
 if (isPrime(i)) {
 sum += i;
 count++;
 }
 }
 System.out.print(sum);
 }
}

As for the primality test I only know of these other:

for (int divisor = 2; divisor < n; divisor++) {
 if (n % divisor == 0) {
 return false;
 }
}
return true;

and

for (int divisor = 2; divisor <= n/2; divisor++) {
 if (n % divisor == 0) {
 return false;
 }
}
return true;
edited tags
Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
Source Link
user69928
user69928

Efficient Sum of Primes

I wrote this simple implementation of the sum of primes for the code eval

Challenge Description:

Write a program which determines the sum of the first 1000 prime numbers.

I would like to know of a better - more efficient - way of implementing the solution.

import java.lang.Math;
public class SumOfPrimes {
 public static boolean isPrime(int n) {
 if (n < 2) {
 return false;
 }
 if (n == 2) {
 return true;
 }
 if (n % 2 == 0) {
 return false;
 }
 for (int i = 3; i <= Math.sqrt((double) n); i += 2) {
 if (n % i == 0) {
 return false;
 }
 }
 return true;
 }
 public static void main(String[] args) {
 long sum = 0;
 for (int i = 1, count = 0; count < 1000; i++) {
 if (isPrime(i)) {
 sum += i;
 count++;
 }
 }
 System.out.print(sum);
 }
}

As for the primality test I only know of these other:

for (int divisor = 2; divisor < n; divisor++) {
 if (n % divisor == 0) {
 return false;
 }
}
return true;

and

for (int divisor = 2; divisor <= n/2; divisor++) {
 if (n % divisor == 0) {
 return false;
 }
}
return true;
lang-java

AltStyle によって変換されたページ (->オリジナル) /