patterncppMinorCanonical
Project Euler Problem 10. Sum of all primes < 2mil
Viewed 0 times
problemprimesallprojecteulersum2mil
Problem
I'm new to coding so I can't create complex code yet. I do as much as I can with
I get that this code is unoptimized and does too many unnecessary checks and calculations. Is there a way to optimize the program with just loops and conditions?
NB:
fors and ifs, since while I know a bit about arrays and other topics, I am not familiar enough with them to apply them on my own. This code works, but if I tell it to go to 2 million in main the program never finishes; 200,000 took about a minute, while 20,000 took a few seconds.I get that this code is unoptimized and does too many unnecessary checks and calculations. Is there a way to optimize the program with just loops and conditions?
#include
int nopf(int k){
int sn=0,b;
for(b=2;b<k;b++){
if(k%b==0)sn++;
}
return sn;
}
main(){
int i,s=0;
for(i=2;i<200000;i++){
if(nopf(i)==0)s+=i;
}
cout<<s;
}NB:
nopf is a function to check if a number is prime or not.Solution
One of the fastest way to find primes is with a Sieve, which works like this:
(image courtesy of linked Wikipedia article)
All you have to do is create an array with 2 million elements:
Then you perform the sieve to it:
And sum it:
Result:
(image courtesy of linked Wikipedia article)
All you have to do is create an array with 2 million elements:
#define MAX 2000000
// ...
bool* getSieve()
{
bool isPrimeArray[] = new bool[MAX + 1];Then you perform the sieve to it:
for (int i = 2; i <= n; i++) {
isPrimeArray[i] = true;
}
for (int i = 2; i * i <= n; i++) {
if (isPrimeArray[i]) {
for (int j = i; i * j <= n; j++) {
isPrimeArray[i * j] = false;
}
}
}And sum it:
int index = 0;
long result = 0;
for (boolean isPrime : isPrimeArray) {
if (isPrime) {
result += index;
}
index++;
}
return result;Result:
#include
#define MAX 2000000
bool* getSieve()
{
bool isPrimeArray[] = new bool[MAX + 1];
for (int i = 2; i <= n; i++) {
isPrimeArray[i] = true;
}
for (int i = 2; i * i <= n; i++) {
if (isPrimeArray[i]) {
for (int j = i; i * j <= n; j++) {
isPrimeArray[i * j] = false;
}
}
}
}
int sumAllPrimes()
{
bool* isPrimeArray = getSieve();
int index = 0;
long result = 0;
for (boolean isPrime : isPrimeArray) {
if (isPrime) {
result += index;
}
index++;
}
return result;
}
int main()
{
cout << sumAllPrimes();
}Code Snippets
#define MAX 2000000
// ...
bool* getSieve()
{
bool isPrimeArray[] = new bool[MAX + 1];for (int i = 2; i <= n; i++) {
isPrimeArray[i] = true;
}
for (int i = 2; i * i <= n; i++) {
if (isPrimeArray[i]) {
for (int j = i; i * j <= n; j++) {
isPrimeArray[i * j] = false;
}
}
}int index = 0;
long result = 0;
for (boolean isPrime : isPrimeArray) {
if (isPrime) {
result += index;
}
index++;
}
return result;#include <iostream>
#define MAX 2000000
bool* getSieve()
{
bool isPrimeArray[] = new bool[MAX + 1];
for (int i = 2; i <= n; i++) {
isPrimeArray[i] = true;
}
for (int i = 2; i * i <= n; i++) {
if (isPrimeArray[i]) {
for (int j = i; i * j <= n; j++) {
isPrimeArray[i * j] = false;
}
}
}
}
int sumAllPrimes()
{
bool* isPrimeArray = getSieve();
int index = 0;
long result = 0;
for (boolean isPrime : isPrimeArray) {
if (isPrime) {
result += index;
}
index++;
}
return result;
}
int main()
{
cout << sumAllPrimes();
}Context
StackExchange Code Review Q#114188, answer score: 8
Revisions (0)
No revisions yet.