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 public static boolean isPrime(long n){ 9 boolean isPrime = true && (n==2 || n%2!=0); 10 //Optimized-->for(long i=3; i<n&&isPrime;i+=2){ 14 chars added 11 for(long i=3; i<Math.sqrt(n)+1&&isPrime;i+=2){ 12 if(n%i ==0){ 13 isPrime = false; 14 } 15 } 16 return isPrime; 17 } 18 19 public static void main(String[] args) { 20 long startTime = System.nanoTime(); 21 22 long sum = 0; 23 int n=2; 24 do{ 25 if(isPrime(n)){sum+=n;} 26 n++; 27 }while(n < 2000000); 28 System.out.println(sum); 29 30 long estimatedTime = System.nanoTime() - startTime; 31 System.out.println((float)estimatedTime/1000000000); 32 } 33 }