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

Content added Content deleted
m (→‎{{header|J}}: document the algorithm)
m (syntax highlighting fixup automation)
Line 6: Line 6:


=={{header|11l}}==
=={{header|11l}}==
<lang 11l>F primes_upto(limit)
<syntaxhighlight lang="11l">F primes_upto(limit)
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
Line 24: Line 24:
V diff = pr1 - pr2
V diff = pr1 - pr2
I (is_square(diff) & diff > 36)
I (is_square(diff) & diff > 36)
print(pr1‘ ’pr2‘ diff = ’diff)</lang>
print(pr1‘ ’pr2‘ diff = ’diff)</syntaxhighlight>


{{out}}
{{out}}
Line 58: Line 58:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # find a adjacent primes where the primes differ by a square > 36 #
<syntaxhighlight lang="algol68">BEGIN # find a adjacent primes where the primes differ by a square > 36 #
INT min diff = 37;
INT min diff = 37;
INT max prime = 1 000 000;
INT max prime = 1 000 000;
Line 81: Line 81:
FI
FI
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 113: Line 113:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f FIND_ADJACENTS_PRIMES_WHICH_DIFFERENCE_IS_SQUARE_INTEGER.AWK
# syntax: GAWK -f FIND_ADJACENTS_PRIMES_WHICH_DIFFERENCE_IS_SQUARE_INTEGER.AWK
# converted from FreeBASIC
# converted from FreeBASIC
Line 155: Line 155:
return(q)
return(q)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 188: Line 188:


=={{header|C}}==
=={{header|C}}==
<lang c>#include<stdio.h>
<syntaxhighlight lang="c">#include<stdio.h>
#include<stdlib.h>
#include<stdlib.h>


Line 222: Line 222:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|CLU}}==
=={{header|CLU}}==
<lang clu>% Integer square root
<syntaxhighlight lang="clu">% Integer square root
isqrt = proc (s: int) returns (int)
isqrt = proc (s: int) returns (int)
x0: int := s/2
x0: int := s/2
Line 285: Line 285:
end
end
end
end
end start_up</lang>
end start_up</syntaxhighlight>
{{out}}
{{out}}
<pre> 89753 - 89689 = 64 = 8^2
<pre> 89753 - 89689 = 64 = 8^2
Line 316: Line 316:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Find adjacents primes which difference is square integer . Nigel Galloway: November 23rd., 2021
// 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")
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}}
{{out}}
<pre>
<pre>
Line 352: Line 352:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: formatting io kernel lists lists.lazy math math.functions
<syntaxhighlight lang="factor">USING: formatting io kernel lists lists.lazy math math.functions
math.primes.lists sequences ;
math.primes.lists sequences ;


Line 374: Line 374:
"============================" print
"============================" print
big-sq-adj-primes-diff [ second 1,000,000 < ] lwhile
big-sq-adj-primes-diff [ second 1,000,000 < ] lwhile
[ "%-6d %-6d %d\n" vprintf ] leach</lang>
[ "%-6d %-6d %d\n" vprintf ] leach</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 410: Line 410:


=={{header|Fermat}}==
=={{header|Fermat}}==
<lang fermat>Func Issqr( n ) = if (Sqrt(n))^2=n then 1 else 0 fi.;
<syntaxhighlight lang="fermat">Func Issqr( n ) = if (Sqrt(n))^2=n then 1 else 0 fi.;
i:=3;
i:=3;
j:=3;
j:=3;
Line 422: Line 422:
j:=j+2;
j:=j+2;
od;
od;
od;</lang>
od;</syntaxhighlight>


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>#include "isprime.bas"
<syntaxhighlight lang="freebasic">#include "isprime.bas"


function nextprime( n as uinteger ) as uinteger
function nextprime( n as uinteger ) as uinteger
Line 447: Line 447:
if j-i > 36 and issquare(j-i) then print i, j, j-i
if j-i > 36 and issquare(j-i) then print i, j, j-i
i = j
i = j
wend</lang>
wend</syntaxhighlight>
{{out}}<pre>
{{out}}<pre>
89689 89753 64
89689 89753 64
Line 480: Line 480:
{{trans|Wren}}
{{trans|Wren}}
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 503: Line 503:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 511: Line 511:


=={{header|GW-BASIC}}==
=={{header|GW-BASIC}}==
<lang gwbasic>10 P=3 : P2=0
<syntaxhighlight lang="gwbasic">10 P=3 : P2=0
20 GOSUB 180
20 GOSUB 180
30 IF P2>1000000! THEN END
30 IF P2>1000000! THEN END
Line 535: Line 535:
230 GOSUB 80
230 GOSUB 80
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</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==
<lang J> #(,.-~/"1) p:0 1+/~I.(= <.)6.5>.%:2-~/\p:i.p:inv 1e6 NB. count them
<syntaxhighlight lang="j"> #(,.-~/"1) p:0 1+/~I.(= <.)6.5>.%:2-~/\p:i.p:inv 1e6 NB. count them
26
26
(,.-~/"1) p:0 1+/~I.(= <.)6.5>.%:2-~/\p:i.p:inv 1e6 NB. show them
(,.-~/"1) p:0 1+/~I.(= <.)6.5>.%:2-~/\p:i.p:inv 1e6 NB. show them
Line 566: Line 566:
954763 954827 64
954763 954827 64
981823 981887 64
981823 981887 64
997813 997877 64</lang>
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.
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.
Line 577: Line 577:


'''Preliminaries'''
'''Preliminaries'''
<lang jq>def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
<syntaxhighlight lang="jq">def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;


# Primes less than . // infinite
# Primes less than . // infinite
Line 584: Line 584:
| if $n < 3 then empty
| if $n < 3 then empty
else 2, (range(3; $n) | select(is_prime))
else 2, (range(3; $n) | select(is_prime))
end;</lang>
end;</syntaxhighlight>
'''The task'''
'''The task'''
<lang jq># Input is given to primes/0 - to determine the maximum prime to consider
<syntaxhighlight lang="jq"># Input is given to primes/0 - to determine the maximum prime to consider
# Output: stream of [$prime, $nextPrime]
# Output: stream of [$prime, $nextPrime]
def adjacentPrimesWhichDifferBySquare:
def adjacentPrimesWhichDifferBySquare:
Line 611: Line 611:
| "\(.[1]|l) - \(.[0]|l) = \($diff|lpad(4))" ) ;
| "\(.[1]|l) - \(.[0]|l) = \($diff|lpad(4))" ) ;


