Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Feb 9th, 2010 | Syntax: Java | Size: 0.53 KB | Hits: 158 | Expires: Never
Copy text to clipboard
  1. //Find the sum of all the primes below two million.
  2.  
  3. package problem10;
  4.  
  5. public class Problem10 {
  6.         public static void main(String[] args) {
  7.                 long sum = 0;
  8.                 for (long i = 0; i < 2000000; i++) {
  9.                         if (isPrime(i)) {
  10.                                 sum += i;
  11.                         }
  12.                         if (i % 10000 == 0) {
  13.                                 System.out.println(i);
  14.                         }
  15.                 }
  16.                 System.out.println(sum);
  17.         }
  18.        
  19.         public static boolean isPrime(long n) {
  20.                 long i = 2;
  21.                 while (i < n) {
  22.                         if (n % i == 0) {
  23.                                 return false;
  24.                         }
  25.                         i++;
  26.                 }
  27.                 return true;
  28.         }
  29. }
  30. //142913828923
  31. //9223372036854775808