Strong and weak primes: Difference between revisions
Content added Content deleted
No edit summary |
Catskill549 (talk | contribs) (added AWK) |
||
Line 189: | Line 189: | ||
weak primes below 1,000,000: 37,780 |
weak primes below 1,000,000: 37,780 |
||
weak primes below 10,000,000: 321,750 |
weak primes below 10,000,000: 321,750 |
||
</pre> |
|||
=={{header|AWK}}== |
|||
<lang AWK> |
|||
# syntax: GAWK -f STRONG_AND_WEAK_PRIMES.AWK |
|||
BEGIN { |
|||
for (i=1; i<1E7; i++) { |
|||
if (is_prime(i)) { |
|||
arr[++n] = i |
|||
} |
|||
} |
|||
# strong: |
|||
stop1 = 36 ; stop2 = 1E6 ; stop3 = 1E7 |
|||
count1 = count2 = count3 = 0 |
|||
printf("The first %d strong primes:",stop1) |
|||
for (i=2; count1<stop1; i++) { |
|||
if (arr[i] > (arr[i-1] + arr[i+1]) / 2) { |
|||
count1++ |
|||
printf(" %d",arr[i]) |
|||
} |
|||
} |
|||
printf("\n") |
|||
for (i=2; i<stop3; i++) { |
|||
if (arr[i] > (arr[i-1] + arr[i+1]) / 2) { |
|||
count3++ |
|||
if (arr[i] < stop2) { |
|||
count2++ |
|||
} |
|||
} |
|||
} |
|||
printf("Number below %d: %d\n",stop2,count2) |
|||
printf("Number below %d: %d\n",stop3,count3) |
|||
# weak: |
|||
stop1 = 37 ; stop2 = 1E6 ; stop3 = 1E7 |
|||
count1 = count2 = count3 = 0 |
|||
printf("The first %d weak primes:",stop1) |
|||
for (i=2; count1<stop1; i++) { |
|||
if (arr[i] < (arr[i-1] + arr[i+1]) / 2) { |
|||
count1++ |
|||
printf(" %d",arr[i]) |
|||
} |
|||
} |
|||
printf("\n") |
|||
for (i=2; i<stop3; i++) { |
|||
if (arr[i] < (arr[i-1] + arr[i+1]) / 2) { |
|||
count3++ |
|||
if (arr[i] < stop2) { |
|||
count2++ |
|||
} |
|||
} |
|||
} |
|||
printf("Number below %d: %d\n",stop2,count2) |
|||
printf("Number below %d: %d\n",stop3,count3) |
|||
exit(0) |
|||
} |
|||
function is_prime(n, d) { |
|||
d = 5 |
|||
if (n < 2) { return(0) } |
|||
if (n % 2 == 0) { return(n == 2) } |
|||
if (n % 3 == 0) { return(n == 3) } |
|||
while (d*d <= n) { |
|||
if (n % d == 0) { return(0) } |
|||
d += 2 |
|||
if (n % d == 0) { return(0) } |
|||
d += 4 |
|||
} |
|||
return(1) |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
The first 36 strong primes: 11 17 29 37 41 59 67 71 79 97 101 107 127 137 149 163 179 191 197 223 227 239 251 269 277 281 307 311 331 347 367 379 397 419 431 439 |
|||
Number below 1000000: 37723 |
|||
Number below 10000000: 320992 |
|||
The first 37 weak primes: 3 7 13 19 23 31 43 47 61 73 83 89 103 109 113 131 139 151 167 181 193 199 229 233 241 271 283 293 313 317 337 349 353 359 383 389 401 |
|||
Number below 1000000: 37781 |
|||
Number below 10000000: 321750 |
|||
</pre> |
</pre> |
||