Quadrat special primes: Difference between revisions

Content added Content deleted
(J)
m (syntax highlighting fixup automation)
Line 10: Line 10:
{{trans|Nim}}
{{trans|Nim}}


<lang 11l>F is_prime(n)
<syntaxhighlight lang="11l">F is_prime(n)
I n == 2
I n == 2
R 1B
R 1B
Line 35: Line 35:
print(‘Quadrat special primes < 16000:’)
print(‘Quadrat special primes < 16000:’)
L(qp) quadraPrimes
L(qp) quadraPrimes
print(‘#5’.format(qp), end' I (L.index + 1) % 7 == 0 {"\n"} E ‘ ’)</lang>
print(‘#5’.format(qp), end' I (L.index + 1) % 7 == 0 {"\n"} E ‘ ’)</syntaxhighlight>


{{out}}
{{out}}
Line 51: Line 51:
=={{header|Action!}}==
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
{{libheader|Action! Sieve of Eratosthenes}}
<lang Action!>INCLUDE "H6:SIEVE.ACT"
<syntaxhighlight lang="action!">INCLUDE "H6:SIEVE.ACT"


DEFINE MAX="15999"
DEFINE MAX="15999"
Line 93: Line 93:
p=FindNextQuadraticPrime(p)
p=FindNextQuadraticPrime(p)
OD
OD
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Quadrat_Special_Primes.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Quadrat_Special_Primes.png Screenshot from Atari 8-bit computer]
Line 105: Line 105:
{{Trans|ALGOL W}}
{{Trans|ALGOL W}}
{{libheader|ALGOL 68-primes}}
{{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 #
# an array of squares #
# an array of squares #
PROC get squares = ( INT n )[]INT:
PROC get squares = ( INT n )[]INT:
Line 132: Line 132:
DO sq pos +:= 2 OD
DO sq pos +:= 2 OD
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 143: Line 143:


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
<lang algolw>begin % find some primes where the gap between the current prime and the next is a square %
<syntaxhighlight 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 %
% sets p( 1 :: n ) to a sieve of primes up to n %
procedure Eratosthenes ( logical array p( * ) ; integer value n ) ;
procedure Eratosthenes ( logical array p( * ) ; integer value n ) ;
Line 185: Line 185:
end while_thisPrime_lt_MAX_NUMBER
end while_thisPrime_lt_MAX_NUMBER
end
end
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 195: Line 195:
</pre>
</pre>
=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f QUADRAT_SPECIAL_PRIMES.AWK
# syntax: GAWK -f QUADRAT_SPECIAL_PRIMES.AWK
# converted from FreeBASIC
# converted from FreeBASIC
Line 228: Line 228:
return(1)
return(1)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 243: Line 243:
==={{header|Applesoft BASIC}}===
==={{header|Applesoft BASIC}}===
{{trans|BASIC256}}
{{trans|BASIC256}}
<lang gwbasic> 100 FOR P = 2 TO 16000
<syntaxhighlight lang="gwbasic"> 100 FOR P = 2 TO 16000
110 PRINT S$P;
110 PRINT S$P;
120 LET S$ = " "
120 LET S$ = " "
Line 264: Line 264:
290 IF NOT ISPRIME THEN RETURN
290 IF NOT ISPRIME THEN RETURN
300 NEXT D
300 NEXT D
310 RETURN</lang>
310 RETURN</syntaxhighlight>
==={{header|BASIC256}}===
==={{header|BASIC256}}===
<lang BASIC256>function isPrime(v)
<syntaxhighlight lang="basic256">function isPrime(v)
if v < 2 then return False
if v < 2 then return False
if v mod 2 = 0 then return v = 2
if v mod 2 = 0 then return v = 2
Line 291: Line 291:
j = 1
j = 1
end while
end while
end</lang>
end</syntaxhighlight>


==={{header|PureBasic}}===
==={{header|PureBasic}}===
<lang PureBasic>Procedure isPrime(v.i)
<syntaxhighlight lang="purebasic">Procedure isPrime(v.i)
If v <= 1 : ProcedureReturn #False
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
ElseIf v < 4 : ProcedureReturn #True
Line 333: Line 333:
ForEver
ForEver
Input()
Input()
CloseConsole()</lang>
CloseConsole()</syntaxhighlight>


==={{header|Yabasic}}===
==={{header|Yabasic}}===
<lang yabasic>sub isPrime(v)
<syntaxhighlight lang="yabasic">sub isPrime(v)
if v < 2 then return False : fi
if v < 2 then return False : fi
if mod(v, 2) = 0 then return v = 2 : fi
if mod(v, 2) = 0 then return v = 2 : fi
Line 361: Line 361:
j = 1
j = 1
loop
loop
end</lang>
end</syntaxhighlight>


=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.98}}
{{works with|Factor|0.98}}
<lang factor>USING: fry io kernel lists lists.lazy math math.primes prettyprint ;
<syntaxhighlight lang="factor">USING: fry io kernel lists lists.lazy math math.primes prettyprint ;


2 [ 1 lfrom swap '[ sq _ + ] lmap-lazy [ prime? ] lfilter car ]
2 [ 1 lfrom swap '[ sq _ + ] lmap-lazy [ prime? ] lfilter car ]
lfrom-by [ 16000 < ] lwhile [ pprint bl ] leach nl</lang>
lfrom-by [ 16000 < ] lwhile [ pprint bl ] leach nl</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 375: Line 375:


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


Line 391: Line 391:
loop
loop
print
print
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 399: Line 399:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Wren}}
{{trans|Wren}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 473: Line 473:
}
}
fmt.Println("\n", count+1, "such primes found.")
fmt.Println("\n", count+1, "such primes found.")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 481: Line 481:


