-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_50.c
More file actions
54 lines (46 loc) · 1.08 KB
/
problem_50.c
File metadata and controls
54 lines (46 loc) · 1.08 KB
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
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 1000000
static int sieve_init( unsigned int *sieve, unsigned int n )
{
unsigned int i, j;
memset( sieve, 1, ( n + 1 ) * sizeof( unsigned int ) );
sieve[0] = 0;
sieve[1] = 0;
for( i = 2; i <= sqrt( n ); i++ ) {
if( sieve[ i ] ) {
for( j = 2 * i; j <= n; j += i ) {
sieve[ j ] = 0;
}
}
}
}
int main( )
{
unsigned int *sieve = malloc( sizeof( int ) * ( N + 1 ) );
sieve_init( sieve, N );
unsigned int res_r = 0, res;
unsigned int n, next, d = 1;
while( ++d < N ) {
n = d;
if( !sieve[n] ) continue;
unsigned int r = 1;
next = n;
while( n < N ) {
next++;
while( next < N && !sieve[ next ] ) next++;
if( next == N ) break;
n += next;
if( n < N ) {
r++;
if( res_r < r && sieve[n] ) {
res_r = r;
res = n;
}
}
}
}
printf( "Answer: %d\n", res );
}