-
Notifications
You must be signed in to change notification settings - Fork 0
/
A001043C.CPP
63 lines (51 loc) · 1.4 KB
/
A001043C.CPP
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
//-------------------------------------------------------------------
//
// a001043c.cpp
//
// Numbers that are the sum of 2 successive primes
//
// OEIS a001043
//
//-------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int max = 1000;
int sqm = (int)sqrt(max) + 1;
int* primes = (int *)malloc(max * sizeof(int));
int* sieve = (int *)malloc(max * sizeof(int));
int* sums = (int *)malloc((max+2) * sizeof(int));
int i = 0;
int sp = 2; // a pointer to the next prime in the array
int pcount = 0;
for (i = 0; i < max; ++i) {
*(sieve + i) = 1;
}
*(sieve + 0) = 0;
*(sieve + 1) = 0;
for (sp = 2; sp <= sqm; ++sp) {
if (*(sieve + sp) != 0) {
int sc = sp + sp;
for (sc = sp+sp; sc < max; sc = sc + sp) {
*(sieve + sc) = 0;
}
}
}
for (i = 0; i < max; ++i) {
if (*(sieve + i) == 1) {
*(primes + pcount) = i;
++pcount;
}
}
for (i = 0; i < (pcount-1); ++i) {
*(sums + i) = *(primes + i) + *(primes + i + 1);
}
for (i = 0; i < (pcount-1); ++i) {
printf("%d %d\n", i + 1, *(sums + i));
}
free(sieve);
free(primes);
free(sums);
return 0;
}