Find adjacent primes which differ by a square integer: Difference between revisions
Content added Content deleted
m (Peak moved page Find adjacents primes which difference is square integer to Find adjacent primes which differ by a square integer: English grammar) |
|||
Line 361: | Line 361: | ||
240 IF Q = 1 THEN P2 = P: P = T: RETURN |
240 IF Q = 1 THEN P2 = P: P = T: RETURN |
||
250 GOTO 220</lang> |
250 GOTO 220</lang> |
||
=={{header|jq}}== |
|||
{{works with|jq}} |
|||
'''Works with gojq, the Go implementation of jq''' |
|||
See [[Erdős-primes#jq]] for a suitable definition of `is_prime` as used here. |
|||
'''Preliminaries''' |
|||
<lang jq>def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .; |
|||
# Primes less than . // infinite |
|||
def primes: |
|||
(. // infinite) as $n |
|||
| if $n < 3 then empty |
|||
else 2, (range(3; $n) | select(is_prime)) |
|||
end;</lang> |
|||
'''The task''' |
|||
<lang jq># Input is given to primes/0 to determine the maximum prime to consider |
|||
# Output: [ $prime, $nextPrime, $nextPrime - $prime |
|||
def adjacentPrimesWhichDifferBySquare: |
|||
def isSquare: sqrt | . == floor; |
|||
foreach primes as $p ( {previous: null}; |
|||
.emit = null |
|||
| if .previous != null |
|||
and (($p - .previous) | isSquare) |
|||
then .emit = [.previous, $p] |
|||
else . |
|||
end |
|||
| .previous = $p; |
|||
select(.emit).emit); |
|||
# Input is given to primes/0 to determine the maximum prime to consider. |
|||
# Gap must be greater than $gap |
|||
def task($gap): |
|||
def l: lpad(6); |
|||
"Adjacent primes under \(.) whose difference is a square > \($gap):", |
|||
(adjacentPrimesWhichDifferBySquare |
|||
| (.[1] - .[0]) as $diff |
|||
| select($diff > 36) |
|||
| "\(.[1]|l) - \(.[0]|l) = \($diff|lpad(4))" ) ; |
|||
1E6 | task(37)</lang> |
|||
{{out}} |
|||
As for [[#Algol-68]]. |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |