document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /************************************************
  2. Programmer : Muhammad Azri bin Jasni @ Abdul Rani
  3. Program    : project euler problem 10_2.cpp
  4. Link       : http://projecteuler.net/problem=10
  5. Description: My second attempt by refering to answer available in internet
  6. Note       : Must use long long to store the sum. or it will overflow.
  7. *************************************************
  8. The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
  9.  
  10. Find the sum of all the primes below two million.
  11. *************************************************/
  12. #include <iostream>
  13. #include <math.h>
  14. using namespace std;//im using devc++
  15. //bool isPrime(long);
  16.  
  17. int main()//a good practice to use int main() instead of void main()
  18. {
  19.     /*based on this : http://kahthong.com/2011/12/project-euler-problem-10*/
  20.     long long sum = 2;
  21.     long max = 2000000;
  22.     for (long i=2;i<max;i++)
  23.     {
  24.         if (i % 2 != 1)
  25.         {
  26.            continue;
  27.         }
  28.         long d = 3;
  29.         double x = sqrt(i);
  30.         while ( (i%d != 0) && (d<x) )
  31.         {
  32.             d += 2;
  33.         }
  34.         if (((i%d==0&&i!=d)*1)==0)
  35.         {
  36.             sum+=i;
  37.         }
  38.     }
  39.     cout << sum << endl;
  40.     //system("PAUSE");
  41.     return 0;
  42. }
');