Smith numbers: Difference between revisions
Content added Content deleted
Line 1,806: | Line 1,806: | ||
Last 12 Smith Numbers below 10000: |
Last 12 Smith Numbers below 10000: |
||
9778 9840 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985</pre> |
9778 9840 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985</pre> |
||
=={{header|jq}}== |
|||
{{works with|jq}} |
|||
'''Works with gojq, the Go implementation of jq''' |
|||
''' Preliminaries''' |
|||
<lang jq>def is_prime: |
|||
. as $n |
|||
| if ($n < 2) then false |
|||
elif ($n % 2 == 0) then $n == 2 |
|||
elif ($n % 3 == 0) then $n == 3 |
|||
elif ($n % 5 == 0) then $n == 5 |
|||
elif ($n % 7 == 0) then $n == 7 |
|||
elif ($n % 11 == 0) then $n == 11 |
|||
elif ($n % 13 == 0) then $n == 13 |
|||
elif ($n % 17 == 0) then $n == 17 |
|||
elif ($n % 19 == 0) then $n == 19 |
|||
else {i:23} |
|||
| until( (.i * .i) > $n or ($n % .i == 0); .i += 2) |
|||
| .i * .i > $n |
|||
end; |
|||
def sum(s): reduce s as $x (null; . + $x); |
|||
# emit a stream of the prime factors as per prime factorization |
|||
def prime_factors: |
|||
. as $num |
|||
| def m($p): # emit $p with appropriate multiplicity |
|||
$num | while( . % $p == 0; . / $p ) |
|||
| $p ; |
|||
if (. % 2) == 0 then m(2) else empty end, |
|||
(range(3; 1 + (./2); 2) |
|||
| select(($num % .) == 0 and is_prime) |
|||
| m(.)); |
|||
</lang> |
|||
'''The task''' |
|||
<lang jq># input should be an integer |
|||
def is_smith: |
|||
def sumdigits: |
|||
tostring|explode|map([.]|implode|tonumber)| add; |
|||
(is_prime|not) and |
|||
(sumdigits == sum(prime_factors|sumdigits)); |
|||
"Smith numbers up to 10000:\n", |
|||
(range(1; 10000) | select(is_smith)) |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
Smith numbers up to 10000: |
|||
4 |
|||
22 |
|||
27 |
|||
58 |
|||
... |
|||
9942 |
|||
9968 |
|||
9975 |
|||
9985 |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |