12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import java.lang.ArithmeticException;
- public class SummationOfPrimes {
- private long num;
- private long sum;
- public SummationOfPrimes(long num) {
- this.num = num;
- this.setSum();
- }
- public long getSum() {
- return this.sum;
- }
- private void setSum() {
- long sum = 0;
- for (long i = 0; i < this.num; i++) {
- if ( isPrime(i) ) {
- sum += i;
- }
- else
- continue;
- }
- this.sum = sum;
- }
-
- private static boolean isPrime(long num) {
-
- if (num == 0 || num == 1)
- return false;
-
- else if (num == 2)
- return true;
-
- else {
- for (long i = 2; i < num; i++) {
- if (num % i == 0)
- return false;
- }
- }
-
- return true;
- }
- }
|