1E6 | task(36)</lang>
1E6 | task(36)</syntaxhighlight>
{{out}}
{{out}}
As for [[#ALGOL_68]].
As for [[#ALGOL_68]].


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Primes
<syntaxhighlight lang="julia">using Primes


function squareprimegaps(limit)
function squareprimegaps(limit)
Line 640: Line 640:
squareprimegaps(10_000_000_000)
squareprimegaps(10_000_000_000)


</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>


Line 671: Line 671:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ps = Prime[Range[PrimePi[10^6]]];
<syntaxhighlight lang="mathematica">ps = Prime[Range[PrimePi[10^6]]];
ps = Partition[ps, 2, 1];
ps = Partition[ps, 2, 1];
ps = {#1, #2, #2 - #1} & @@@ ps;
ps = {#1, #2, #2 - #1} & @@@ ps;
ps //= Select[Extract[{3}]/*GreaterThan[36]];
ps //= Select[Extract[{3}]/*GreaterThan[36]];
ps //= Select[Extract[{3}]/*Sqrt/*IntegerQ];
ps //= Select[Extract[{3}]/*Sqrt/*IntegerQ];
ps // Grid</lang>
ps // Grid</syntaxhighlight>
{{out}}
{{out}}
<pre>89689 89753 64
<pre>89689 89753 64
Line 706: Line 706:


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<lang parigp>
<syntaxhighlight lang="parigp">
for(i=3,1000000,j=nextprime(i+1);if(isprime(i)&&j-i>36&&issquare(j-i),print(i," ",j," ",j-i)))
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}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # https://rosettacode.org/wiki/Find_adjacents_primes_which_difference_is_square_integer
use strict; # https://rosettacode.org/wiki/Find_adjacents_primes_which_difference_is_square_integer
Line 721: Line 721:
(my $diff = $primeref->[$i] - $primeref->[$i - 1]) > 36 or next;
(my $diff = $primeref->[$i] - $primeref->[$i - 1]) > 36 or next;
is_square($diff) and print "$primeref->[$i] - $primeref->[$i - 1] = $diff\n";
is_square($diff) and print "$primeref->[$i] - $primeref->[$i - 1] = $diff\n";
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 753: Line 753:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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 771: Line 771:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 803: Line 803:


=={{header|Python}}==
=={{header|Python}}==
<lang python>
<syntaxhighlight lang="python">
import math
import math
print("working...")
print("working...")
Line 837: Line 837:


print("done...")
print("done...")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 871: Line 871:


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6>use Lingua::EN::Numbers;
<syntaxhighlight lang="raku" line>use Lingua::EN::Numbers;
use Math::Primesieve;
use Math::Primesieve;


Line 894: Line 894:
say "\nGap {$p.key}: {comma @counts[$p.key]} found$ten:";
say "\nGap {$p.key}: {comma @counts[$p.key]} found$ten:";
put join "\n", $p.value.batch(5)».map({"($_, {$_+ $p.key})"})».join(', ');
put join "\n", $p.value.batch(5)».map({"($_, {$_+ $p.key})"})».join(', ');
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Adjacent primes up to 10,000,000,000 with a gap value that is a perfect square:
<pre>Adjacent primes up to 10,000,000,000 with a gap value that is a perfect square:
Line 933: Line 933:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "stdlib.ring"
see "working..." + nl
see "working..." + nl
Line 967: Line 967:
next
next
return 0
return 0
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,001: Line 1,001:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>require "prime"
<syntaxhighlight lang="ruby">require "prime"


Prime.each(1_000_000).each_cons(2) do |a, b|
Prime.each(1_000_000).each_cons(2) do |a, b|
Line 1,009: Line 1,009:
puts "#{b} - #{a} = #{diff}" if isqrt*isqrt == diff
puts "#{b} - #{a} = #{diff}" if isqrt*isqrt == diff
end
end
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>89753 - 89689 = 64
<pre>89753 - 89689 = 64
Line 1,039: Line 1,039:
</pre>
</pre>
=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>var p = 2
<syntaxhighlight lang="ruby">var p = 2
var upto = 1e6
var upto = 1e6


Line 1,047: Line 1,047:
}
}
p = q
p = q
})</lang>
})</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,081: Line 1,081:
{{libheader|Wren-math}}
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "./math" for Int
<syntaxhighlight lang="ecmascript">import "./math" for Int
import "./fmt" for Fmt
import "./fmt" for Fmt


Line 1,095: Line 1,095:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,129: Line 1,129:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func IsPrime(N); \Return 'true' if odd N > 2 is prime
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if odd N > 2 is prime
int N, I;
int N, I;
[for I:= 3 to sqrt(N) do
[for I:= 3 to sqrt(N) do
Line 1,156: Line 1,156:
N:= N+1; \step by 1+1 = 2 (for odd numbers)
N:= N+1; \step by 1+1 = 2 (for odd numbers)
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}