Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************************
- Programmer : Muhammad Azri bin Jasni @ Abdul Rani
- Program : project euler problem 10.cpp
- Link : http://projecteuler.net/problem=10
- Description: My first attempt, take very long time...in 1 hour... and wrong answer
- *************************************************
- The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
- Find the sum of all the primes below two million.
- *************************************************/
- /* Pseudocode: (This is the brute force style)
- const long max = 10;//long type is until 2,147,483,647 (2000million, so more than 2 million)
- long sumOfPrime=2;
- for (i=3, i<max, i++)
- {
- if checkPrime(i)
- {
- sumofPrime=sumOfPrime+i;
- }
- }
- print sumOfPrime
- */
- #include <iostream>
- using namespace std;//im using devc++
- bool isPrime(long);
- int main()//a good practice to use int main() instead of void main()
- {
- const long max = 2000000;//long type is until 2,147,483,647
- //const long max =10;
- long sumOfPrime=2;//may be more than 2million, so use long type
- for (long i=3; i<max; i++)
- {
- if (isPrime(i))
- {
- sumOfPrime = sumOfPrime+i;
- // cout <<i<<endl;
- }
- }
- cout << endl << sumOfPrime;
- return 0;
- }
- bool isPrime(long number)//taken from problem 7 http://ifelsewhatever.blogspot.com/2012/07/project-euler-problem-7.html
- {
- int count=0;
- for (int a=1;a<=number;a++)
- {
- if (number%a==0)
- {
- count++;
- if (count>2) //modified in attempt to reduce time here
- return false; //logically, if it divid
- }
- }
- /*if (count==2)
- {
- return true;
- }
- else
- {
- return false;
- }*/
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement