Find adjacent primes which differ by a square integer: Difference between revisions

Added Easylang
(julia example)
(Added Easylang)
 
(33 intermediate revisions by 19 users not shown)
Line 4:
<br>Find adjacent primes under '''1,000,000''' whose difference '''(> 36)''' is a square integer.
<br><br>
 
=={{header|11l}}==
<syntaxhighlight lang="11l">F primes_upto(limit)
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
I is_prime[n]
L(i) (n * n .. limit).step(n)
is_prime[i] = 0B
R enumerate(is_prime).filter((i, prime) -> prime).map((i, prime) -> i)
 
V primes = primes_upto(1'000'000)
 
F is_square(x)
R Int(sqrt(x)) ^ 2 == x
 
L(n) 2 .< primes.len
V pr1 = primes[n]
V pr2 = primes[n - 1]
V diff = pr1 - pr2
I (is_square(diff) & diff > 36)
print(pr1‘ ’pr2‘ diff = ’diff)</syntaxhighlight>
 
{{out}}
<pre>
89753 89689 diff = 64
107441 107377 diff = 64
288647 288583 diff = 64
368021 367957 diff = 64
381167 381103 diff = 64
396833 396733 diff = 100
400823 400759 diff = 64
445427 445363 diff = 64
623171 623107 diff = 64
625763 625699 diff = 64
637067 637003 diff = 64
710777 710713 diff = 64
725273 725209 diff = 64
779477 779413 diff = 64
801947 801883 diff = 64
803813 803749 diff = 64
821741 821677 diff = 64
832583 832519 diff = 64
838349 838249 diff = 100
844841 844777 diff = 64
883871 883807 diff = 64
912167 912103 diff = 64
919511 919447 diff = 64
954827 954763 diff = 64
981887 981823 diff = 64
997877 997813 diff = 64
</pre>
 
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68">BEGIN # find a adjacent primes where the primes differ by a square > 36 #
INT min diff = 37;
INT max prime = 1 000 000;
Line 30 ⟶ 81:
FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 59 ⟶ 110:
981887 - 981823 = 64
997877 - 997813 = 64
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">squares: map 7..15 'x -> x*x
primes: select 1..1000000 => prime?
 
loop.with:'i primes\[0..(size primes)-2] 'p [
next: primes\[i+1]
if contains? squares next-p ->
print [pad to :string next 6 "-" pad to :string p 6 "=" next-p]
]</syntaxhighlight>
 
{{out}}
 
<pre> 89753 - 89689 = 64
107441 - 107377 = 64
288647 - 288583 = 64
368021 - 367957 = 64
381167 - 381103 = 64
396833 - 396733 = 100
400823 - 400759 = 64
445427 - 445363 = 64
623171 - 623107 = 64
625763 - 625699 = 64
637067 - 637003 = 64
710777 - 710713 = 64
725273 - 725209 = 64
779477 - 779413 = 64
801947 - 801883 = 64
803813 - 803749 = 64
821741 - 821677 = 64
832583 - 832519 = 64
838349 - 838249 = 100
844841 - 844777 = 64
883871 - 883807 = 64
912167 - 912103 = 64
919511 - 919447 = 64
954827 - 954763 = 64
981887 - 981823 = 64
997877 - 997813 = 64</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f FIND_ADJACENTS_PRIMES_WHICH_DIFFERENCE_IS_SQUARE_INTEGER.AWK
# converted from FreeBASIC
BEGIN {
start = i = 3
stop = 999999
while (j <= stop) {
j = next_prime(i)
if (j-i > 36 && is_square(j-i)) {
printf("%9d %9d %9d\n",i,j,j-i)
count++
}
i = j
}
printf("Adjacent primes which difference is square integer (>36) %d-%d: %d\n",start,stop,count)
exit(0)
}
function is_prime(n, d) {
d = 5
if (n < 2) { return(0) }
if (n % 2 == 0) { return(n == 2) }
if (n % 3 == 0) { return(n == 3) }
while (d*d <= n) {
if (n % d == 0) { return(0) }
d += 2
if (n % d == 0) { return(0) }
d += 4
}
return(1)
}
function is_square(n) {
return (int(sqrt(n))^2 == n)
}
function next_prime(n, q) { # finds next prime after n
if (n == 0) { return(2) }
if (n < 3) { return(++n) }
q = n + 2
while (!is_prime(q)) {
q += 2
}
return(q)
}
</syntaxhighlight>
{{out}}
<pre>
89689 89753 64
107377 107441 64
288583 288647 64
367957 368021 64
381103 381167 64
396733 396833 100
400759 400823 64
445363 445427 64
623107 623171 64
625699 625763 64
637003 637067 64
710713 710777 64
725209 725273 64
779413 779477 64
801883 801947 64
803749 803813 64
821677 821741 64
832519 832583 64
838249 838349 100
844777 844841 64
883807 883871 64
912103 912167 64
919447 919511 64
954763 954827 64
981823 981887 64
997813 997877 64
Adjacent primes which difference is square integer (>36) 3-999999: 26
</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include<stdio.h>
#include<stdlib.h>
 