=={{header|J}}==
=={{header|J}}==
<lang J>{{
<syntaxhighlight lang="j">{{
j=. 0
j=. 0
r=. 2
r=. 2
Line 488: Line 488:
end.
end.
}} p:i.p:inv 16e3
}} 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</lang>
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}}==
=={{header|jq}}==
'''Adaptation of [[#Julia|Julia]]
'''Adaptation of [[#Julia|Julia]]
Line 495: Line 495:


For the definition of `is_prime` used here, see https://rosettacode.org/wiki/Additive_primes
For the definition of `is_prime` used here, see https://rosettacode.org/wiki/Additive_primes
<syntaxhighlight lang="jq">
<lang jq>
# Input: a number > 2
# Input: a number > 2
# Output: an array of the quadrat primes less than `.`
# Output: an array of the quadrat primes less than `.`
Line 516: Line 516:
"Quadrat special primes < 16000:",
"Quadrat special primes < 16000:",
(16000 | quadrat[])
(16000 | quadrat[])
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 530: Line 530:


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


function quadrat(N = 16000)
function quadrat(N = 16000)
Line 550: Line 550:
println("Quadrat special primes < 16000:")
println("Quadrat special primes < 16000:")
foreach(p -> print(rpad(p[2], 6), p[1] % 10 == 0 ? "\n" : ""), enumerate(quadrat()))
foreach(p -> print(rpad(p[2], 6), p[1] % 10 == 0 ? "\n" : ""), enumerate(quadrat()))
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Quadrat special primes < 16000:
Quadrat special primes < 16000:
Line 561: Line 561:


=={{header|Ksh}}==
=={{header|Ksh}}==
<lang ksh>
<syntaxhighlight lang="ksh">
#!/bin/ksh
#!/bin/ksh


Line 613: Line 613:
done
done
printf "\n"
printf "\n"
</syntaxhighlight>
</lang>
{{out}}<pre>
{{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
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
Line 619: Line 619:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ps = {2};
<syntaxhighlight lang="mathematica">ps = {2};
Do[
Do[
Do[
Do[
Line 636: Line 636:
];
];
ps //= Most;
ps //= Most;
Multicolumn[ps, {Automatic, 7}, Appearance -> "Horizontal"]</lang>
Multicolumn[ps, {Automatic, 7}, Appearance -> "Horizontal"]</syntaxhighlight>
{{out}}
{{out}}
<pre>2 3 7 11 47 83 227
<pre>2 3 7 11 47 83 227
Line 647: Line 647:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import math, strutils, sugar
<syntaxhighlight lang="nim">import math, strutils, sugar


func isPrime(n: Natural): bool =
func isPrime(n: Natural): bool =
Line 685: Line 685:
for qp in quadraPrimes(Max):
for qp in quadraPrimes(Max):
inc count
inc count
stdout.write ($qp).align(5), if count mod 7 == 0: '\n' else: ' '</lang>
stdout.write ($qp).align(5), if count mod 7 == 0: '\n' else: ' '</syntaxhighlight>


{{out}}
{{out}}
Line 699: Line 699:
=={{header|Perl}}==
=={{header|Perl}}==
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 712: Line 712:
} until $sp[-1] >= 16000;
} until $sp[-1] >= 16000;


pop @sp and say ((sprintf '%-7d'x@sp, @sp) =~ s/.{1,$#sp}\K\s/\n/gr);</lang>
pop @sp and say ((sprintf '%-7d'x@sp, @sp) =~ s/.{1,$#sp}\K\s/\n/gr);</syntaxhighlight>
{{out}}
{{out}}
<pre>2 3 7 11 47 83 227
<pre>2 3 7 11 47 83 227
Line 723: Line 723:


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


=={{header|Python}}==
=={{header|Python}}==
<lang python>#!/usr/bin/python
<syntaxhighlight lang="python">#!/usr/bin/python


def isPrime(n):
def isPrime(n):
Line 808: Line 808:
break
break
print(p, end = " ");
print(p, end = " ");
j = 1</lang>
j = 1</syntaxhighlight>


=={{header|Raku}}==
=={{header|Raku}}==


<lang perl6>my @sqp = 2, -> $previous {
<syntaxhighlight lang="raku" line>my @sqp = 2, -> $previous {
my $next;
my $next;
for (1..∞).map: *² {
for (1..∞).map: *² {
Line 822: Line 822:


say "{+$_} matching numbers:\n", $_».fmt('%5d').batch(7).join: "\n" given
say "{+$_} matching numbers:\n", $_».fmt('%5d').batch(7).join: "\n" given
@sqp[^(@sqp.first: * > 16000, :k)];</lang>
@sqp[^(@sqp.first: * > 16000, :k)];</syntaxhighlight>
{{out}}
{{out}}
<pre>49 matching numbers:
<pre>49 matching numbers:
Line 834: Line 834:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program finds the smallest primes such that the difference of successive terms */
<syntaxhighlight lang="rexx">/*REXX program finds the smallest primes such that the difference of successive terms */
/*─────────────────────────────────────────────────── are the smallest quadrat numbers. */
/*─────────────────────────────────────────────────── are the smallest quadrat numbers. */
parse arg hi cols . /*obtain optional argument from the CL.*/
parse arg hi cols . /*obtain optional argument from the CL.*/
Line 883: Line 883:
end /*k*/ /* [↑] only process numbers ≤ √ J */
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</lang>
end /*j*/; return</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 899: Line 899:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>load "stdlib.ring"
<syntaxhighlight lang="ring">load "stdlib.ring"
/* Searching for the smallest prime gaps under a limit,
/* Searching for the smallest prime gaps under a limit,
Line 942: Line 942:
s = string(x) l = len(s)
s = string(x) l = len(s)
if l > y y = l ok
if l > y y = l ok
return substr(" ", 11 - y + l) + s</lang>
return substr(" ", 11 - y + l) + s</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:20em">working...
<pre style="height:20em">working...
Line 1,072: Line 1,072:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func quadrat_primes(callback) {
<syntaxhighlight lang="ruby">func quadrat_primes(callback) {


var prev = 2
var prev = 2
Line 1,089: Line 1,089:
take(k)
take(k)
})
})
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,098: Line 1,098:
{{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,121: Line 1,121:
}
}
}
}
System.print("\n%(count+1) such primes found.")</lang>
System.print("\n%(count+1) such primes found.")</syntaxhighlight>


{{out}}
{{out}}
Line 1,181: Line 1,181:
=={{header|XPL0}}==
=={{header|XPL0}}==
Find primes where the difference between the current one and a following one is a perfect square.
Find primes where the difference between the current one and a following one is a perfect square.
<lang XPL0>func IsPrime(N); \Return 'true' if N is a prime number
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
int N, I;
[if N <= 1 then return false;
[if N <= 1 then return false;
Line 1,206: Line 1,206:
Text(0, " such primes found below 16000.
Text(0, " such primes found below 16000.
");
");
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}