Jump to content

Cousin primes: Difference between revisions

Add C
(Add J)
(Add C)
Line 159:
967 971
There are 41 cousin prime pairs below 1000</pre>
 
=={{header|C}}==
<lang c>#include <stdio.h>
#include <string.h>
 
#define LIMIT 1000
 
void sieve(int max, char *s) {
int p, k;
memset(s, 0, max);
for (p=2; p*p<=max; p++)
if (!s[p])
for (k=p*p; k<=max; k+=p)
s[k]=1;
}
 
int main(void) {
char primes[LIMIT+1];
int p, count=0;
sieve(LIMIT, primes);
for (p=2; p<=LIMIT; p++) {
if (!primes[p] && !primes[p+4]) {
count++;
printf("%4d: %4d\n", p, p+4);
}
}
printf("There are %d cousin prime pairs below %d.\n", count, LIMIT);
return 0;
}</lang>
 
{{out}}
 
<pre style="height:14em;"> 3: 7
7: 11
13: 17
19: 23
37: 41
43: 47
67: 71
79: 83
97: 101
103: 107
109: 113
127: 131
163: 167
193: 197
223: 227
229: 233
277: 281
307: 311
313: 317
349: 353
379: 383
397: 401
439: 443
457: 461
463: 467
487: 491
499: 503
613: 617
643: 647
673: 677
739: 743
757: 761
769: 773
823: 827
853: 857
859: 863
877: 881
883: 887
907: 911
937: 941
967: 971
There are 41 cousin prime pairs below 1000.</pre>
 
=={{header|Cowgol}}==
2,114

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.