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_3.cpp
- Link : http://projecteuler.net/problem=10
- Description: My third attempt after understanding project euler problem 10_2.cpp
- *************************************************
- The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
- Find the sum of all the primes below two million.
- *************************************************/
- #include <iostream>
- #include <math.h>
- using namespace std;
- bool primeOdd(long);//based on only odd number possibility
- int main ()
- {
- long long sum = 2;//include 2 instantly as first prime no, 1 is not a prime no
- long max = 2000000;
- for (long i=3; i<max; i++)
- {
- if (i%2==0)//eliminate even no from calculation
- {
- continue;
- }
- if ( primeOdd(i) )//check prime number
- {
- sum += i;
- }
- }
- cout << "Sum of all prime number below " << max << " is: " << sum;//display
- return 0;
- }
- bool primeOdd(long i)
- {
- long a = 3;//start with first odd no, 1 is not a prime
- double b = sqrt(i);
- while (i%a!=00 && a<b)
- {
- a+=2;//compare with next odd number if not divisible until the number itself
- }
- if ((i%a==00 && i!=a) == 0)
- {
- return true;
- }
- else
- {return false;}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement