Quadrat special primes: Difference between revisions

m
m (→‎{{header|Perl}}: fix output)
m (→‎{{header|Wren}}: Minor tidy)
 
(29 intermediate revisions by 22 users not shown)
Line 3:
 
;Task:
Find the sequence of increasing primes, q, from 2 up to but excluding '''16,000''',
'''n'''   is smallest prime such that the difference of successive terms are the smallest squares of positive integers,
where the successor of q is the least prime, p, such that p - q is a perfect square.
where     '''n'''   <   '''16000'''.
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">F is_prime(n)
I n == 2
R 1B
I n < 2 | n % 2 == 0
R 0B
L(i) (3 .. Int(sqrt(n))).step(2)
I n % i == 0
R 0B
R 1B
 
V Max = 16'000
V quadraPrimes = [2, 3]
V n = 3
L
L(i) (2 .. Int(sqrt(Max))).step(2)
V next = n + i * i
I next >= Max
^L.break
I is_prime(next)
n = next
quadraPrimes [+]= n
L.break
 
print(‘Quadrat special primes < 16000:’)
L(qp) quadraPrimes
print(‘#5’.format(qp), end' I (L.index + 1) % 7 == 0 {"\n"} E ‘ ’)</syntaxhighlight>
 
{{out}}
<pre>
Quadrat special primes < 16000:
2 3 7 11 47 83 227
263 587 911 947 983 1019 1163
1307 1451 1487 1523 1559 2459 3359
4259 4583 5483 5519 5843 5879 6203
6779 7103 7247 7283 7607 7643 8219
8363 10667 11243 11279 11423 12323 12647
12791 13367 13691 14591 14627 14771 15671
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<syntaxhighlight lang="action!">INCLUDE "H6:SIEVE.ACT"
 
DEFINE MAX="15999"
DEFINE MAXSQUARES="126"
BYTE ARRAY primes(MAX+1)
INT ARRAY squares(MAXSQUARES)
 
PROC CalcSquares()
INT i
 
FOR i=1 TO MAXSQUARES
DO
squares(i-1)=i*i
OD
RETURN
 
INT FUNC FindNextQuadraticPrime(INT x)
INT i,next
 
FOR i=0 TO MAXSQUARES-1
DO
next=x+squares(i)
IF next>MAX THEN
RETURN (-1)
FI
IF primes(next) THEN
RETURN (next)
FI
OD
RETURN (-1)
 
PROC Main()
INT p=[2]
 
Put(125) PutE() ;clear the screen
Sieve(primes,MAX+1)
CalcSquares()
WHILE p>0
DO
PrintI(p) Put(32)
p=FindNextQuadraticPrime(p)
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Quadrat_Special_Primes.png Screenshot from Atari 8-bit computer]
<pre>
2 3 7 11 47 83 227 263 587 911 947 983 1019 1163 1307 1451 1487 1523 1559 2459
3359 4259 4583 5483 5519 5843 5879 6203 6779 7103 7247 7283 7607 7643 8219 8363
10667 11243 11279 11423 12323 12647 12791 13367 13691 14591 14627 14771 15671
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|ALGOL W}}
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # find some primes where the gap between the current prime and the next is a square #
<syntaxhighlight lang="algol68">BEGIN # find some primes where the gap between the current prime and the next is a square #
# reurns a sieve of primes up to n #
PROC eratosthenes = ( INT n )[]BOOL:
BEGIN
[ 1 : n ]BOOL p;
p[ 1 ] := FALSE; p[ 2 ] := TRUE;
FOR i FROM 3 BY 2 TO n DO p[ i ] := TRUE OD;
FOR i FROM 4 BY 2 TO n DO p[ i ] := FALSE OD;
FOR i FROM 3 BY 2 TO ENTIER sqrt( n ) DO
IF p[ i ] THEN FOR s FROM i * i BY i + i TO n DO p[ s ] := FALSE OD FI
OD;
p
END # eratosthenes # ;
# an array of squares #
PROC get squares = ( INT n )[]INT:
Line 30 ⟶ 114:
END # get squares # ;
INT max number = 16000;
PR read "primes.incl.a68" PR
[]BOOL prime = eratosthenes( max number );
[]BOOL prime = PRIMESIEVE max number;
[]INT square = get squares( max number );
INT p count, this prime, next prime;
Line 47 ⟶ 132:
DO sq pos +:= 2 OD
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 58 ⟶ 143:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin % find some primes where the gap between the current prime and the next is a square %
% sets p( 1 :: n ) to a sieve of primes up to n %
procedure Eratosthenes ( logical array p( * ) ; integer value n ) ;
Line 100 ⟶ 185:
end while_thisPrime_lt_MAX_NUMBER
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 108 ⟶ 193:
10667 11243 11279 11423 12323 12647 12791 13367 13691 14591 14627 14771
15671
</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f QUADRAT_SPECIAL_PRIMES.AWK
# converted from FreeBASIC
BEGIN {
stop = 15999
p = 2
j = 1
printf("%5d ",p)
count++
while (1) {
while (1) {
if (is_prime(p+j*j)) { break }
j++
}
p += j*j
if (p > stop) { break }
printf("%5d%1s",p,++count%10?"":"\n")
j = 1
}
printf("\nQuadrat special primes 1-%d: %d\n",stop,count)
exit(0)
}
function is_prime(x, i) {
if (x <= 1) {
return(0)
}
for (i=2; i<=int(sqrt(x)); i++) {
if (x % i == 0) {
return(0)
}
}
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
2 3 7 11 47 83 227 263 587 911
947 983 1019 1163 1307 1451 1487 1523 1559 2459
3359 4259 4583 5483 5519 5843 5879 6203 6779 7103
7247 7283 7607 7643 8219 8363 10667 11243 11279 11423
12323 12647 12791 13367 13691 14591 14627 14771 15671
Quadrat special primes 1-15999: 49
</pre>
 
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
{{trans|BASIC256}}
<syntaxhighlight lang="gwbasic"> 100 FOR P = 2 TO 16000
110 PRINT S$P;
120 LET S$ = " "
130 FOR J = 1 TO 1E9
140 LET V = P + J * J
150 GOSUB 200"IS PRIME"
160 IF NOT ISPRIME THEN NEXT J
170 LET P = V - 1
180 NEXT P
190 END
200 DEF FN MOD(DIVISR) = V - INT (V / DIVISR) * DIVISR
210 ISPRIME = FALSE
220 IF V < 2 THEN RETURN
230 ISPRIME = V = 2
240 IF FN MOD(2) = 0 THEN RETURN
250 ISPRIME = V = 3
260 IF FN MOD(3) = 0 THEN RETURN
270 FOR D = 5 TO SQR (V) STEP 2
280 LET ISPRIME = FN MOD(D)
290 IF NOT ISPRIME THEN RETURN
300 NEXT D
310 RETURN</syntaxhighlight>
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">function isPrime(v)
if v < 2 then return False
if v mod 2 = 0 then return v = 2
if v mod 3 = 0 then return v = 3
d = 5
while d * d <= v
if v mod d = 0 then return False else d += 2
end while
return True
end function
 
p = 2
j = 1
 
print 2; " ";
while True
while True
if isPrime(p + j*j) then exit while
j += 1
end while
p += j*j
if p > 16000 then exit while
print p; " ";
j = 1
end while
end</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">Procedure isPrime(v.i)
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
ElseIf v % 2 = 0 : ProcedureReturn #False
ElseIf v < 9 : ProcedureReturn #True
ElseIf v % 3 = 0 : ProcedureReturn #False
Else
Protected r = Round(Sqr(v), #PB_Round_Down)
Protected f = 5
While f <= r
If v % f = 0 Or v % (f + 2) = 0
ProcedureReturn #False
EndIf
f + 6
Wend
EndIf
ProcedureReturn #True
EndProcedure
 
OpenConsole()
p.i = 2
j.i = 1
 
Print("2" + #TAB$)
Repeat
Repeat
If isPrime(p + j*j)
Break
EndIf
j + 1
ForEver
p + j*j
If p > 16000
Break
EndIf
Print(Str(p) + #TAB$)
j = 1
ForEver
Input()
CloseConsole()</syntaxhighlight>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">sub isPrime(v)
if v < 2 then return False : fi
if mod(v, 2) = 0 then return v = 2 : fi
if mod(v, 3) = 0 then return v = 3 : fi
d = 5
while d * d <= v
if mod(v, d) = 0 then return False else d = d + 2 : fi
wend
return True
end sub
 
p = 2
j = 1
 
print 2, " ";
do
do
if isPrime(p + j*j) then break : fi
j = j + 1
loop
p = p + j*j
if p > 16000 then break : fi
print p, " ";
j = 1
loop
end</syntaxhighlight>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
<syntaxhighlight lang="Delphi">
function IsPrime(N: int64): boolean;
{Fast, optimised prime test}
var I,Stop: int64;
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+0.0));
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;
 
 
 
procedure QuadratSpecialPrimes(Memo: TMemo);
var Q,P,Cnt: integer;
var IA: TIntegerDynArray;
begin
Memo.Lines.Add('Count Prime1 Prime2 Gap Sqrt');
Memo.Lines.Add('---------------------------------');
Cnt:=0;
Q:=2;
for P:=3 to 16000-1 do
if IsPrime(P) then
begin
if frac(sqrt(P - Q))=0 then
begin
Inc(Cnt);
Memo.Lines.Add(Format('%5D%7D%8D%7D%6.1f',[Cnt,Q,P,P-Q,Sqrt(P-Q)]));
Q:=P;
end;
end;
Memo.Lines.Add('Count = '+IntToStr(Cnt));
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
Count Prime1 Prime2 Gap Sqrt
---------------------------------
1 2 3 1 1.0
2 3 7 4 2.0
3 7 11 4 2.0
4 11 47 36 6.0
5 47 83 36 6.0
6 83 227 144 12.0
7 227 263 36 6.0
8 263 587 324 18.0
9 587 911 324 18.0
10 911 947 36 6.0
11 947 983 36 6.0
12 983 1019 36 6.0
13 1019 1163 144 12.0
14 1163 1307 144 12.0
15 1307 1451 144 12.0
16 1451 1487 36 6.0
17 1487 1523 36 6.0
18 1523 1559 36 6.0
19 1559 2459 900 30.0
20 2459 3359 900 30.0
21 3359 4259 900 30.0
22 4259 4583 324 18.0
23 4583 5483 900 30.0
24 5483 5519 36 6.0
25 5519 5843 324 18.0
26 5843 5879 36 6.0
27 5879 6203 324 18.0
28 6203 6779 576 24.0
29 6779 7103 324 18.0
30 7103 7247 144 12.0
31 7247 7283 36 6.0
32 7283 7607 324 18.0
33 7607 7643 36 6.0
34 7643 8219 576 24.0
35 8219 8363 144 12.0
36 8363 10667 2304 48.0
37 10667 11243 576 24.0
38 11243 11279 36 6.0
39 11279 11423 144 12.0
40 11423 12323 900 30.0
41 12323 12647 324 18.0
42 12647 12791 144 12.0
43 12791 13367 576 24.0
44 13367 13691 324 18.0
45 13691 14591 900 30.0
46 14591 14627 36 6.0
47 14627 14771 144 12.0
48 14771 15671 900 30.0
Count = 48
Elapsed Time: 111.233 ms.
</pre>
 
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<syntaxhighlight lang="fsharp">
//Quadrat special primes. Nigel Galloway: January 16th., 2023
let isPs(n:int)=MathNet.Numerics.Euclid.IsPerfectSquare n
let rec fG n g=seq{match Seq.tryHead g with Some h when isPs(h-n)->yield h; yield! fG h g |Some _->yield! fG n g |_->()}
fG 2 (primes32()|>Seq.takeWhile((>)16000))|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
{{out}}
<pre>
2 3 7 11 47 83 227 263 587 911 947 983 1019 1163 1307 1451 1487 1523 1559 2459 3359 4259 4583 5483 5519 5843 5879 6203 6779 7103 7247 7283 7607 7643 8219 8363 10667 11243 11279 11423 12323 12647 12791 13367 13691 14591 14627 14771 15671
</pre>
=={{header|Factor}}==
{{works with|Factor|0.98}}
<syntaxhighlight lang="factor">USING: fry io kernel lists lists.lazy math math.primes prettyprint ;
 
2 [ 1 lfrom swap '[ sq _ + ] lmap-lazy [ prime? ] lfilter car ]
lfrom-by [ 16000 < ] lwhile [ pprint bl ] leach nl</syntaxhighlight>
{{out}}
<pre>
2 3 7 11 47 83 227 263 587 911 947 983 1019 1163 1307 1451 1487 1523 1559 2459 3359 4259 4583 5483 5519 5843 5879 6203 6779 7103 7247 7283 7607 7643 8219 8363 10667 11243 11279 11423 12323 12647 12791 13367 13691 14591 14627 14771 15671
</pre>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
#include "isprime.bas"
 
Line 128 ⟶ 509:
loop
print
</syntaxhighlight>
</lang>
{{out}}
<pre>
2 3 7 11 47 83 227 263 587 911 947 983 1019 1163 1307 1451 1487 1523 1559 2459 3359 4259 4583 5483 5519 5843 5879 6203 6779 7103 7247 7283 7607 7643 8219 8363 10667 11243 11279 11423 12323 12647 12791 13367 13691 14591 14627 14771 15671
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn IsPrime( n as NSUInteger ) as BOOL
BOOL isPrime = YES
NSUInteger i
if n < 2 then exit fn = NO
if n = 2 then exit fn = YES
if n mod 2 == 0 then exit fn = NO
for i = 3 to int(n^.5) step 2
if n mod i == 0 then exit fn = NO
next
end fn = isPrime
 
local fn QuadratSpecialPrimes
NSUInteger p = 2, j = 1, count = 1
printf @"%6lu \b", 2
while (1)
count++
while (1)
if fn IsPrime( p + j*j ) then exit while
j += 1
wend
p += j*j
if p > 16000 then exit while
printf @"%6lu \b", p
if count == 7 then count = 0 : print
j = 1
wend
print
end fn
 
fn QuadratSpecialPrimes
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
2 3 7 11 47 83 227
263 587 911 947 983 1019 1163
1307 1451 1487 1523 1559 2459 3359
4259 4583 5483 5519 5843 5879 6203
6779 7103 7247 7283 7607 7643 8219
8363 10667 11243 11279 11423 12323 12647
12791 13367 13691 14591 14627 14771 15671
</pre>
 
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 210 ⟶ 639:
}
fmt.Println("\n", count+1, "such primes found.")
}</langsyntaxhighlight>
 
{{out}}
<pre>
Same as Wren example.
</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j">{{
j=. 0
r=. 2
while. (j=.j+1)<#y do.
if. (=<.)%:(j{y)-{:r do. r=. r, j{y end.
end.
}} p:i.p:inv 16e3
2 3 7 11 47 83 227 263 587 911 947 983 1019 1163 1307 1451 1487 1523 1559 2459 3359 4259 4583 5483 5519 5843 5879 6203 6779 7103 7247 7283 7607 7643 8219 8363 10667 11243 11279 11423 12323 12647 12791 13367 13691 14591 14627 14771 15671</syntaxhighlight>
=={{header|jq}}==
'''Adaptation of [[#Julia|Julia]]
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
For the definition of `is_prime` used here, see https://rosettacode.org/wiki/Additive_primes
<syntaxhighlight lang="jq">
# Input: a number > 2
# Output: an array of the quadrat primes less than `.`
def quadrat:
. as $N
| ($N|sqrt) as $lastn
| { qprimes: [2], q: 2 }
| until ( .qprimes[-1] >= $N or .q >= $N;
label $out
| foreach range(1; $lastn + 1) as $i (.;
.q = .qprimes[-1] + $i * $i
| if .q >= $N then .emit = true
elif .q|is_prime then .qprimes = .qprimes + [.q]
| .emit = true
else .
end;
select(.emit)) | {qprimes, q}, break $out )
| .qprimes ;
"Quadrat special primes < 16000:",
(16000 | quadrat[])
</syntaxhighlight>
{{out}}
<pre>
Quadrat special primes < 16000:
2
3
7
...
14627
14771
15671
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function quadrat(N = 16000)
Line 238 ⟶ 716:
println("Quadrat special primes < 16000:")
foreach(p -> print(rpad(p[2], 6), p[1] % 10 == 0 ? "\n" : ""), enumerate(quadrat()))
</langsyntaxhighlight>{{out}}
<pre>
Quadrat special primes < 16000:
Line 247 ⟶ 725:
12323 12647 12791 13367 13691 14591 14627 14771 15671
</pre>
 
=={{header|Ksh}}==
<syntaxhighlight lang="ksh">
#!/bin/ksh
 
# Quadrat Special Primes
 
# # Variables:
#
alias SHORTINT='typeset -si'
SHORTINT MAXN=16000
 
# # Functions:
#
 
# # Function _isquadrat(n, m) return 1 when (m-n) is a perfect square
#
function _isquadrat {
typeset _n ; SHORTINT _n=$1
typeset _m ; SHORTINT _m=$2
 
[[ $(( sqrt(_m - _n) )) == +(\d).+(\d) ]] && return 0
return 1
}
 
# # Function _isprime(n) return 1 for prime, 0 for not prime
#
function _isprime {
typeset _n ; integer _n=$1
typeset _i ; integer _i
 
(( _n < 2 )) && return 0
for (( _i=2 ; _i*_i<=_n ; _i++ )); do
(( ! ( _n % _i ) )) && return 0
done
return 1
}
 
######
# main #
######
 
SHORTINT i prev_pr=2
for ((i=2; i<MAXN; i++)); do
_isprime ${i}
if (( $? )); then
_isquadrat ${prev_pr} ${i}
if (( $? )); then
printf "%d " ${i}
prev_pr=${i}
fi
fi
done
printf "\n"
</syntaxhighlight>
{{out}}<pre>
2 3 7 11 47 83 227 263 587 911 947 983 1019 1163 1307 1451 1487 1523 1559 2459 3359 4259 4583 5483 5519 5843 5879 6203 6779 7103 7247 7283 7607 7643 8219 8363 10667 11243 11279 11423 12323 12647 12791 13367 13691 14591 14627 14771 15671
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ps = {2};
Do[
Do[
q = Last[ps] + i^2;
If[PrimeQ[q],
AppendTo[ps, q];
Break[]
]
,
{i, 1, \[Infinity]}
];
If[Last[ps] >= 16000,
Break[]]
,
{\[Infinity]}
];
ps //= Most;
Multicolumn[ps, {Automatic, 7}, Appearance -> "Horizontal"]</syntaxhighlight>
{{out}}
<pre>2 3 7 11 47 83 227
263 587 911 947 983 1019 1163
1307 1451 1487 1523 1559 2459 3359
4259 4583 5483 5519 5843 5879 6203
6779 7103 7247 7283 7607 7643 8219
8363 10667 11243 11279 11423 12323 12647
12791 13367 13691 14591 14627 14771 15671</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
quadrat(n):=block(aux:next_prime(n),while not integerp(sqrt(aux-n)) do aux:next_prime(aux),aux)$
block(a:2,append([a],makelist(a:quadrat(a),48)));
</syntaxhighlight>
{{out}}
<pre>
[2,3,7,11,47,83,227,263,587,911,947,983,1019,1163,1307,1451,1487,1523,1559,2459,3359,4259,4583,5483,5519,5843,5879,6203,6779,7103,7247,7283,7607,7643,8219,8363,10667,11243,11279,11423,12323,12647,12791,13367,13691,14591,14627,14771,15671]
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import math, strutils, sugar
 
func isPrime(n: Natural): bool =
if n < 2: return false
if n mod 2 == 0: return n == 2
if n mod 3 == 0: return n == 3
var d = 5
while d * d <= n:
if n mod d == 0: return false
inc d, 2
if n mod d == 0: return false
inc d, 4
result = true
 
const
Max = 16_000
Squares = collect(newSeq):
for n in countup(2, sqrt(Max.float).int, 2): n * n
 
iterator quadraPrimes(lim: Positive): int =
assert lim >= 3
yield 2
yield 3
var n = 3
block mainloop:
while true:
for square in Squares:
let next = n + square
if next > lim: break mainloop
if next.isPrime:
n = next
yield n
break
 
echo "Quadrat special primes < 16000:"
var count = 0
for qp in quadraPrimes(Max):
inc count
stdout.write ($qp).align(5), if count mod 7 == 0: '\n' else: ' '</syntaxhighlight>
 
{{out}}
<pre>Quadrat special primes < 16000:
2 3 7 11 47 83 227
263 587 911 947 983 1019 1163
1307 1451 1487 1523 1559 2459 3359
4259 4583 5483 5519 5843 5879 6203
6779 7103 7247 7283 7607 7643 8219
8363 10667 11243 11279 11423 12323 12647
12791 13367 13691 14591 14627 14771 15671</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 263 ⟶ 888:
} until $sp[-1] >= 16000;
 
pop @sp and say ((sprintf '%-7d'x@sp, @sp) =~ s/.{1,$#sp}\K\s/\n/gr);</langsyntaxhighlight>
{{out}}
<pre>2 3 7 11 47 83 227
Line 274 ⟶ 899:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">desc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"linear quadratic cubic quartic quintic sextic septic octic nonic decic"</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">limits</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">16000</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">15000</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">14e9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8025e5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">25e12</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">195e12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">75e11</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3e9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">11e8</span><span style="color: #0000FF;">}</span>
Line 296 ⟶ 921:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Found %d %s special primes &lt; %g:\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #000000;">desc</span><span style="color: #0000FF;">[</span><span style="color: #000000;">p</span><span style="color: #0000FF;">],</span><span style="color: #000000;">N</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 334 ⟶ 959:
2 3 1,073,741,827 1,073,742,851
</pre>
 
 
=={{header|Python}}==
<syntaxhighlight lang="python">#!/usr/bin/python
 
def isPrime(n):
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
 
 
if __name__ == '__main__':
p = 2
j = 1
print(2, end = " ");
while True:
while True:
if isPrime(p + j*j):
break
j += 1
p += j*j
if p > 16000:
break
print(p, end = " ");
j = 1</syntaxhighlight>
 
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>my @sqp = 2, -> $previous {
my $next;
for (1..∞).map: *² {
Line 347 ⟶ 998:
 
say "{+$_} matching numbers:\n", $_».fmt('%5d').batch(7).join: "\n" given
@sqp[^(@sqp.first: * > 16000, :k)];</langsyntaxhighlight>
{{out}}
<pre>49 matching numbers:
Line 359 ⟶ 1,010:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds the smallest primes such that the difference of successive terms */
/*─────────────────────────────────────────────────── are the smallest quadrat numbers. */
parse arg hi cols . /*obtain optional argument from the CL.*/
Line 366 ⟶ 1,017:
call genP /*build array of semaphores for primes.*/
w= 10 /*width of a number in any column. */
@sqptitle= 'the smallest primes < ' commas(hi) " such that the" ,
'difference of successive termaterms are the smallest quadrat numbers'
if cols>0 then say ' index │'center(@sqp title, 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
sqp= 0; idx= 1 idx= 1 /*initialize number of sqp and index.*/
op= 1
$= /*a list of nice quad─special primes (so far). */
do j=0 by 0
do k=1 until !.np; np= op + k*k /*find the next square + oldPrime.*/
Line 379 ⟶ 1,030:
sqp= sqp + 1 /*bump the number of sqp's. */
op= np /*assign the newPrime to the oldPrime*/
if cols==<0 then iterate /*Build the list (to be shown later)? */
c= commas(np) /*maybe add commas to the number. */
$= $ right(c, max(w, length(c) ) ) /*add a nicequadratic─special prime ──► list, allow big#.*/
if sqp//cols\==0 then iterate /*have we populated a line of output? */
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
Line 388 ⟶ 1,039:
 
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─')
say
say 'Found ' commas(sqp) " of " @sqp title
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 403 ⟶ 1,055:
if j// 3==0 then iterate /*" " " 3? */
if j// 7==0 then iterate /*" " " 7? */
/* [↑] the above five lines saves time*/
do k=5 while s.k<=j /* [↓] divide by the known odd primes.*/
if j // @.k == 0 then iterate j /*Is J ÷ X? Then not prime. ___ */
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 418 ⟶ 1,069:
31 │ 7,247 7,283 7,607 7,643 8,219 8,363 10,667 11,243 11,279 11,423
41 │ 12,323 12,647 12,791 13,367 13,691 14,591 14,627 14,771 15,671
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────
 
Found 49 of the smallest primes < 16,000 such that the difference of successive terma are the smallest quadrat numbers
Line 423 ⟶ 1,075:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">load "stdlib.ring"
/* Searching for the smallest prime gaps under a limit,
Line 466 ⟶ 1,118:
s = string(x) l = len(s)
if l > y y = l ok
return substr(" ", 11 - y + l) + s</langsyntaxhighlight>
{{out}}
<pre style="height:20em">working...
Line 594 ⟶ 1,246:
 
done...</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
≪ <span style="color:red">{ 2 } 2</span> DUP
'''DO'''
DUP NEXTPRIME
'''IF''' DUP2 SWAP - √ FP NOT '''THEN''' NIP SWAP OVER + SWAP DUP '''END'''
'''UNTIL''' DUP <span style="color:red">16000</span> ≥ '''END'''
DROP2
≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: {2 3 7 11 47 83 227 263 587 911 947 983 1019 1163 1307 1451 1487 1523 1559 2459 3359 4259 4583 5483 5519 5843 5879 6203 6779 7103 7247 7283 7607 7643 8219 8363 10667 11243 11279 11423 12323 12647 12791 13367 13691 14591 14627 14771 15671}
</pre>
Runs in 6 minutes 25 seconds on a HP-50g.
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
res = [2]
 
until res.last > 16000 do
res << (1..).detect{|n| (res.last + n**2).prime? } ** 2 + res.last
end
 
puts res[..-2].join(" ")
</syntaxhighlight>
{{out}}
<pre>2 3 7 11 47 83 227 263 587 911 947 983 1019 1163 1307 1451 1487 1523 1559 2459 3359 4259 4583 5483 5519 5843 5879 6203 6779 7103 7247 7283 7607 7643 8219 8363 10667 11243 11279 11423 12323 12647 12791 13367 13691 14591 14627 14771 15671
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func quadrat_primes(callback) {
 
var prev = 2
callback(prev)
 
loop {
var curr = (1..Inf -> lazy.map { prev + _**2 }.first { .is_prime })
callback(curr)
prev = curr
}
}
 
say gather {
quadrat_primes({|k|
break if (k >= 16000)
take(k)
})
}</syntaxhighlight>
{{out}}
<pre>
[2, 3, 7, 11, 47, 83, 227, 263, 587, 911, 947, 983, 1019, 1163, 1307, 1451, 1487, 1523, 1559, 2459, 3359, 4259, 4583, 5483, 5519, 5843, 5879, 6203, 6779, 7103, 7247, 7283, 7607, 7643, 8219, 8363, 10667, 11243, 11279, 11423, 12323, 12647, 12791, 13367, 13691, 14591, 14627, 14771, 15671]
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./fmt" for Fmt
 
var isSquare = Fn.new { |n|
Line 621 ⟶ 1,327:
}
}
System.print("\n%(count+1) such primes found.")</langsyntaxhighlight>
 
{{out}}
Line 677 ⟶ 1,383:
 
49 such primes found.
</pre>
 
=={{header|XPL0}}==
Find primes where the difference between the current one and a following one is a perfect square.
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
for I:= 2 to sqrt(N) do
if rem(N/I) = 0 then return false;
return true;
];
 
int Count, P, Q;
[Count:= 0;
P:= 2; Q:= 3;
repeat if IsPrime(Q) then
[if sq(sqrt(Q-P)) = Q-P then
[IntOut(0, P);
P:= Q;
Count:= Count+1;
if rem(Count/10) then ChOut(0, 9\tab\) else CrLf(0);
];
];
Q:= Q+2;
until P >= 16000;
CrLf(0);
IntOut(0, Count);
Text(0, " such primes found below 16000.
");
]</syntaxhighlight>
 
{{out}}
<pre>
2 3 7 11 47 83 227 263 587 911
947 983 1019 1163 1307 1451 1487 1523 1559 2459
3359 4259 4583 5483 5519 5843 5879 6203 6779 7103
7247 7283 7607 7643 8219 8363 10667 11243 11279 11423
12323 12647 12791 13367 13691 14591 14627 14771 15671
49 such primes found below 16000.
</pre>
9,476

edits