Primes whose first and last number is 3: Difference between revisions

added AWK
m (→‎{{header|Sidef}}: optimization)
(added AWK)
Line 79:
3343 3373 3413 3433 3463 3533 3583 3593 3613 3623 3643 3673
3733 3793 3803 3823 3833 3853 3863 3923 3943
</pre>
=={{header|AWK}}==
<lang AWK>
# syntax: GAWK -f PRIMES_WHOSE_FIRST_AND_LAST_NUMBER_IS_3.AWK
BEGIN {
start = 1
stop = 3999
for (i=start; i<=stop; i++) {
if (is_prime(i) && i ~ /^3/ && i ~ /3$/) {
printf("%5d%1s",i,++count1%10?"":"\n")
}
}
printf("\nPrimes beginning and ending with '3' %d-%d: %d\n",start,stop,count1)
start = 1
stop = 999999
for (i=start; i<=stop; i++) {
if (is_prime(i) && i ~ /^3/ && i ~ /3$/) {
count2++
}
}
printf("Primes beginning and ending with '3' %d-%d: %d\n",start,stop,count2)
exit(0)
}
function is_prime(x, i) {
if (x <= 1) {
return(0)
}
for (i=2; i<=int(sqrt(x)); i++) {
if (x % i == 0) {
return(0)
}
}
return(1)
}
</lang>
{{out}}
<pre>
3 313 353 373 383 3023 3083 3163 3203 3253
3313 3323 3343 3373 3413 3433 3463 3533 3583 3593
3613 3623 3643 3673 3733 3793 3803 3823 3833 3853
3863 3923 3943
Primes beginning and ending with '3' 1-3999: 33
Primes beginning and ending with '3' 1-999999: 2251
</pre>
 
477

edits