Safe primes and unsafe primes: Difference between revisions

added AWK
(→‎{{header|jq}}: streamline)
(added AWK)
Line 250:
2, 3, 13, 17, 19, 29, 31, 37, 41, 43, 53, 61, 67, 71, 73, 79, 89, 97, 101, 103, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 173, 181, 191, 193, 197, 199, 211, 223, 229, 233
There are 74174 unsafe primes < 1,000,000 and 633922 < 10,000,000."</lang>
=={{header|AWK}}==
<lang AWK>
# syntax: GAWK -f SAFE_PRIMES_AND_UNSAFE_PRIMES.AWK
BEGIN {
for (i=1; i<1E7; i++) {
if (is_prime(i)) {
arr[i] = ""
}
}
# safe:
stop1 = 35 ; stop2 = 1E6 ; stop3 = 1E7
count1 = count2 = count3 = 0
printf("The first %d safe primes:",stop1)
for (i=3; count1<stop1; i+=2) {
if (i in arr && ((i-1)/2 in arr)) {
count1++
printf(" %d",i)
}
}
printf("\n")
for (i=3; i<stop3; i+=2) {
if (i in arr && ((i-1)/2 in arr)) {
count3++
if (i < stop2) {
count2++
}
}
}
printf("Number below %d: %d\n",stop2,count2)
printf("Number below %d: %d\n",stop3,count3)
# unsafe:
stop1 = 40 ; stop2 = 1E6 ; stop3 = 1E7
count1 = count2 = count3 = 1 # since (2-1)/2 is not prime
printf("The first %d unsafe primes: 2",stop1)
for (i=3; count1<stop1; i+=2) {
if (i in arr && !((i-1)/2 in arr)) {
count1++
printf(" %d",i)
}
}
printf("\n")
for (i=3; i<stop3; i+=2) {
if (i in arr && !((i-1)/2 in arr)) {
count3++
if (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 35 safe primes: 5 7 11 23 47 59 83 107 167 179 227 263 347 359 383 467 479 503 563 587 719 839 863 887 983 1019 1187 1283 1307 1319 1367 1439 1487 1523 1619
Number below 1000000: 4324
Number below 10000000: 30657
The first 40 unsafe primes: 2 3 13 17 19 29 31 37 41 43 53 61 67 71 73 79 89 97 101 103 109 113 127 131 137 139 149 151 157 163 173 181 191 193 197 199 211 223 229 233
Number below 1000000: 74174
Number below 10000000: 633922
</pre>
 
=={{header|C}}==
477

edits