Find adjacent primes which differ by a square integer: Difference between revisions
Content added Content deleted
m (→{{header|J}}: document the algorithm) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 6:
=={{header|11l}}==
<
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)</
{{out}}
Line 58:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<
INT min diff = 37;
INT max prime = 1 000 000;
Line 81:
FI
OD
END</
{{out}}
<pre>
Line 113:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f FIND_ADJACENTS_PRIMES_WHICH_DIFFERENCE_IS_SQUARE_INTEGER.AWK
# converted from FreeBASIC
Line 155:
return(q)
}
</syntaxhighlight>
{{out}}
<pre>
Line 188:
=={{header|C}}==
<
#include<stdlib.h>
Line 222:
}
return 0;
}</
=={{header|CLU}}==
<
isqrt = proc (s: int) returns (int)
x0: int := s/2
Line 285:
end
end
end start_up</
{{out}}
<pre> 89753 - 89689 = 64 = 8^2
Line 316:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<
// 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>
Line 352:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
math.primes.lists sequences ;
Line 374:
"============================" print
big-sq-adj-primes-diff [ second 1,000,000 < ] lwhile
[ "%-6d %-6d %d\n" vprintf ] leach</
{{out}}
<pre>
Line 410:
=={{header|Fermat}}==
<
i:=3;
j:=3;
Line 422:
j:=j+2;
od;
od;</
=={{header|FreeBASIC}}==
<
function nextprime( n as uinteger ) as uinteger
Line 447:
if j-i > 36 and issquare(j-i) then print i, j, j-i
i = j
wend</
{{out}}<pre>
89689 89753 64
Line 480:
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 503:
}
}
}</
{{out}}
Line 511:
=={{header|GW-BASIC}}==
<
20 GOSUB 180
30 IF P2>1000000! THEN END
Line 535:
230 GOSUB 80
240 IF Q = 1 THEN P2 = P: P = T: RETURN
250 GOTO 220</
=={{header|J}}==
<
26
(,.-~/"1) p:0 1+/~I.(= <.)6.5>.%:2-~/\p:i.p:inv 1e6 NB. show them
Line 566:
954763 954827 64
981823 981887 64
997813 997877 64</
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:
'''Preliminaries'''
<
# Primes less than . // infinite
Line 584:
| if $n < 3 then empty
else 2, (range(3; $n) | select(is_prime))
end;</
'''The task'''
<
# Output: stream of [$prime, $nextPrime]
def adjacentPrimesWhichDifferBySquare:
Line 611:
| "\(.[1]|l) - \(.[0]|l) = \($diff|lpad(4))" ) ;
1E6 | task(36)</
{{out}}
As for [[#ALGOL_68]].
=={{header|Julia}}==
<
function squareprimegaps(limit)
Line 640:
squareprimegaps(10_000_000_000)
</
<pre>
Line 671:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
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</
{{out}}
<pre>89689 89753 64
Line 706:
=={{header|PARI/GP}}==
<
for(i=3,1000000,j=nextprime(i+1);if(isprime(i)&&j-i>36&&issquare(j-i),print(i," ",j," ",j-i)))
</syntaxhighlight>
=={{header|Perl}}==
<
use strict; # https://rosettacode.org/wiki/Find_adjacents_primes_which_difference_is_square_integer
Line 721:
(my $diff = $primeref->[$i] - $primeref->[$i - 1]) > 36 or next;
is_square($diff) and print "$primeref->[$i] - $primeref->[$i - 1] = $diff\n";
}</
{{out}}
<pre>
Line 753:
=={{header|Phix}}==
<!--<
<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 771:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 803:
=={{header|Python}}==
<
import math
print("working...")
Line 837:
print("done...")
</syntaxhighlight>
{{out}}
<pre>
Line 871:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
use Math::Primesieve;
Line 894:
say "\nGap {$p.key}: {comma @counts[$p.key]} found$ten:";
put join "\n", $p.value.batch(5)».map({"($_, {$_+ $p.key})"})».join(', ');
}</
{{out}}
<pre>Adjacent primes up to 10,000,000,000 with a gap value that is a perfect square:
Line 933:
=={{header|Ring}}==
<
load "stdlib.ring"
see "working..." + nl
Line 967:
next
return 0
</syntaxhighlight>
{{out}}
<pre>
Line 1,001:
=={{header|Ruby}}==
<
Prime.each(1_000_000).each_cons(2) do |a, b|
Line 1,009:
puts "#{b} - #{a} = #{diff}" if isqrt*isqrt == diff
end
</syntaxhighlight>
{{out}}
<pre>89753 - 89689 = 64
Line 1,039:
</pre>
=={{header|Sidef}}==
<
var upto = 1e6
Line 1,047:
}
p = q
})</
{{out}}
<pre>
Line 1,081:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<
import "./fmt" for Fmt
Line 1,095:
}
}
}</
{{out}}
Line 1,129:
=={{header|XPL0}}==
<
int N, I;
[for I:= 3 to sqrt(N) do
Line 1,156:
N:= N+1; \step by 1+1 = 2 (for odd numbers)
];
]</
{{out}}
|