Deceptive numbers: Difference between revisions
Content added Content deleted
(→Python: simplify as far as it improves performance) |
|||
Line 308: | Line 308: | ||
<syntaxhighlight lang="j"> deceptives 21 |
<syntaxhighlight lang="j"> deceptives 21 |
||
91 259 451 481 703 1729 2821 2981 3367 4141 4187 5461 6533 6541 6601 7471 7777 8149 8401 8911 10001</syntaxhighlight> |
91 259 451 481 703 1729 2821 2981 3367 4141 4187 5461 6533 6541 6601 7471 7777 8149 8401 8911 10001</syntaxhighlight> |
||
=={{header|jq}}== |
|||
''Adapted from [[#Wren|Wren]]'' |
|||
'''Works with gojq and fq, the Go implementations of jq''' |
|||
The following program assumes integer arithmetic is sufficiently |
|||
accurate. Both gojq and fq meet this requirement as they |
|||
support unbounded precision integer arithmetic. |
|||
Execution time using gojq on a 3GHz machine: 0.26s |
|||
<syntaxhighlight 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 23 |
|||
| until( (. * .) > $n or ($n % . == 0); .+2) |
|||
| . * . > $n |
|||
end; |
|||
# Output: a stream |
|||
def deceptives: |
|||
{n:17, repunit: 1111111111111111} |
|||
| while(true; |
|||
.emit = null |
|||
| if (.n | is_prime | not) and (.n % 3 != 0) and (.n % 5 != 0) |
|||
then if (.repunit % .n == 0) |
|||
then .emit = .n |
|||
else . |
|||
end |
|||
else . |
|||
end |
|||
| .n += 2 |
|||
| .repunit |= . * 100 + 11) |
|||
| select(.emit).emit; |
|||
"The first 25 deceptive numbers are:", [limit(25;deceptives)] |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
[91,259,451,481,703,1729,2821,2981,3367,4141,4187,5461,6533,6541,6601,7471,7777,8149,8401,8911,10001,11111,12403,13981,14701] |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |