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.cpp
  4. Link       : http://projecteuler.net/problem=10
  5. Description: My first attempt, take very long time...in 1 hour... and wrong answer
  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. /* Pseudocode: (This is the brute force style)
  12.  
  13. const long max = 10;//long type is until 2,147,483,647 (2000million, so more than 2 million)
  14.  
  15. long sumOfPrime=2;
  16.  
  17. for (i=3, i<max, i++)
  18. {
  19.     if checkPrime(i)
  20.     {
  21.         sumofPrime=sumOfPrime+i;
  22.     }
  23. }
  24.  
  25. print sumOfPrime
  26. */
  27. #include <iostream>
  28. using namespace std;//im using devc++
  29. bool isPrime(long);
  30.  
  31. int main()//a good practice to use int main() instead of void main()
  32. {
  33.     const long max = 2000000;//long type is until 2,147,483,647
  34.     //const long max =10;
  35.     long sumOfPrime=2;//may be more than 2million, so use long type
  36.  
  37.     for (long i=3; i<max; i++)
  38.     {
  39.         if (isPrime(i))
  40.         {
  41.            sumOfPrime = sumOfPrime+i;
  42.         //   cout <<i<<endl;
  43.         }
  44.      }
  45.     cout << endl << sumOfPrime;
  46.     return 0;
  47. }
  48.  
  49. bool isPrime(long number)//taken from problem 7 http://ifelsewhatever.blogspot.com/2012/07/project-euler-problem-7.html
  50. {    
  51.      int count=0;
  52.      for (int a=1;a<=number;a++)
  53.      {
  54.          if (number%a==0)
  55.          {
  56.             count++;
  57.             if (count>2)     //modified in attempt to reduce time here
  58.                return false; //logically, if it divid
  59.          }
  60.      }
  61.      /*if (count==2)
  62.      {
  63.         return true;
  64.      }
  65.      else
  66.      {
  67.          return false;
  68.      }*/
  69.      return true;
  70. }
');