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

Added Easylang
m (syntax highlighting fixup automation)
(Added Easylang)
 
(7 intermediate revisions by 5 users not shown)
Line 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}}==
Line 313 ⟶ 353:
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#}}==
Line 536 ⟶ 723:
240 IF Q = 1 THEN P2 = P: P = T: RETURN
250 GOTO 220</syntaxhighlight>
 
=={{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}}==
Line 868 ⟶ 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>
 
Line 1,038 ⟶ 1,335:
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
Line 1,081 ⟶ 1,431:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
1,973

edits