Line 96 ⟶ 262:
}
return 0;
}</langsyntaxhighlight>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% Integer square root
isqrt = proc (s: int) returns (int)
x0: int := s/2
if x0=0 then return(s) end
x1: int := (x0 + s/x0)/2
while x1 < x0 do
x0 := x1
x1 := (x0 + s/x0)/2
end
return(x0)
end isqrt
% See if a number is square
% Note that all squares are 0, 1, 4, or 9 mod 16.
is_square = proc (n: int) returns (bool)
d: int := n//16
if d=0 cor d=1 cor d=4 cor d=9 then
return(n = isqrt(n)**2)
else
return(false)
end
end is_square
 
% Find all primes up to a given number
sieve = proc (top: int) returns (array[int])
prime: array[bool] := array[bool]$fill(2,top-1,true)
for p: int in int$from_to(2,isqrt(top)) do
if prime[p] then
for c: int in int$from_to_by(p*p,top,p) do
prime[c] := false
end
end
end
list: array[int] := array[int]$predict(1,isqrt(top))
for p: int in int$from_to(2,top) do
if prime[p] then array[int]$addh(list,p) end
end
return(list)
end sieve
 
start_up = proc ()
MAX = 1000000
DIFF = 36
po: stream := stream$primary_output()
primes: array[int] := sieve(MAX)
for i: int in int$from_to(array[int]$low(primes)+1,
array[int]$high(primes)) do
d: int := primes[i] - primes[i-1]
if d>DIFF cand is_square(d) then
stream$putright(po, int$unparse(primes[i]), 6)
stream$puts(po, " - ")
stream$putright(po, int$unparse(primes[i-1]), 6)
stream$puts(po, " = ")
stream$putright(po, int$unparse(d), 4)
stream$puts(po, " = ")
stream$putright(po, int$unparse(isqrt(d)), 4)
stream$putl(po, "^2")
end
end
end start_up</syntaxhighlight>
{{out}}
<pre> 89753 - 89689 = 64 = 8^2
107441 - 107377 = 64 = 8^2
288647 - 288583 = 64 = 8^2
368021 - 367957 = 64 = 8^2
381167 - 381103 = 64 = 8^2
396833 - 396733 = 100 = 10^2
400823 - 400759 = 64 = 8^2
445427 - 445363 = 64 = 8^2
623171 - 623107 = 64 = 8^2
625763 - 625699 = 64 = 8^2
637067 - 637003 = 64 = 8^2
710777 - 710713 = 64 = 8^2
725273 - 725209 = 64 = 8^2
779477 - 779413 = 64 = 8^2
801947 - 801883 = 64 = 8^2
803813 - 803749 = 64 = 8^2
821741 - 821677 = 64 = 8^2
832583 - 832519 = 64 = 8^2
838349 - 838249 = 100 = 10^2
844841 - 844777 = 64 = 8^2
883871 - 883807 = 64 = 8^2
912167 - 912103 = 64 = 8^2
919511 - 919447 = 64 = 8^2
954827 - 954763 = 64 = 8^2
981887 - 981823 = 64 = 8^2
997877 - 997813 = 64 = 8^2</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
function IsPrime(N: integer): boolean;
{Optimised prime test - about 40% faster than the naive approach}
var I,Stop: integer;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (i + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
end;
 
function GetNextPrime(Start: integer): integer;
{Get the next prime number after Start}
begin
repeat Inc(Start)
until IsPrime(Start);
Result:=Start;
end;
 
 
 
procedure ShowPrimeDiffs(Memo: TMemo);
var P1,P2,D: integer;
begin
P1:=GetNextPrime(2);
repeat
begin
P2:=GetNextPrime(P1);
D:=P2 - P1;
if (D>36) and (Frac(sqrt(D))=0) then
begin
Memo.Lines.Add(IntToStr(P2)+' - '+IntToStr(P1)+' = '+IntToStr(D));
end;
P1:=P2;
end
until P2>=1000000;
end;
 
</syntaxhighlight>
{{out}}
<pre>
89753 - 89689 = 64
107441 - 107377 = 64
288647 - 288583 = 64
368021 - 367957 = 64
381167 - 381103 = 64
396833 - 396733 = 100
400823 - 400759 = 64
445427 - 445363 = 64
623171 - 623107 = 64
625763 - 625699 = 64
637067 - 637003 = 64
710777 - 710713 = 64
725273 - 725209 = 64
779477 - 779413 = 64
801947 - 801883 = 64
803813 - 803749 = 64
821741 - 821677 = 64
832583 - 832519 = 64
838349 - 838249 = 100
844841 - 844777 = 64
883871 - 883807 = 64
912167 - 912103 = 64
919511 - 919447 = 64
954827 - 954763 = 64
981887 - 981823 = 64
997877 - 997813 = 64
</pre>
 
 
=={{header|EasyLang}}==
{{trans|AWK}}
<syntaxhighlight>
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
prim = 2
proc nextprim . .
repeat
prim += 1
until isprim prim = 1
.
.
func is_square n .
h = floor sqrt n
return if h * h = n
.
while prim < 1000000
prev = prim
nextprim
if prim - prev > 36 and is_square (prim - prev) = 1
print prim & " - " & prev & " = " & prim - prev
.
.
</syntaxhighlight>
{{out}}
<pre>
89753 - 89689 = 64
107441 - 107377 = 64
288647 - 288583 = 64
368021 - 367957 = 64
381167 - 381103 = 64
396833 - 396733 = 100
400823 - 400759 = 64
445427 - 445363 = 64
623171 - 623107 = 64
625763 - 625699 = 64
637067 - 637003 = 64
710777 - 710713 = 64
725273 - 725209 = 64
779477 - 779413 = 64
801947 - 801883 = 64
803813 - 803749 = 64
821741 - 821677 = 64
832583 - 832519 = 64
838349 - 838249 = 100
844841 - 844777 = 64
883871 - 883807 = 64
912167 - 912103 = 64
919511 - 919447 = 64
954827 - 954763 = 64
981887 - 981823 = 64
997877 - 997813 = 64
</pre>
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<syntaxhighlight lang="fsharp">
// Find adjacents primes which difference is square integer . Nigel Galloway: November 23rd., 2021
primes32()|>Seq.takeWhile((>)1000000)|>Seq.pairwise|>Seq.filter(fun(n,g)->let n=g-n in let g=(float>>sqrt>>int)n in g>6 && n=g*g)|>Seq.iter(printfn "%A")
</syntaxhighlight>
{{out}}
<pre>
(89689, 89753)
(107377, 107441)
(288583, 288647)
(367957, 368021)
(381103, 381167)
(396733, 396833)
(400759, 400823)
(445363, 445427)
(623107, 623171)
(625699, 625763)
(637003, 637067)
(710713, 710777)
(725209, 725273)
(779413, 779477)
(801883, 801947)
(803749, 803813)
(821677, 821741)
(832519, 832583)
(838249, 838349)
(844777, 844841)
(883807, 883871)
(912103, 912167)
(919447, 919511)
(954763, 954827)
(981823, 981887)
(997813, 997877)
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<syntaxhighlight lang="factor">USING: formatting io kernel lists lists.lazy math math.functions
math.primes.lists sequences ;
 
: adj-primes ( -- list ) lprimes dup cdr lzip ;
 
: diff ( pair -- n ) first2 swap - ;
 
: adj-primes-diff ( -- list )
adj-primes [ dup diff suffix ] lmap-lazy ;
 
: big-adj-primes-diff ( -- list )
adj-primes-diff [ last 36 > ] lfilter ;
 
: square? ( n -- ? ) sqrt dup >integer number= ;
 
: big-sq-adj-primes-diff ( -- list )
big-adj-primes-diff [ last square? ] lfilter ;
 
"Adjacent primes under a million whose difference is a square > 36:" print nl
"p1 p2 difference" print
"============================" print
big-sq-adj-primes-diff [ second 1,000,000 < ] lwhile
[ "%-6d %-6d %d\n" vprintf ] leach</syntaxhighlight>
{{out}}
<pre>
Adjacent primes under a million whose difference is a square > 36:
 
p1 p2 difference
============================
89689 89753 64
107377 107441 64
288583 288647 64
367957 368021 64
381103 381167 64
396733 396833 100
400759 400823 64
445363 445427 64
623107 623171 64
625699 625763 64
637003 637067 64
710713 710777 64
725209 725273 64
779413 779477 64
801883 801947 64
803749 803813 64
821677 821741 64
832519 832583 64
838249 838349 100
844777 844841 64
883807 883871 64
912103 912167 64
919447 919511 64
954763 954827 64
981823 981887 64
997813 997877 64
</pre>
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">Func Issqr( n ) = if (Sqrt(n))^2=n then 1 else 0 fi.;
i:=3;
j:=3;
Line 111 ⟶ 609:
j:=j+2;
od;
od;</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
 
function nextprime( n as uinteger ) as uinteger
Line 136 ⟶ 634:
if j-i > 36 and issquare(j-i) then print i, j, j-i
i = j
wend</langsyntaxhighlight>
{{out}}<pre>
89689 89753 64
Line 164 ⟶ 662:
981823 981887 64
997813 997877 64
</pre>
 
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"math"
"rcu"
)
 
