Got an iPhone or iPad? We have a brand new Pastebin App for both devices, and it's totally free! Click here to download the new Pastebin App for iOS.
Guest

Untitled

By: a guest on Feb 9th, 2010  |  syntax: Java  |  size: 0.53 KB  |  hits: 165  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  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