The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
cevap : 142913828922
#include <stdio.h>
#pragma hdrstop
#include <tchar.h>
//---------------------------------------------------------------------------
#pragma argsused
unsigned char is_number_prime(unsigned long analyses_number, unsigned long *prime_number_counter);
unsigned long long sum_prime_numbers(unsigned long *prime_numbers, unsigned long *prime_number_counter, unsigned long max_number);
int _tmain(int argc, _TCHAR* argv[]) {
unsigned long test,analyses_number,index;
unsigned long long sum_of_prime_numbers;
unsigned long *prime_numbers;
unsigned long i;
unsigned long prime_number_counter;
unsigned long max_number = 2000000;
unsigned long start_point = 2;
prime_numbers = (unsigned long *) calloc(max_number, sizeof(unsigned long ));
memset (prime_numbers, NULL, max_number);
index = 0;
prime_number_counter = 0;
for (analyses_number = start_point; analyses_number < max_number; ++analyses_number) {
if (is_number_prime(analyses_number, &prime_number_counter) == 1) {
prime_numbers[index] = analyses_number;
index++;
}
}
sum_of_prime_numbers = sum_prime_numbers(prime_numbers, &prime_number_counter, max_number);
return 0;
}
//---------------------------------------------------------------------------
unsigned char is_number_prime(unsigned long analyses_number, unsigned long *prime_number_counter) {
unsigned long counter = 0;
unsigned long i;
for (i = 2; i < analyses_number; i ++) {
if (analyses_number%i == 0)
counter++;
}
if (counter != 0)
{
return 0; // not prime number
} else {
*prime_number_counter = *prime_number_counter + 1;
return 1; // prime number
}
}
unsigned long long sum_prime_numbers(unsigned long *prime_numbers, unsigned long *prime_number_counter, unsigned long max_number) {
unsigned long i;
unsigned long long sum_of_prime_numbers = 0;
for (i = 0; i < max_number; ++i)
{
if (i < *prime_number_counter)
{
sum_of_prime_numbers = sum_of_prime_numbers + prime_numbers[i];
}else
{
prime_numbers[i] = NULL;
}
}
return sum_of_prime_numbers ;
}