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

Added Easylang
(added AWK)
(Added Easylang)
 
(23 intermediate revisions by 13 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 60 ⟶ 111:
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">
<lang AWK>
# syntax: GAWK -f FIND_ADJACENTS_PRIMES_WHICH_DIFFERENCE_IS_SQUARE_INTEGER.AWK
# converted from FreeBASIC
Line 103 ⟶ 195:
return(q)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 136 ⟶ 228:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include<stdio.h>
#include<stdlib.h>
 
Line 170 ⟶ 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#)]
<langsyntaxhighlight 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>
</lang>
{{out}}
<pre>
Line 210 ⟶ 539:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: formatting io kernel lists lists.lazy math math.functions
math.primes.lists sequences ;
 
Line 232 ⟶ 561:
"============================" print
big-sq-adj-primes-diff [ second 1,000,000 < ] lwhile
[ "%-6d %-6d %d\n" vprintf ] leach</langsyntaxhighlight>
{{out}}
<pre>
Line 268 ⟶ 597:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">Func Issqr( n ) = if (Sqrt(n))^2=n then 1 else 0 fi.;
i:=3;
j:=3;
Line 280 ⟶ 609:
j:=j+2;
od;
od;</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
 
function nextprime( n as uinteger ) as uinteger
Line 305 ⟶ 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 333 ⟶ 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 360 ⟶ 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)
Line 387 ⟶ 887:
squareprimegaps(10_000_000_000)
 
</langsyntaxhighlight>{{out}}
<pre>
 
Line 416 ⟶ 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}}==
<langsyntaxhighlight 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;
{
(my is_square($diff) =and print "$primeref->[$i] - $primeref->[$i - 1]) >= 36 or next$diff\n";
}</syntaxhighlight>
is_square($diff) and print "$primeref->[$i + 1] - $primeref->[$i - 1] = $diff\n";
}</lang>
{{out}}
<pre>
8975989753 - 89689 = 64
107449107441 - 107377 = 64
288649288647 - 288583 = 64
368029368021 - 367957 = 64
381169381167 - 381103 = 64
396871396833 - 396733 = 100
400837400823 - 400759 = 64
445433445427 - 445363 = 64
623209623171 - 623107 = 64
625777625763 - 625699 = 64
637073637067 - 637003 = 64
710779710777 - 710713 = 64
725293725273 - 725209 = 64
779489779477 - 779413 = 64
801949801947 - 801883 = 64
803819803813 - 803749 = 64
821747821741 - 821677 = 64
832591832583 - 832519 = 64
838351838349 - 838249 = 100
844847844841 - 844777 = 64
883877883871 - 883807 = 64
912173912167 - 912103 = 64
919519919511 - 919447 = 64
954829954827 - 954763 = 64
981889981887 - 981823 = 64
997879997877 - 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 484 ⟶ 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 516 ⟶ 1,050:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
import math
print("working...")
Line 550 ⟶ 1,084:
 
print("done...")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 581 ⟶ 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 607 ⟶ 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 646 ⟶ 1,230:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 680 ⟶ 1,264:
next
return 0
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 711 ⟶ 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 716 ⟶ 1,431:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./fmt" for Fmt
 
Line 730 ⟶ 1,445:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 764 ⟶ 1,479:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if odd N > 2 is prime
int N, I;
[for I:= 3 to sqrt(N) do
Line 791 ⟶ 1,506:
N:= N+1; \step by 1+1 = 2 (for odd numbers)
];
]</langsyntaxhighlight>
 
{{out}}
1,975

edits