Wolstenholme numbers: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Changed to Wren S/H) |
|||
Line 601: | Line 601: | ||
15: 40306783143871607599 ... 58901192511859288941 (digits: 1539) |
15: 40306783143871607599 ... 58901192511859288941 (digits: 1539) |
||
</pre> |
</pre> |
||
=={{header|jq}}== |
|||
'''Works with gojq, the Go implementation of jq''' |
|||
gojq supports infinite-precision integer arithmetic and has no problem |
|||
computing the first 10,000 Wolstenholme numbers, but the algorithm |
|||
used here for testing primality is only suitable for identifying |
|||
the first four prime numbers in the sequence. |
|||
See [[Arithmetic/Rational#jq]] for a library of functions that supports |
|||
rational arithmetic and that is suitable for inclusion using jq's `include` |
|||
directive. |
|||
<syntaxhighlight lang="jq"> |
|||
include "rational" {search: "."}; # see comment above |
|||
# Take advantage of gojq's support for infinite-precision integer arithmetic: |
|||
# If $j is 0, then an error condition is raised; |
|||
# otherwise, assuming infinite-precision integer arithmetic, |
|||
# if the input and $j are integers, then the result will be an integer. |
|||
def idivide($j): |
|||
(. % $j) as $mod |
|||
| (. - $mod) / $j ; |
|||
# To take advantage of gojq's arbitrary-precision integer arithmetic: |
|||
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in); |
|||
def properfactors: |
|||
. as $in |
|||
| [2, $in, false] |
|||
| recurse( |
|||
. as [$p, $q, $valid, $s] |
|||
| if $q == 1 then empty |
|||
elif $q % $p == 0 then [$p, ($q|idivide($p)), true] |
|||
elif $p == 2 then [3, $q, false, $s] |
|||
else ($s // ($q | sqrt)) as $s |
|||
| if ($p + 2) <= $s then [$p + 2, $q, false, $s] |
|||
else [$q, 1, true] |
|||
end |
|||
end ) |
|||
| if .[2] and .[0] != $in then .[0] else empty end ; |
|||
def is_prime: |
|||
[limit(1; properfactors)] | length == 0; |
|||
# Use $list to specify which Wolstenholme numbers are to be displayed; |
|||
# use $primes to specify the number of prime Wolstenholme numbers to identify. |
|||
def wolstenholme($max; $list; $primes): |
|||
{primes: [], w: [], h: 0} |
|||
| foreach range (1; 1+$max) as $k (.; |
|||
.emit = null |
|||
| .h = radd(.h; r(1; $k * $k)) |
|||
| .w += [.h] |
|||
| (.h | .n) as $n |
|||
| if (.primes|length) < $primes and ($n|is_prime) then .primes += [$n] end |
|||
| if $k <= 20 |
|||
then .emit = [$k, $n] |
|||
elif ($k | IN($list[])) |
|||
then .emit ="\($k): \($n|tostring[0:20]) (digits: \($n|tostring|length))" |
|||
else . |
|||
end; |
|||
select(.emit).emit, |
|||
(if $k == $max then {primes} else empty end) ); |
|||
"Wolstenholme numbers:", wolstenholme(10000; [500, 1000, 2500, 5000, 10000]; 4) |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
Wolstenholme numbers: |
|||
[1,1] |
|||
[2,5] |
|||
[3,49] |
|||
[4,205] |
|||
[5,5269] |
|||
[6,5369] |
|||
[7,266681] |
|||
[8,1077749] |
|||
[9,9778141] |
|||
[10,1968329] |
|||
[11,239437889] |
|||
[12,240505109] |
|||
[13,40799043101] |
|||
[14,40931552621] |
|||
[15,205234915681] |
|||
[16,822968714749] |
|||
[17,238357395880861] |
|||
[18,238820721143261] |
|||
[19,86364397717734821] |
|||
[20,17299975731542641] |
|||
500: 40989667509417020364 (digits: 434) |
|||
1000: 83545938483149689478 (digits: 866) |
|||
2500: 64537911900230612090 (digits: 2164) |
|||
5000: 34472086597488537716 (digits: 4340) |
|||
10000: 54714423173933343999 (digits: 8693) |
|||
{"primes":[1,5,266681,40799043101]} |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |