Sum of two adjacent numbers are primes: Difference between revisions
Sum of two adjacent numbers are primes (view source)
Revision as of 13:02, 6 February 2022
, 2 years ago→{{header|C}}: Recoded to do extra credit.
(→{{header|Wren}}: Recoded to do extra credit.) |
(→{{header|C}}: Recoded to do extra credit.) |
||
Line 331:
=={{header|C}}==
{{trans|
<lang c>#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
typedef int bool;
void primeSieve(int *c, int limit, bool processEven, bool primesOnly) {
if (!(n%d)) return FALSE;▼
if (processEven) {
d += 2;▼
while (TRUE) {
p2 = p * p;
if (p2 >= limit) break;
for (i = p2; i < limit; i += 2*p) c[i] = TRUE;
while (TRUE) {
}
if (primesOnly) {
/* move the actual primes to the front of the array */
c[0] = 2;
for (i = 3, ix = 1; i < limit; i += 2) {
if (!c[i]) c[ix++] = i;
}
}
▲ return TRUE;
}
int main() {
int
int limit = (int)(log(n) * (double)n * 1.2); /* should be more than enough */
int *primes = (int *)calloc(limit, sizeof(int));
primeSieve(primes, limit-1, FALSE, TRUE);
printf("The first 20 pairs of natural numbers whose sum is prime are:\n");
printf("%2d + %2d =
▲ }
▲ n++;
}
printf("\nThe 10 millionth such pair is:\n");
p = primes[n];
hp = p / 2;
printf("%2d + %2d = %2d\n", hp, hp+1, p);
free(primes);
return 0;
}</lang>
Line 387 ⟶ 410:
35 + 36 = 71
36 + 37 = 73
The 10 millionth such pair is:
89712345 + 89712346 = 179424691
</pre>
|