Primes whose sum of digits is 25: Difference between revisions
Content added Content deleted
(→{{header|JavaScript}}: Added a JS version.) |
Catskill549 (talk | contribs) |
||
Line 332: | Line 332: | ||
{{Out}} |
{{Out}} |
||
<pre>[997,1699,1789,1879,1987,2689,2797,2887,3499,3697,3769,3877,3967,4597,4759,4957,4993]</pre> |
<pre>[997,1699,1789,1879,1987,2689,2797,2887,3499,3697,3769,3877,3967,4597,4759,4957,4993]</pre> |
||
=={{header|AWK}}== |
|||
<lang AWK> |
|||
# syntax: GAWK -f PRIMES_WHICH_SUM_OF_DIGITS_IS_25.AWK |
|||
BEGIN { |
|||
start = 1 |
|||
stop = 5000 |
|||
for (i=start; i<=stop; i++) { |
|||
if (is_prime(i)) { |
|||
sum = 0 |
|||
for (j=1; j<=length(i); j++) { |
|||
sum += substr(i,j,1) |
|||
} |
|||
if (sum == 25) { |
|||
printf("%d ",i) |
|||
count++ |
|||
} |
|||
} |
|||
} |
|||
printf("\nPrime numbers %d-%d whose digits sum to 25: %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> |
|||
997 1699 1789 1879 1987 2689 2797 2887 3499 3697 3769 3877 3967 4597 4759 4957 4993 |
|||
Prime numbers 1-5000 whose digits sum to 25: 17 |
|||
</pre> |
|||
=={{header|C++}}== |
=={{header|C++}}== |