func main() {
limit := 999999
primes := rcu.Primes(limit)
fmt.Println("Adjacent primes under 1,000,000 whose difference is a square > 36:")
for i := 1; i < len(primes); i++ {
diff := primes[i] - primes[i-1]
if diff > 36 {
s := int(math.Sqrt(float64(diff)))
if diff == s*s {
cp1 := rcu.Commatize(primes[i])
cp2 := rcu.Commatize(primes[i-1])
fmt.Printf("%7s - %7s = %3d = %2d x %2d\n", cp1, cp2, diff, s, s)
}
}
}
}</syntaxhighlight>
 
{{out}}
<pre>
Same as Wren example.
</pre>
 
=={{header|GW-BASIC}}==
<langsyntaxhighlight lang="gwbasic">10 P=3 : P2=0
20 GOSUB 180
30 IF P2>1000000! THEN END
Line 191 ⟶ 722:
230 GOSUB 80
240 IF Q = 1 THEN P2 = P: P = T: RETURN
250 GOTO 220</langsyntaxhighlight>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">
import Data.List.Split ( divvy )
 
isSquare :: Int -> Bool
isSquare n = (snd $ properFraction $ sqrt $ fromIntegral n) == 0.0
 
isPrime :: Int -> Bool
isPrime n
|n == 2 = True
|n == 1 = False
|otherwise = null $ filter (\i -> mod n i == 0 ) [2 .. root]
where
root :: Int
root = floor $ sqrt $ fromIntegral n
 
