Sum of primes in odd positions is prime: Difference between revisions

added AWK
m (added to Prime Numbers category.)
(added AWK)
Line 47:
119 653 17959
143 823 26879
</pre>
=={{header|AWK}}==
<lang AWK>
# syntax: GAWK -f SUM_OF_PRIMES_IN_ODD_POSITIONS_IS_PRIME.AWK
# converted from Ring
BEGIN {
print(" i p sum")
print("------ ------ ------")
start = 2
stop = 999
for (i=start; i<=stop; i++) {
if (is_prime(i)) {
if (++nr % 2 == 1) {
sum += i
if (is_prime(sum)) {
count++
printf("%6d %6d %6d\n",nr,i,sum)
}
}
}
}
printf("Odd indexed primes %d-%d: %d\n",start,stop,count)
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>
i p sum
------ ------ ------
1 2 2
3 5 7
11 31 89
27 103 659
35 149 1181
67 331 5021
91 467 9923
95 499 10909
99 523 11941
119 653 17959
143 823 26879
Odd indexed primes 2-999: 11
</pre>
 
477

edits