-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem_050.c
72 lines (58 loc) · 1.33 KB
/
problem_050.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define N 1000000
uint8_t sieve[N];
/* init_sieve: initialize the array to test if a number is prime */
void init_sieve(void)
{
int32_t i, j;
for (i = 0; i < N; ++i)
sieve[i] = 1;
sieve[0] = sieve[1] = 0;
for (i = 2; i < N; ++i)
if (sieve[i])
for (j = 2*i; j < N; j += i)
sieve[j] = 0;
}
/* find_longest_prime_sum: find the prime number that can be written with the
* longest sum of consecutive primes */
int32_t find_longest_prime_sum(void)
{
int32_t i, j, n_primes, longest_length, longest_sum, sum, *primes;
/* count number of primes */
n_primes = 0;
for (i = 0; i < N; ++i)
if (sieve[i])
++n_primes;
/* regroup primes into a single array */
primes = malloc(sizeof *primes * n_primes);
for (i = j = 0; i < N; ++i)
if (sieve[i])
primes[j++] = i;
longest_sum = longest_length = 0;
for (i = 0; i < n_primes; ++i)
{
sum = 0;
for (j = i; j < n_primes; ++j)
{
sum += primes[j];
if (sum > N)
break;
/* current sum is a prime and longer than current known
* longest sequence. Save its statistics. */
if (sieve[sum] && j - i > longest_length)
{
longest_length = j - i;
longest_sum = sum;
}
}
}
return longest_sum;
}
int32_t main(void)
{
init_sieve();
printf("Problem 50: %d\n", find_longest_prime_sum());
return 0;
}