Iccanobif primes: Difference between revisions

m (order)
Line 70:
2 3 5 31 43 773 7951 64901 52057 393121
</pre>
 
=={{header|jq}}==
'''Works with jq and gojq, the C and Go implementations of jq'''
 
The following program will also work using jaq, the Rust
implementation of jq, provided the
adjustments described in the Addendum are made.
 
gojq supports infinite-precision integer arithmetic, but the `sqrt` algorithm
presented here is insufficient for computing the 12th Iccanobif prime
in a reasonable time.
<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
($n | sqrt) as $rt
| 23
| until( . > $rt or ($n % . == 0); .+2)
| . > $rt
end;
 
# Output: an indefinitely long stream of fibonacci numbers subject to
# integer arithmetic limitations if any
def fib: [0,1]|while(1;[last,add])[1];
 
def reverseNumber: tostring | explode | reverse | implode | tonumber;
 
"First 11 Iccanobif primes:",
limit(11; fib | tostring | reverseNumber | select(is_prime))
</syntaxhighlight>
{{output}}
<pre>
First 11 Iccanobif primes:
2
3
5
31
43
773
7951
64901
52057
393121
56577108676171
</pre>
 
===Addendum: jaq version===
jaq does not have indefinite-precision integer arithmetic, so
here we'll just briefly summarize the tweaks needed:
 
(1) Use `isqrt` as defined at [[Isqrt_(integer_square_root)_of_X#jq]]
but with the addition of `floor` at the end of the def of `idivide`.
 
(2) Replace reverseNumber so that leading 0s do not appear in the
reversed string:
<syntaxhighlight lang=jq>
# Input: an array of codepoints
# 48 is the codepoint of "0"
def rmLeadingZeros:
if .[0] == 48 then .[1:] | rmLeadingZeros else . end;
def reverseNumber: tostring | explode | reverse | rmLeadingZeros | implode | tonumber;
</syntaxhighlight>
 
=={{header|Julia}}==
2,503

edits