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_3.cpp
  4. Link       : http://projecteuler.net/problem=10
  5. Description: My third attempt after understanding project euler problem 10_2.cpp
  6. *************************************************
  7. The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
  8.  
  9. Find the sum of all the primes below two million.
  10. *************************************************/
  11. #include <iostream>
  12. #include <math.h>
  13. using namespace std;
  14. bool primeOdd(long);//based on only odd number possibility
  15. int main ()
  16. {
  17.     long long sum = 2;//include 2 instantly as first prime no, 1 is not a prime no
  18.     long max = 2000000;
  19.    
  20.     for (long i=3; i<max; i++)
  21.     {
  22.         if (i%2==0)//eliminate even no from calculation
  23.         {
  24.            continue;
  25.         }
  26.         if ( primeOdd(i) )//check prime number
  27.         {
  28.            sum += i;
  29.         }
  30.     }
  31.     cout << "Sum of all prime number below " << max << " is: " << sum;//display
  32.     return 0;
  33. }
  34.  
  35. bool primeOdd(long i)
  36. {
  37.      long a = 3;//start with first odd no, 1 is not a prime
  38.      double b = sqrt(i);
  39.      while (i%a!=00 && a<b)
  40.      {
  41.         a+=2;//compare with next odd number if not divisible until the number itself
  42.      }
  43.      if ((i%a==00 && i!=a) == 0)
  44.      {
  45.         return true;
  46.      }
  47.      else
  48.          {return false;}
  49. }
');