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

Added Easylang
(J)
(Added Easylang)
 
(9 intermediate revisions by 7 users not shown)
Line 6:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">F primes_upto(limit)
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
Line 24:
V diff = pr1 - pr2
I (is_square(diff) & diff > 36)
print(pr1‘ ’pr2‘ diff = ’diff)</langsyntaxhighlight>
 
{{out}}
Line 58:
=={{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 81:
FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
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}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f FIND_ADJACENTS_PRIMES_WHICH_DIFFERENCE_IS_SQUARE_INTEGER.AWK
# converted from FreeBASIC
Line 155 ⟶ 195:
return(q)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 188 ⟶ 228:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include<stdio.h>
#include<stdlib.h>
 
Line 222 ⟶ 262:
}
return 0;
}</langsyntaxhighlight>
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">% Integer square root
isqrt = proc (s: int) returns (int)
x0: int := s/2
Line 285 ⟶ 325:
end
end
end start_up</langsyntaxhighlight>
{{out}}
<pre> 89753 - 89689 = 64 = 8^2
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#}}==
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 352 ⟶ 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 374 ⟶ 561:
"============================" print
big-sq-adj-primes-diff [ second 1,000,000 < ] lwhile
[ "%-6d %-6d %d\n" vprintf ] leach</langsyntaxhighlight>
{{out}}
<pre>
Line 410 ⟶ 597:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">Func Issqr( n ) = if (Sqrt(n))^2=n then 1 else 0 fi.;
i:=3;
j:=3;
Line 422 ⟶ 609:
j:=j+2;
od;
od;</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
 
function nextprime( n as uinteger ) as uinteger
Line 447 ⟶ 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 480 ⟶ 667:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 503 ⟶ 690:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 511 ⟶ 698:
 
=={{header|GW-BASIC}}==
<langsyntaxhighlight lang="gwbasic">10 P=3 : P2=0
20 GOSUB 180
30 IF P2>1000000! THEN END
Line 535 ⟶ 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}}==
<langsyntaxhighlight Jlang="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
Line 566 ⟶ 813:
954763 954827 64
981823 981887 64
997813 997877 64</langsyntaxhighlight>
 
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}}==
Line 575 ⟶ 824:
 
'''Preliminaries'''
<langsyntaxhighlight lang="jq">def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
# Primes less than . // infinite
Line 582 ⟶ 831:
| if $n < 3 then empty
else 2, (range(3; $n) | select(is_prime))
end;</langsyntaxhighlight>
'''The task'''
<langsyntaxhighlight lang="jq"># Input is given to primes/0 - to determine the maximum prime to consider
# Output: stream of [$prime, $nextPrime]
def adjacentPrimesWhichDifferBySquare:
Line 609 ⟶ 858:
| "\(.[1]|l) - \(.[0]|l) = \($diff|lpad(4))" ) ;
 
1E6 | task(36)</langsyntaxhighlight>
{{out}}
As for [[#ALGOL_68]].
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function squareprimegaps(limit)
Line 638 ⟶ 887:
squareprimegaps(10_000_000_000)
 
</langsyntaxhighlight>{{out}}
<pre>
 
Line 669 ⟶ 918:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="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</langsyntaxhighlight>
{{out}}
<pre>89689 89753 64
Line 704 ⟶ 953:
 
=={{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
Line 719 ⟶ 968:
(my $diff = $primeref->[$i] - $primeref->[$i - 1]) > 36 or next;
is_square($diff) and print "$primeref->[$i] - $primeref->[$i - 1] = $diff\n";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 751 ⟶ 1,000:
 
=={{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 769 ⟶ 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 801 ⟶ 1,050:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
import math
print("working...")
Line 835 ⟶ 1,084:
 
print("done...")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 866 ⟶ 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 892 ⟶ 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 931 ⟶ 1,230:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 965 ⟶ 1,264:
next
return 0
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 999 ⟶ 1,298:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require "prime"
 
Prime.each(1_000_000).each_cons(2) do |a, b|
Line 1,007 ⟶ 1,306:
puts "#{b} - #{a} = #{diff}" if isqrt*isqrt == diff
end
</syntaxhighlight>
</lang>
{{out}}
<pre>89753 - 89689 = 64
Line 1,036 ⟶ 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}}==
<langsyntaxhighlight lang="ruby">var p = 2
var upto = 1e6
 
Line 1,045 ⟶ 1,397:
}
p = q
})</langsyntaxhighlight>
{{out}}
<pre>
Line 1,079 ⟶ 1,431:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./fmt" for Fmt
 
Line 1,093 ⟶ 1,445:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,127 ⟶ 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 1,154 ⟶ 1,506:
N:= N+1; \step by 1+1 = 2 (for odd numbers)
];
]</langsyntaxhighlight>
 
{{out}}
1,975

edits