solution :: [[Int]]
solution = filter (\li -> isSquare (last li - head li ) &&
( last li - head li ) > 36 ) $ divvy 2 1 $ filter isPrime [2..1000000]
 
printResultLine :: [Int] -> String
printResultLine list = show ( last list ) ++ " - " ++ ( show $ head list )
++ " = " ++ ( show ( last list - head list ))
 
main :: IO ( )
main = do
let resultPairs = solution
mapM_ (\li -> putStrLn $ printResultLine li ) resultPairs</syntaxhighlight>
 
{{out}}
<pre>
89753 - 89689 = 64
107441 - 107377 = 64
288647 - 288583 = 64
368021 - 367957 = 64
381167 - 381103 = 64
396833 - 396733 = 100
400823 - 400759 = 64
445427 - 445363 = 64
623171 - 623107 = 64
625763 - 625699 = 64
637067 - 637003 = 64
710777 - 710713 = 64
725273 - 725209 = 64
779477 - 779413 = 64
801947 - 801883 = 64
803813 - 803749 = 64
821741 - 821677 = 64
832583 - 832519 = 64
838349 - 838249 = 100
844841 - 844777 = 64
883871 - 883807 = 64
912167 - 912103 = 64
919511 - 919447 = 64
954827 - 954763 = 64
981887 - 981823 = 64
997877 - 997813 = 64
</pre>
 
 
=={{header|J}}==
<syntaxhighlight lang="j"> #(,.-~/"1) p:0 1+/~I.(= <.)6.5>.%:2-~/\p:i.p:inv 1e6 NB. count them
26
(,.-~/"1) p:0 1+/~I.(= <.)6.5>.%:2-~/\p:i.p:inv 1e6 NB. show them
89689 89753 64
107377 107441 64
288583 288647 64
367957 368021 64
381103 381167 64
396733 396833 100
400759 400823 64
445363 445427 64
623107 623171 64
625699 625763 64
637003 637067 64
710713 710777 64
725209 725273 64
779413 779477 64
801883 801947 64
803749 803813 64
821677 821741 64
832519 832583 64
838249 838349 100
844777 844841 64
883807 883871 64
912103 912167 64
919447 919511 64
954763 954827 64
981823 981887 64
997813 997877 64</syntaxhighlight>
 
In other words: enumerate primes less than 1e6, find the pairwise differences, find where the prime pairs where maximum of their square root and 6.5 is an integer, and list those pairs with their differences.
 
=={{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'''
<syntaxhighlight 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;</syntaxhighlight>
'''The task'''
<syntaxhighlight lang="jq"># Input is given to primes/0 - to determine the maximum prime to consider
# Output: stream of [$prime, $nextPrime]
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 > $gap)
| "\(.[1]|l) - \(.[0]|l) = \($diff|lpad(4))" ) ;
 
1E6 | task(36)</syntaxhighlight>
{{out}}
As for [[#ALGOL_68]].
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function squareprimegaps(limit=10^11)
pri = primes(limit)
squares = Set([1; [x * x for x in 2:2:100]])
diffs = [pri[i] - pri[i - 1] for i in 2:length(pri)]
squarediffs = sort(unique(filter(n -> n in squares, diffs)))
println("Square\n\nSquare prime gaps to $limit:")
for sq in squarediffs
i = findfirst(x -> x == sq, diffs)
n = count(x -> x == sq, diffs)
if limit == 1000000 && sq > 36
println("Square difference $sq: $n found. Example: ($(pri[i]), $(pri[i + 1])).")
println("Showing all $n with square difference $sq:")
pairs = [(pri[i], pri[i + 1]) for i in findall(x -> x == sq, diffs)]
foreach(p -> print(last(p), first(p) % 4 == 0 ? "\n" : " "), enumerate(pairs))
else
println("Square difference $sq: $n found. Example: ($(pri[i]), $(pri[i + 1])).")
end
end
end
 
squareprimegaps(1_000_000)
squareprimegaps(10_000_000_000)
</lang>{{out}}
 
</syntaxhighlight>{{out}}
<pre>
 
Square prime gaps to 1000000:
Square difference 1: 1 found. Example: (2, 3).
Square difference 4: 8143 found. Example: (7, 11).
Square difference 16: 2881 found. Example: (1831, 1847).
Square difference 36: 767 found. Example: (9551, 9587).
Showing all 24 with square difference 64:
(89689, 89753) (107377, 107441) (288583, 288647) (367957, 368021)
(381103, 381167) (400759, 400823) (445363, 445427) (623107, 623171)
(625699, 625763) (637003, 637067) (710713, 710777) (725209, 725273)
(779413, 779477) (801883, 801947) (803749, 803813) (821677, 821741)
(832519, 832583) (844777, 844841) (883807, 883871) (912103, 912167)
(919447, 919511) (954763, 954827) (981823, 981887) (997813, 997877)
Showing all 2 with square difference 100:
(396733, 396833) (838249, 838349)
 
Square prime gaps to 10000000000:
Square difference 1: 1 found. Example: (2, 3).
Line 223 ⟶ 916:
Square difference 256: 41 found. Example: (1872851947, 1872852203).
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ps = Prime[Range[PrimePi[10^6]]];
ps = Partition[ps, 2, 1];
ps = {#1, #2, #2 - #1} & @@@ ps;
ps //= Select[Extract[{3}]/*GreaterThan[36]];
ps //= Select[Extract[{3}]/*Sqrt/*IntegerQ];
ps // Grid</syntaxhighlight>
{{out}}
<pre>89689 89753 64
107377 107441 64
288583 288647 64
367957 368021 64
381103 381167 64
396733 396833 100
400759 400823 64
445363 445427 64
623107 623171 64
625699 625763 64
637003 637067 64
710713 710777 64
725209 725273 64
779413 779477 64
801883 801947 64
803749 803813 64
821677 821741 64
832519 832583 64
838249 838349 100
844777 844841 64
883807 883871 64
912103 912167 64
919447 919511 64
954763 954827 64
981823 981887 64
997813 997877 64</pre>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">
for(i=3,1000000,j=nextprime(i+1);if(isprime(i)&&j-i>36&&issquare(j-i),print(i," ",j," ",j-i)))
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Find_adjacents_primes_which_difference_is_square_integer
use warnings;
use ntheory qw( primes is_square );
 
my $primeref = primes(1e6);
for my $i (1 .. $#$primeref) {
(my $diff = $primeref->[$i] - $primeref->[$i - 1]) > 36 or next;
is_square($diff) and print "$primeref->[$i] - $primeref->[$i - 1] = $diff\n";
}</syntaxhighlight>
{{out}}
<pre>
89753 - 89689 = 64
107441 - 107377 = 64
288647 - 288583 = 64
368021 - 367957 = 64
381167 - 381103 = 64
396833 - 396733 = 100
400823 - 400759 = 64
445427 - 445363 = 64
623171 - 623107 = 64
625763 - 625699 = 64
637067 - 637003 = 64
710777 - 710713 = 64
725273 - 725209 = 64
779477 - 779413 = 64
801947 - 801883 = 64
803813 - 803749 = 64
821741 - 821677 = 64
832583 - 832519 = 64
838349 - 838249 = 100
844841 - 844777 = 64
883871 - 883807 = 64
912167 - 912103 = 64
919511 - 919447 = 64
954827 - 954763 = 64
981887 - 981823 = 64
997877 - 997813 = 64
</pre>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1_000_000</span>
Line 248 ⟶ 1,018:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 280 ⟶ 1,050:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
import math
print("working...")
Line 314 ⟶ 1,084:
 
print("done...")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 345 ⟶ 1,115:
997877 997813 diff = 64
done...
</pre>
 
=={{header|Quackery}}==
 
<code>eratosthenes</code>, <code>isprime</code>, and <code>sqrt</code> are defined at [[Sieve of Eratosthenes#Quackery]].
 
<syntaxhighlight lang="Quackery"> 1000000 eratosthenes
 
0 0
1000000 times
[ i^ isprime if
[ nip i^ 2dup swap -
dup 36 > iff
[ dup sqrt dup * = if
[ 2dup swap
2dup - unrot
echo say " - "
echo say " = "
echo cr ] ]
else drop ] ]
2drop</syntaxhighlight>
 
{{out}}
 
<pre>89689 - 89753 = 64
107377 - 107441 = 64
288583 - 288647 = 64
367957 - 368021 = 64
381103 - 381167 = 64
396733 - 396833 = 100
400759 - 400823 = 64
445363 - 445427 = 64
623107 - 623171 = 64
625699 - 625763 = 64
637003 - 637067 = 64
710713 - 710777 = 64
725209 - 725273 = 64
779413 - 779477 = 64
801883 - 801947 = 64
803749 - 803813 = 64
821677 - 821741 = 64
832519 - 832583 = 64
838249 - 838349 = 100
844777 - 844841 = 64
883807 - 883871 = 64
912103 - 912167 = 64
919447 - 919511 = 64
954763 - 954827 = 64
981823 - 981887 = 64
997813 - 997877 = 64
</pre>
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
use Math::Primesieve;
 
Line 371 ⟶ 1,191:
say "\nGap {$p.key}: {comma @counts[$p.key]} found$ten:";
put join "\n", $p.value.batch(5)».map({"($_, {$_+ $p.key})"})».join(', ');
}</langsyntaxhighlight>
{{out}}
<pre>Adjacent primes up to 10,000,000,000 with a gap value that is a perfect square:
Line 410 ⟶ 1,230:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 444 ⟶ 1,264:
next
return 0
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 475 ⟶ 1,295:
997877 997813 diff = 64
done...
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require "prime"
 
Prime.each(1_000_000).each_cons(2) do |a, b|
diff = b - a
next unless diff > 36
isqrt = Integer.sqrt(diff)
puts "#{b} - #{a} = #{diff}" if isqrt*isqrt == diff
end
</syntaxhighlight>
{{out}}
<pre>89753 - 89689 = 64
107441 - 107377 = 64
288647 - 288583 = 64
368021 - 367957 = 64
381167 - 381103 = 64
396833 - 396733 = 100
400823 - 400759 = 64
445427 - 445363 = 64
623171 - 623107 = 64
625763 - 625699 = 64
637067 - 637003 = 64
710777 - 710713 = 64
725273 - 725209 = 64
779477 - 779413 = 64
801947 - 801883 = 64
803813 - 803749 = 64
821741 - 821677 = 64
832583 - 832519 = 64
838349 - 838249 = 100
844841 - 844777 = 64
883871 - 883807 = 64
912167 - 912103 = 64
919511 - 919447 = 64
954827 - 954763 = 64
981887 - 981823 = 64
997877 - 997813 = 64
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
use prime_tools ;
 
fn is_square_number( num : u32 ) -> bool {
let comp_num : f32 = num as f32 ;
let root = comp_num.sqrt( ) ;
return root == root.floor( ) ;
}
 
fn main() {
let primes: Vec<u32> = prime_tools::get_primes_less_than_x(1000000_u32) ;
let len = primes.len( ) ;
let mut i : usize = 0 ;
while i < len - 1 {
let diff : u32 = primes[ i + 1 ] - primes[ i ] ;
if diff > 36 && is_square_number( diff ) {
println!("{} - {} = {}" , primes[ i + 1 ] , primes[ i ] , diff) ;
}
i += 1 ;
}
}</syntaxhighlight>
{{out}}
<pre>
89753 - 89689 = 64
107441 - 107377 = 64
288647 - 288583 = 64
368021 - 367957 = 64
381167 - 381103 = 64
396833 - 396733 = 100
400823 - 400759 = 64
445427 - 445363 = 64
623171 - 623107 = 64
625763 - 625699 = 64
637067 - 637003 = 64
710777 - 710713 = 64
725273 - 725209 = 64
779477 - 779413 = 64
801947 - 801883 = 64
803813 - 803749 = 64
821741 - 821677 = 64
832583 - 832519 = 64
838349 - 838249 = 100
844841 - 844777 = 64
883871 - 883807 = 64
912167 - 912103 = 64
919511 - 919447 = 64
954827 - 954763 = 64
981887 - 981823 = 64
997877 - 997813 = 64
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var p = 2
var upto = 1e6
 
each_prime(p.next_prime, upto, {|q|
if (q-p > 36 && is_square(q-p)) {
say "#{'%6s' % q} - #{'%6s' % p} = #{'%2s' % isqrt(q-p)}^2"
}
p = q
})</syntaxhighlight>
{{out}}
<pre>
89753 - 89689 = 8^2
107441 - 107377 = 8^2
288647 - 288583 = 8^2
368021 - 367957 = 8^2
381167 - 381103 = 8^2
396833 - 396733 = 10^2
400823 - 400759 = 8^2
445427 - 445363 = 8^2
623171 - 623107 = 8^2
625763 - 625699 = 8^2
637067 - 637003 = 8^2
710777 - 710713 = 8^2
725273 - 725209 = 8^2
779477 - 779413 = 8^2
801947 - 801883 = 8^2
803813 - 803749 = 8^2
821741 - 821677 = 8^2
832583 - 832519 = 8^2
838349 - 838249 = 10^2
844841 - 844777 = 8^2
883871 - 883807 = 8^2
912167 - 912103 = 8^2
919511 - 919447 = 8^2
954827 - 954763 = 8^2
981887 - 981823 = 8^2
997877 - 997813 = 8^2
</pre>
 
Line 480 ⟶ 1,431:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./fmt" for Fmt
 
Line 494 ⟶ 1,445:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 525 ⟶ 1,476:
981,887 - 981,823 = 64 = 8 x 8
997,877 - 997,813 = 64 = 8 x 8
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if odd N > 2 is prime
int N, I;
[for I:= 3 to sqrt(N) do
[if rem(N/I) = 0 then return false;
I:= I+1;
];
return true;
];
 
int N, P0, P1, D, RD;
[P0:= 2;
for N:= 3 to 1_000_000-1 do
[if IsPrime(N) then
[P1:= N;
D:= P1 - P0; \D is even because odd - odd = even
if D >= 64 then \the next even square > 36 is 64
[RD:= sqrt(D);
if RD*RD = D then
[IntOut(0, P1); Text(0, " - ");
IntOut(0, P0); Text(0, " = ");
IntOut(0, D); CrLf(0);
];
];
P0:= P1;
];
N:= N+1; \step by 1+1 = 2 (for odd numbers)
];
]</syntaxhighlight>
 
{{out}}
<pre>
89753 - 89689 = 64
107441 - 107377 = 64
288647 - 288583 = 64
368021 - 367957 = 64
381167 - 381103 = 64
396833 - 396733 = 100
400823 - 400759 = 64
445427 - 445363 = 64
623171 - 623107 = 64
625763 - 625699 = 64
637067 - 637003 = 64
710777 - 710713 = 64
725273 - 725209 = 64
779477 - 779413 = 64
801947 - 801883 = 64
803813 - 803749 = 64
821741 - 821677 = 64
832583 - 832519 = 64
838349 - 838249 = 100
844841 - 844777 = 64
883871 - 883807 = 64
912167 - 912103 = 64
919511 - 919447 = 64
954827 - 954763 = 64
981887 - 981823 = 64
997877 - 997813 = 64
</pre>
1,973

edits