1 package com.sam.twisters.euler; 2 /* 3 * The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. 4 * Find the sum of all the primes below two million. 5 */ 6 public class Proj10 { 7 8 /* Method that returns true if n is prime otherwise returns false*/ 9 /* Use the brute force method! */ 10 /* I have optimized it so that it skips even numbers.*/ 11 public static boolean isPrime(long n){ 12 boolean isPrime = true && (n==2 || n%2!=0); 13 for(long i=3; i<n&&isPrime;i+=2){ 14 if(n%i ==0){ 15 isPrime = false; 16 } 17 } 18 return isPrime; 19 } 20 21 public static void main(String[] args) { 22 long startTime = System.nanoTime(); 23 24 long sum = 0; 25 int n=2; 26 do{ 27 if(isPrime(n)){sum+=n;} 28 n++; 29 }while(n < 2000000); 30 System.out.println(sum); 31 32 long estimatedTime = System.nanoTime() - startTime; 33 System.out.println((float)estimatedTime/1000000000); 34 } 35 }