Next special primes: Difference between revisions
Content deleted Content added
m →{{header|Phix}}: added personal tag |
|||
(19 intermediate revisions by 16 users not shown) | |||
Line 6: | Line 6: | ||
where '''n''' < '''1050'''. |
where '''n''' < '''1050'''. |
||
<br><br> |
<br><br> |
||
=={{header|11l}}== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="11l">F is_prime(a) |
|||
I a == 2 |
|||
R 1B |
|||
I a < 2 | a % 2 == 0 |
|||
R 0B |
|||
L(i) (3 .. Int(sqrt(a))).step(2) |
|||
I a % i == 0 |
|||
R 0B |
|||
R 1B |
|||
V p = 3 |
|||
V i = 2 |
|||
print(‘2 3’, end' ‘ ’) |
|||
L p + i < 1050 |
|||
I is_prime(p + i) |
|||
p += i |
|||
print(p, end' ‘ ’) |
|||
i += 2</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
2 3 5 11 19 29 41 59 79 101 127 157 191 227 269 313 359 409 461 521 587 659 733 809 887 967 1049 |
|||
</pre> |
|||
=={{header|Action!}}== |
|||
{{libheader|Action! Sieve of Eratosthenes}} |
|||
<syntaxhighlight lang="action!">INCLUDE "H6:SIEVE.ACT" |
|||
PROC Main() |
|||
DEFINE MAX="1049" |
|||
BYTE ARRAY primes(MAX+1) |
|||
INT i,count=[1],lastprime=[3],lastgap=[1] |
|||
Put(125) PutE() ;clear the screen |
|||
Sieve(primes,MAX+1) |
|||
PrintI(lastprime) |
|||
FOR i=1 TO MAX |
|||
DO |
|||
IF primes(i)=1 AND i-lastprime>lastgap THEN |
|||
lastgap=i-lastprime |
|||
lastprime=i |
|||
Put(32) PrintI(i) |
|||
count==+1 |
|||
FI |
|||
OD |
|||
PrintF("%E%EThere are %I next special primes",count) |
|||
RETURN</syntaxhighlight> |
|||
{{out}} |
|||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Next_special_primes.png Screenshot from Atari 8-bit computer] |
|||
<pre> |
|||
3 5 11 19 29 41 59 79 101 127 157 191 227 269 313 359 409 461 521 587 659 733 809 887 967 1049 |
|||
There are 26 next special primes |
|||
</pre> |
|||
=={{header|ALGOL 68}}== |
|||
{{Trans|ALGOL W}} |
|||
<syntaxhighlight lang="algol68"> |
|||
BEGIN # find some primes where the gap between the current prime and the # |
|||
# next is greater than the gap between previus primes and the current # |
|||
PR read "primes.incl.a68" PR # include prime utilities # |
|||
INT max number = 1050; # largest number we will consider # |
|||
[]BOOL prime = PRIMESIEVE max number; |
|||
print( ( " this next", newline ) ); |
|||
print( ( "element prime prime gap", newline ) ); |
|||
INT p count := 1, this prime := 2, next prime := 3; |
|||
PROC show prime = ( INT gap )VOID: |
|||
BEGIN |
|||
print( ( " ", whole( p count, - 5), ":", whole( this prime, - 5 ) ) ); |
|||
print( ( " ", whole( next prime, - 5 ), whole( gap, - 5 ) ) ); |
|||
print( ( newline ) ) |
|||
END # show prime # ; |
|||
# the first gap is 1 (between 2 and 3) the gap between all other primes # |
|||
# is even so we treat 2-3 as a special case # |
|||
show prime( 1 ); |
|||
INT p gap := 0; |
|||
WHILE this prime < max number DO |
|||
this prime := next prime; |
|||
WHILE p gap +:= 2; |
|||
next prime := this prime + p gap; |
|||
IF next prime > max number |
|||
THEN FALSE |
|||
ELSE NOT prime[ next prime ] |
|||
FI |
|||
DO SKIP OD; |
|||
IF next prime < max number THEN |
|||
p count +:= 1; |
|||
show prime( p gap ) |
|||
FI |
|||
OD |
|||
END |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
this next |
|||
element prime prime gap |
|||
1: 2 3 1 |
|||
2: 3 5 2 |
|||
3: 5 11 6 |
|||
4: 11 19 8 |
|||
5: 19 29 10 |
|||
6: 29 41 12 |
|||
7: 41 59 18 |
|||
8: 59 79 20 |
|||
9: 79 101 22 |
|||
10: 101 127 26 |
|||
11: 127 157 30 |
|||
12: 157 191 34 |
|||
13: 191 227 36 |
|||
14: 227 269 42 |
|||
15: 269 313 44 |
|||
16: 313 359 46 |
|||
17: 359 409 50 |
|||
18: 409 461 52 |
|||
19: 461 521 60 |
|||
20: 521 587 66 |
|||
21: 587 659 72 |
|||
22: 659 733 74 |
|||
23: 733 809 76 |
|||
24: 809 887 78 |
|||
25: 887 967 80 |
|||
26: 967 1049 82 |
|||
</pre> |
|||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
<syntaxhighlight lang="algolw"> |
|||
<lang algolw>begin % find some primes where the gap between the current prtime and the next is greater than % |
|||
begin % find some primes where the gap between the current prime and the next is greater than % |
|||
% the gap between previus primes and the current % |
% the gap between previus primes and the current % |
||
% sets p( 1 :: n ) to a sieve of primes up to n % |
% sets p( 1 :: n ) to a sieve of primes up to n % |
||
Line 47: | Line 176: | ||
end while_thisPrime_lt_MAX_NUMBER |
end while_thisPrime_lt_MAX_NUMBER |
||
end |
end |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 79: | Line 208: | ||
26: 967 1049 82 |
26: 967 1049 82 |
||
</pre> |
</pre> |
||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="rebol">specials: new [2 3] |
|||
lim: 1050 |
|||
lastP: 3 |
|||
lastGap: 1 |
|||
loop 5.. .step:2 lim 'n [ |
|||
if not? prime? n -> continue |
|||
if lastGap < n - lastP [ |
|||
lastGap: n - lastP |
|||
lastP: n |
|||
'specials ++ n |
|||
] |
|||
] |
|||
print "List of next special primes less than 1050:" |
|||
print specials</syntaxhighlight> |
|||
{{out}} |
|||
<pre>List of next special primes less than 1050: |
|||
2 3 5 11 19 29 41 59 79 101 127 157 191 227 269 313 359 409 461 521 587 659 733 809 887 967 1049</pre> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f NEXT_SPECIAL_PRIMES.AWK |
# syntax: GAWK -f NEXT_SPECIAL_PRIMES.AWK |
||
BEGIN { |
BEGIN { |
||
Line 112: | Line 266: | ||
return(1) |
return(1) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 144: | Line 298: | ||
Next special primes 1-1050: 26 |
Next special primes 1-1050: 26 |
||
</pre> |
</pre> |
||
=={{header|BASIC}}== |
|||
==={{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 = 3 |
|||
i = 2 |
|||
print "2 3 "; #special case |
|||
do |
|||
if isPrime(p + i) then |
|||
p += i |
|||
print p; " "; |
|||
end if |
|||
i += 2 |
|||
until p + i >= 1050 |
|||
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 = 3 |
|||
i.i = 2 |
|||
Print("2 3 ") ;special Case |
|||
Repeat |
|||
If isPrime(p + i) |
|||
p + i |
|||
Print(Str(p) + " ") |
|||
EndIf |
|||
i + 2 |
|||
Until p + i >= 1050 |
|||
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 = 3 |
|||
i = 2 |
|||
print "2 3 "; //special case |
|||
repeat |
|||
if isPrime(p + i) = 1 then |
|||
p = p + i |
|||
print p; |
|||
endif |
|||
i = i + 2 |
|||
until p + i >= 1050 |
|||
end</syntaxhighlight> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdbool.h> |
#include <stdbool.h> |
||
Line 176: | Line 418: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 208: | Line 450: | ||
887 967 80 |
887 967 80 |
||
967 1049 82 |
967 1049 82 |
||
</pre> |
|||
=={{header|EasyLang}}== |
|||
{{trans|11l}} |
|||
<syntaxhighlight lang=text> |
|||
fastfunc isprim num . |
|||
i = 2 |
|||
while i <= sqrt num |
|||
if num mod i = 0 |
|||
return 0 |
|||
. |
|||
i += 1 |
|||
. |
|||
return 1 |
|||
. |
|||
p = 3 |
|||
i = 2 |
|||
write "2 3 " |
|||
while p + i < 1050 |
|||
if isprim (p + i) = 1 |
|||
p += i |
|||
write p & " " |
|||
. |
|||
i += 2 |
|||
. |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
2 3 5 11 19 29 41 59 79 101 127 157 191 227 269 313 359 409 461 521 587 659 733 809 887 967 1049 |
|||
</pre> |
</pre> |
||
=={{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#)] |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Next special primes. Nigel Galloway: March 26th., 2021 |
// Next special primes. Nigel Galloway: March 26th., 2021 |
||
let mP=let mutable n,g=2,0 in primes32()|>Seq.choose(fun y->match y-n>g,n with (true,i)->g<-y-n; n<-y; Some(i,g,y) |_->None) |
let mP=let mutable n,g=2,0 in primes32()|>Seq.choose(fun y->match y-n>g,n with (true,i)->g<-y-n; n<-y; Some(i,g,y) |_->None) |
||
mP|>Seq.takeWhile(fun(_,_,n)->n<1050)|>Seq.iteri(fun i (n,g,l)->printfn "n%d=%d n%d=%d n%d-n%d=%d" i n (i+1) l (i+1) i g) |
mP|>Seq.takeWhile(fun(_,_,n)->n<1050)|>Seq.iteri(fun i (n,g,l)->printfn "n%d=%d n%d=%d n%d-n%d=%d" i n (i+1) l (i+1) i g) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 248: | Line 519: | ||
Here's another way of writing the mP sequence above which is (hopefully) a little clearer: |
Here's another way of writing the mP sequence above which is (hopefully) a little clearer: |
||
< |
<syntaxhighlight lang="fsharp"> |
||
let mP = seq { |
let mP = seq { |
||
let mutable prevp, maxdiff = 2, 0 |
let mutable prevp, maxdiff = 2, 0 |
||
Line 258: | Line 529: | ||
prevp <- p |
prevp <- p |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.98}} |
{{works with|Factor|0.98}} |
||
<lang>USING: formatting io kernel math math.primes ; |
<syntaxhighlight lang="text">USING: formatting io kernel math math.primes ; |
||
"2 " write 1 3 |
"2 " write 1 3 |
||
[ dup 1050 < ] [ |
[ dup 1050 < ] [ |
||
2dup "(%d) %d " printf [ + next-prime ] keep 2dup - nip swap |
2dup "(%d) %d " printf [ + next-prime ] keep 2dup - nip swap |
||
] while 2drop nl</ |
] while 2drop nl</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 274: | Line 545: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">#include "isprime.bas" |
||
dim as integer p = 3, i = 2 |
dim as integer p = 3, i = 2 |
||
Line 285: | Line 556: | ||
end if |
end if |
||
i += 2 |
i += 2 |
||
loop until p+i >=1050 : print</ |
loop until p+i >=1050 : print</syntaxhighlight> |
||
{{out}}<pre>2 3 5 11 19 29 41 59 79 101 127 157 191 227 269 313 359 409 461 521 587 659 733 809 887 967 1049</pre> |
{{out}}<pre>2 3 5 11 19 29 41 59 79 101 127 157 191 227 269 313 359 409 461 521 587 659 733 809 887 967 1049</pre> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 333: | Line 604: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 366: | Line 637: | ||
967 1049 82 |
967 1049 82 |
||
</pre> |
</pre> |
||
=={{header|J}}== |
|||
<syntaxhighlight lang="j"> (, 4 p: (-~ +:)/@(_2&{.))^:(1049 > {:)^:_ (2 3) |
|||
2 3 5 11 19 29 41 59 79 101 127 157 191 227 269 313 359 409 461 521 587 659 733 809 887 967 1049</syntaxhighlight> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="java">class SpecialPrimes { |
||
private static boolean isPrime(int n) { |
private static boolean isPrime(int n) { |
||
if (n < 2) return false; |
if (n < 2) return false; |
||
Line 398: | Line 673: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 431: | Line 706: | ||
967 1049 82 |
967 1049 82 |
||
</pre> |
</pre> |
||
=={{header|jq}}== |
|||
{{works with|jq}} |
|||
'''Works with gojq, the Go implementation of jq''' |
|||
This entry uses `is_primes` as can be defined as in [[Erd%C5%91s-primes#jq]]. |
|||
<syntaxhighlight lang="jq"> |
|||
def primes: |
|||
2, (range(3;infinite;2) | select(is_prime)); |
|||
def emit_until(cond; stream): label $out | stream | if cond then break $out else . end; |
|||
def special_primes: |
|||
foreach primes as $p ({}; |
|||
.emit = null |
|||
| if .p == null then .p = $p | .emit = .p |
|||
else ($p - .p) as $g |
|||
| if $g > .gap then .p = $p | .gap=$g | .emit = .p |
|||
else . |
|||
end |
|||
end; |
|||
select(.emit).emit); |
|||
# The task |
|||
# The following assumesg invocation with the -n option: |
|||
emit_until(. >= 1050; special_primes)</syntaxhighlight> |
|||
{{out}} |
|||
Invocation example: jq -n -f program.jq |
|||
<pre> |
|||
2 |
|||
3 |
|||
5 |
|||
11 |
|||
19 |
|||
29 |
|||
41 |
|||
59 |
|||
79 |
|||
101 |
|||
127 |
|||
157 |
|||
191 |
|||
227 |
|||
269 |
|||
313 |
|||
359 |
|||
409 |
|||
461 |
|||
521 |
|||
587 |
|||
659 |
|||
733 |
|||
809 |
|||
887 |
|||
967 |
|||
1049 |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using Primes |
||
let |
let |
||
Line 448: | Line 781: | ||
end |
end |
||
end |
end |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Special primes under 1050: |
Special primes under 1050: |
||
Line 479: | Line 812: | ||
967 1049 82 |
967 1049 82 |
||
</pre> |
</pre> |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
|||
<syntaxhighlight lang="mathematica">max = 1050; |
|||
n = {2, 3}; |
|||
Do[ |
|||
If[PrimeQ[i], |
|||
lastdiff = n[[-1]] - n[[-2]]; |
|||
If[i - n[[-1]] > lastdiff, |
|||
AppendTo[n, i] |
|||
] |
|||
] |
|||
, |
|||
{i, Max[n] + 1, max} |
|||
] |
|||
n</syntaxhighlight> |
|||
{{out}} |
|||
<pre>{2, 3, 5, 11, 19, 29, 41, 59, 79, 101, 127, 157, 191, 227, 269, 313, 359, 409, 461, 521, 587, 659, 733, 809, 887, 967, 1049}</pre> |
|||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import strutils, sugar |
||
func isPrime(n: Positive): bool = |
func isPrime(n: Positive): bool = |
||
Line 506: | Line 856: | ||
let list = collect(newSeq, for p in nextSpecialPrimes(1050): p) |
let list = collect(newSeq, for p in nextSpecialPrimes(1050): p) |
||
echo "List of next special primes less than 1050:" |
echo "List of next special primes less than 1050:" |
||
echo list.join(" ")</ |
echo list.join(" ")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 516: | Line 866: | ||
just showing the small difference to increasing prime gaps.<BR>LastPrime is updated outside or inside If |
just showing the small difference to increasing prime gaps.<BR>LastPrime is updated outside or inside If |
||
< |
<syntaxhighlight lang="pascal"> |
||
program NextSpecialprimes; |
program NextSpecialprimes; |
||
//increasing prime gaps see |
//increasing prime gaps see |
||
Line 570: | Line 920: | ||
writeln; |
writeln; |
||
NextSpecial; |
NextSpecial; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 615: | Line 965: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use feature <state say>; |
use feature <state say>; |
||
Line 637: | Line 987: | ||
pop @specials; |
pop @specials; |
||
printf "%4d %4d\n", @$_ for @specials;</ |
printf "%4d %4d\n", @$_ for @specials;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 2 0 |
<pre> 2 0 |
||
Line 668: | Line 1,018: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #004080;">integer</span> <span style="color: #000000;">lastSpecial</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">lastGap</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> |
<span style="color: #004080;">integer</span> <span style="color: #000000;">lastSpecial</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">lastGap</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</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;">"Special primes under 1,050:\n"</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;">"Special primes under 1,050:\n"</span><span style="color: #0000FF;">)</span> |
||
Line 680: | Line 1,030: | ||
<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 712: | Line 1,062: | ||
967 1049 82 |
967 1049 82 |
||
</pre> |
</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 = 3 |
|||
i = 2 |
|||
print("2 3", end = " "); |
|||
while True: |
|||
if isPrime(p + i) == 1: |
|||
p += i |
|||
print(p, end = " "); |
|||
i += 2 |
|||
if p + i >= 1050: |
|||
break</syntaxhighlight> |
|||
=={{header|Quackery}}== |
|||
<code>isprime</code> is defined at [[Primality by trial division#Quackery]]. |
|||
<syntaxhighlight lang="Quackery"> ' [ 2 ] 1 |
|||
[ over -1 peek + |
|||
[ dup isprime |
|||
not while |
|||
1+ again ] |
|||
dup 1050 < while |
|||
join |
|||
dup -2 split nip |
|||
do swap - 1+ again ] |
|||
drop |
|||
echo |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>[ 2 3 5 11 19 29 41 59 79 101 127 157 191 227 269 313 359 409 461 521 587 659 733 809 887 967 1049 ]</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>sub is-special ( ($previous, $gap) ) { |
||
state @primes = grep *.is-prime, 2..*; |
state @primes = grep *.is-prime, 2..*; |
||
shift @primes while @primes[0] <= $previous + $gap; |
shift @primes while @primes[0] <= $previous + $gap; |
||
Line 724: | Line 1,119: | ||
my $limit = @specials.first: :k, *.[0] > 1050; |
my $limit = @specials.first: :k, *.[0] > 1050; |
||
say .fmt('%4d') for @specials.head($limit);</ |
say .fmt('%4d') for @specials.head($limit);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 757: | Line 1,152: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
{{trans|RING}} |
{{trans|RING}} |
||
< |
<syntaxhighlight lang="rexx">/*REXX program finds next special primes: difference of successive terms is increasing.*/ |
||
parse arg hi cols . /*obtain optional argument from the CL.*/ |
parse arg hi cols . /*obtain optional argument from the CL.*/ |
||
if hi=='' | hi=="," then hi= 1050 /* " " " " " " */ |
if hi=='' | hi=="," then hi= 1050 /* " " " " " " */ |
||
Line 793: | Line 1,188: | ||
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */ |
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */ |
||
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " flags. */ |
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " flags. */ |
||
#=5; |
#=5; sq.#= @.# **2 /*number of primes so far; prime². */ |
||
/* [↓] generate more primes ≤ high.*/ |
/* [↓] generate more primes ≤ high.*/ |
||
do j=@.#+2 by 2 to hi /*find odd primes from here on. */ |
do j=@.#+2 by 2 to hi /*find odd primes from here on. */ |
||
parse var j '' -1 _; |
parse var j '' -1 _; if _==5 then iterate /*J ÷ by 5? (right digit).*/ |
||
if j//3==0 then iterate; if j//7==0 then iterate /*" " " 3? J ÷ by 7? */ |
|||
do k=5 while sq.k<=j /* [↓] divide by the known odd primes.*/ |
|||
/* [↑] 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. ___ */ |
if j // @.k == 0 then iterate j /*Is J ÷ X? Then not prime. ___ */ |
||
end /*k*/ /* [↑] only process numbers ≤ √ J */ |
end /*k*/ /* [↑] only process numbers ≤ √ J */ |
||
#= #+1; @.#= j; |
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ |
||
end /*j*/; return</ |
end /*j*/; return</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 817: | Line 1,210: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
Line 842: | Line 1,235: | ||
see nl + "done..." + nl |
see nl + "done..." + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 873: | Line 1,266: | ||
967 1049 82 |
967 1049 82 |
||
done... |
done... |
||
</pre> |
|||
=={{header|RPL}}== |
|||
≪ 0 → gap |
|||
≪ {2} 2 1 CF |
|||
'''WHILE''' 1 FC? '''REPEAT''' |
|||
DUP |
|||
'''DO''' NEXTPRIME '''UNTIL''' DUP2 - gap < '''END''' |
|||
'''IF''' DUP 1050 < '''THEN''' |
|||
DUP2 - 'gap' STO |
|||
NIP SWAP OVER + SWAP |
|||
'''ELSE''' 1 SF '''END''' |
|||
'''END''' DROP2 |
|||
≫ ≫ '<span style="color:blue">TASK</span>' STO |
|||
{{out}} |
|||
<pre> |
|||
1: {2 3 5 11 19 29 41 59 79 101 127 157 191 227 269 313 359 409 461 521 587 659 733 809 887 967 1049} |
|||
</pre> |
|||
=={{header|Sidef}}== |
|||
<syntaxhighlight lang="ruby">func special_primes(upto) { |
|||
var gap = 0 |
|||
var prev = 2 |
|||
var list = [[prev, gap]] |
|||
loop { |
|||
var n = prev+gap |
|||
n = n.next_prime |
|||
break if (n > upto) |
|||
gap = n-prev |
|||
list << [n, gap] |
|||
prev = n |
|||
} |
|||
return list |
|||
} |
|||
special_primes(1050).each_2d {|p,gap| |
|||
say "#{'%4s' % p} #{'%4s' % gap}" |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
2 0 |
|||
3 1 |
|||
5 2 |
|||
11 6 |
|||
19 8 |
|||
29 10 |
|||
41 12 |
|||
59 18 |
|||
79 20 |
|||
101 22 |
|||
127 26 |
|||
157 30 |
|||
191 34 |
|||
227 36 |
|||
269 42 |
|||
313 44 |
|||
359 46 |
|||
409 50 |
|||
461 52 |
|||
521 60 |
|||
587 66 |
|||
659 72 |
|||
733 74 |
|||
809 76 |
|||
887 78 |
|||
967 80 |
|||
1049 82 |
|||
</pre> |
</pre> |
||
Line 878: | Line 1,341: | ||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="wren">import "./math" for Int |
||
import "/fmt" for Fmt |
import "./fmt" for Fmt |
||
var primes = Int.primeSieve(1049) |
var primes = Int.primeSieve(1049) |
||
Line 893: | Line 1,356: | ||
lastSpecial = p |
lastSpecial = p |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
|||
<pre> |
|||
Special primes under 1,050: |
|||
Prime1 Prime2 Gap |
|||
2 3 1 |
|||
3 5 2 |
|||
5 11 6 |
|||
11 19 8 |
|||
19 29 10 |
|||
29 41 12 |
|||
41 59 18 |
|||
59 79 20 |
|||
79 101 22 |
|||
101 127 26 |
|||
127 157 30 |
|||
157 191 34 |
|||
191 227 36 |
|||
227 269 42 |
|||
269 313 44 |
|||
313 359 46 |
|||
359 409 50 |
|||
409 461 52 |
|||
461 521 60 |
|||
521 587 66 |
|||
587 659 72 |
|||
659 733 74 |
|||
733 809 76 |
|||
809 887 78 |
|||
887 967 80 |
|||
967 1049 82 |
|||
</pre> |
|||
=={{header|XPL0}}== |
|||
{{trans|C}} |
|||
<syntaxhighlight lang "XPL0">include xpllib; \for IsPrime and Print |
|||
int I, LastSpecial, LastGap; |
|||
[LastSpecial:= 3; LastGap:= 1; |
|||
Print("Special primes under 1,050:\n"); |
|||
Print("Prime1 Prime2 Gap\n"); |
|||
Print("%6d %6d %3d\n", 2, 3, LastGap); |
|||
for I:= 5 to 1050-1 do |
|||
[if IsPrime(I) and I-LastSpecial > LastGap then |
|||
[LastGap:= I - LastSpecial; |
|||
Print("%6d %6d %3d\n", LastSpecial, I, LastGap); |
|||
LastSpecial:= I; |
|||
]; |
|||
I:= I+1; |
|||
]; |
|||
]</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
Latest revision as of 22:00, 22 July 2024
Next special primes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
- Task
n is smallest prime such that the difference of successive terms is strictly increasing,
where n < 1050.
11l
F is_prime(a)
I a == 2
R 1B
I a < 2 | a % 2 == 0
R 0B
L(i) (3 .. Int(sqrt(a))).step(2)
I a % i == 0
R 0B
R 1B
V p = 3
V i = 2
print(‘2 3’, end' ‘ ’)
L p + i < 1050
I is_prime(p + i)
p += i
print(p, end' ‘ ’)
i += 2
- Output:
2 3 5 11 19 29 41 59 79 101 127 157 191 227 269 313 359 409 461 521 587 659 733 809 887 967 1049
Action!
INCLUDE "H6:SIEVE.ACT"
PROC Main()
DEFINE MAX="1049"
BYTE ARRAY primes(MAX+1)
INT i,count=[1],lastprime=[3],lastgap=[1]
Put(125) PutE() ;clear the screen
Sieve(primes,MAX+1)
PrintI(lastprime)
FOR i=1 TO MAX
DO
IF primes(i)=1 AND i-lastprime>lastgap THEN
lastgap=i-lastprime
lastprime=i
Put(32) PrintI(i)
count==+1
FI
OD
PrintF("%E%EThere are %I next special primes",count)
RETURN
- Output:
Screenshot from Atari 8-bit computer
3 5 11 19 29 41 59 79 101 127 157 191 227 269 313 359 409 461 521 587 659 733 809 887 967 1049 There are 26 next special primes
ALGOL 68
BEGIN # find some primes where the gap between the current prime and the #
# next is greater than the gap between previus primes and the current #
PR read "primes.incl.a68" PR # include prime utilities #
INT max number = 1050; # largest number we will consider #
[]BOOL prime = PRIMESIEVE max number;
print( ( " this next", newline ) );
print( ( "element prime prime gap", newline ) );
INT p count := 1, this prime := 2, next prime := 3;
PROC show prime = ( INT gap )VOID:
BEGIN
print( ( " ", whole( p count, - 5), ":", whole( this prime, - 5 ) ) );
print( ( " ", whole( next prime, - 5 ), whole( gap, - 5 ) ) );
print( ( newline ) )
END # show prime # ;
# the first gap is 1 (between 2 and 3) the gap between all other primes #
# is even so we treat 2-3 as a special case #
show prime( 1 );
INT p gap := 0;
WHILE this prime < max number DO
this prime := next prime;
WHILE p gap +:= 2;
next prime := this prime + p gap;
IF next prime > max number
THEN FALSE
ELSE NOT prime[ next prime ]
FI
DO SKIP OD;
IF next prime < max number THEN
p count +:= 1;
show prime( p gap )
FI
OD
END
- Output:
this next element prime prime gap 1: 2 3 1 2: 3 5 2 3: 5 11 6 4: 11 19 8 5: 19 29 10 6: 29 41 12 7: 41 59 18 8: 59 79 20 9: 79 101 22 10: 101 127 26 11: 127 157 30 12: 157 191 34 13: 191 227 36 14: 227 269 42 15: 269 313 44 16: 313 359 46 17: 359 409 50 18: 409 461 52 19: 461 521 60 20: 521 587 66 21: 587 659 72 22: 659 733 74 23: 733 809 76 24: 809 887 78 25: 887 967 80 26: 967 1049 82
ALGOL W
begin % find some primes where the gap between the current prime and the next is greater than %
% the gap between previus primes and the current %
% sets p( 1 :: n ) to a sieve of primes up to n %
procedure Eratosthenes ( logical array p( * ) ; integer value n ) ;
begin
p( 1 ) := false; p( 2 ) := true;
for i := 3 step 2 until n do p( i ) := true;
for i := 4 step 2 until n do p( i ) := false;
for i := 3 step 2 until truncate( sqrt( n ) ) do begin
integer ii; ii := i + i;
if p( i ) then for pr := i * i step ii until n do p( pr ) := false
end for_i ;
end Eratosthenes ;
integer MAX_NUMBER;
MAX_NUMBER := 1050;
begin
logical array prime( 1 :: MAX_NUMBER );
integer pCount, pGap, thisPrime, nextPrime;
% sieve the primes to MAX_NUMBER %
Eratosthenes( prime, MAX_NUMBER );
% the first gap is 1 (between 2 and 3) the gap between all other primes is even %
% so we treat 2-3 as a special case %
write( " this next" );
write( "element prime prime gap" );
pCount := pGap := 1; thisPrime := 2; nextPrime := 3;
write( i_w := 5, s_w := 0, " ", pCount, ":", thisPrime, " ", nextPrime, pGap );
pGap := 0;
while thisPrime < MAX_NUMBER do begin
thisPrime := nextPrime;
while begin
pGap := pGap + 2;
nextPrime := thisPrime + pGap;
nextPrime < MAX_NUMBER and not prime( nextPrime )
end do begin end;
if nextPrime < MAX_NUMBER then begin
pCount := pCount + 1;
write( i_w := 5, s_w := 0, " ", pCount, ":", thisPrime, " ", nextPrime, pGap )
end if_nextPrime_lt_MAX_NUMBER
end while_thisPrime_lt_MAX_NUMBER
end
end.
- Output:
this next element prime prime gap 1: 2 3 1 2: 3 5 2 3: 5 11 6 4: 11 19 8 5: 19 29 10 6: 29 41 12 7: 41 59 18 8: 59 79 20 9: 79 101 22 10: 101 127 26 11: 127 157 30 12: 157 191 34 13: 191 227 36 14: 227 269 42 15: 269 313 44 16: 313 359 46 17: 359 409 50 18: 409 461 52 19: 461 521 60 20: 521 587 66 21: 587 659 72 22: 659 733 74 23: 733 809 76 24: 809 887 78 25: 887 967 80 26: 967 1049 82
Arturo
specials: new [2 3]
lim: 1050
lastP: 3
lastGap: 1
loop 5.. .step:2 lim 'n [
if not? prime? n -> continue
if lastGap < n - lastP [
lastGap: n - lastP
lastP: n
'specials ++ n
]
]
print "List of next special primes less than 1050:"
print specials
- Output:
List of next special primes less than 1050: 2 3 5 11 19 29 41 59 79 101 127 157 191 227 269 313 359 409 461 521 587 659 733 809 887 967 1049
AWK
# syntax: GAWK -f NEXT_SPECIAL_PRIMES.AWK
BEGIN {
start = 1
stop = 1050
print("Prime1 Prime2 Gap")
last_special = 3
last_gap = 1
printf("%6d %6d %3d\n",2,3,last_gap)
count = 1
for (i=start; i<=stop; i++) {
if (is_prime(i) && i-last_special > last_gap) {
last_gap = i - last_special
printf("%6d %6d %3d\n",last_special,i,last_gap)
last_special = i
count++
}
}
printf("Next special primes %d-%d: %d\n",start,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)
}
- Output:
Prime1 Prime2 Gap 2 3 1 3 5 2 5 11 6 11 19 8 19 29 10 29 41 12 41 59 18 59 79 20 79 101 22 101 127 26 127 157 30 157 191 34 191 227 36 227 269 42 269 313 44 313 359 46 359 409 50 409 461 52 461 521 60 521 587 66 587 659 72 659 733 74 733 809 76 809 887 78 887 967 80 967 1049 82 Next special primes 1-1050: 26
BASIC
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 = 3
i = 2
print "2 3 "; #special case
do
if isPrime(p + i) then
p += i
print p; " ";
end if
i += 2
until p + i >= 1050
end
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 = 3
i.i = 2
Print("2 3 ") ;special Case
Repeat
If isPrime(p + i)
p + i
Print(Str(p) + " ")
EndIf
i + 2
Until p + i >= 1050
Input()
CloseConsole()
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 = 3
i = 2
print "2 3 "; //special case
repeat
if isPrime(p + i) = 1 then
p = p + i
print p;
endif
i = i + 2
until p + i >= 1050
end
C
#include <stdio.h>
#include <stdbool.h>
bool isPrime(int n) {
int d;
if (n < 2) return false;
if (!(n%2)) return n == 2;
if (!(n%3)) return n == 3;
d = 5;
while (d*d <= n) {
if (!(n%d)) return false;
d += 2;
if (!(n%d)) return false;
d += 4;
}
return true;
}
int main() {
int i, lastSpecial = 3, lastGap = 1;
printf("Special primes under 1,050:\n");
printf("Prime1 Prime2 Gap\n");
printf("%6d %6d %3d\n", 2, 3, lastGap);
for (i = 5; i < 1050; i += 2) {
if (isPrime(i) && (i-lastSpecial) > lastGap) {
lastGap = i - lastSpecial;
printf("%6d %6d %3d\n", lastSpecial, i, lastGap);
lastSpecial = i;
}
}
}
- Output:
Special primes under 1,050: Prime1 Prime2 Gap 2 3 1 3 5 2 5 11 6 11 19 8 19 29 10 29 41 12 41 59 18 59 79 20 79 101 22 101 127 26 127 157 30 157 191 34 191 227 36 227 269 42 269 313 44 313 359 46 359 409 50 409 461 52 461 521 60 521 587 66 587 659 72 659 733 74 733 809 76 809 887 78 887 967 80 967 1049 82
EasyLang
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
p = 3
i = 2
write "2 3 "
while p + i < 1050
if isprim (p + i) = 1
p += i
write p & " "
.
i += 2
.
- Output:
2 3 5 11 19 29 41 59 79 101 127 157 191 227 269 313 359 409 461 521 587 659 733 809 887 967 1049
F#
This task uses Extensible Prime Generator (F#)
// Next special primes. Nigel Galloway: March 26th., 2021
let mP=let mutable n,g=2,0 in primes32()|>Seq.choose(fun y->match y-n>g,n with (true,i)->g<-y-n; n<-y; Some(i,g,y) |_->None)
mP|>Seq.takeWhile(fun(_,_,n)->n<1050)|>Seq.iteri(fun i (n,g,l)->printfn "n%d=%d n%d=%d n%d-n%d=%d" i n (i+1) l (i+1) i g)
- Output:
n0=2 n1=3 n1-n0=1 n1=3 n2=5 n2-n1=2 n2=5 n3=11 n3-n2=6 n3=11 n4=19 n4-n3=8 n4=19 n5=29 n5-n4=10 n5=29 n6=41 n6-n5=12 n6=41 n7=59 n7-n6=18 n7=59 n8=79 n8-n7=20 n8=79 n9=101 n9-n8=22 n9=101 n10=127 n10-n9=26 n10=127 n11=157 n11-n10=30 n11=157 n12=191 n12-n11=34 n12=191 n13=227 n13-n12=36 n13=227 n14=269 n14-n13=42 n14=269 n15=313 n15-n14=44 n15=313 n16=359 n16-n15=46 n16=359 n17=409 n17-n16=50 n17=409 n18=461 n18-n17=52 n18=461 n19=521 n19-n18=60 n19=521 n20=587 n20-n19=66 n20=587 n21=659 n21-n20=72 n21=659 n22=733 n22-n21=74 n22=733 n23=809 n23-n22=76 n23=809 n24=887 n24-n23=78 n24=887 n25=967 n25-n24=80 n25=967 n26=1049 n26-n25=82
Here's another way of writing the mP sequence above which is (hopefully) a little clearer:
let mP = seq {
let mutable prevp, maxdiff = 2, 0
for p in primes32() do
let diff = p - prevp
if diff > maxdiff then
yield (prevp, diff, p)
maxdiff <- diff
prevp <- p
}
Factor
USING: formatting io kernel math math.primes ;
"2 " write 1 3
[ dup 1050 < ] [
2dup "(%d) %d " printf [ + next-prime ] keep 2dup - nip swap
] while 2drop nl
- Output:
2 (1) 3 (2) 5 (6) 11 (8) 19 (10) 29 (12) 41 (18) 59 (20) 79 (22) 101 (26) 127 (30) 157 (34) 191 (36) 227 (42) 269 (44) 313 (46) 359 (50) 409 (52) 461 (60) 521 (66) 587 (72) 659 (74) 733 (76) 809 (78) 887 (80) 967 (82) 1049
FreeBASIC
#include "isprime.bas"
dim as integer p = 3, i = 2
print "2 3 "; 'special case
do
if isprime( p + i ) then
p += i
print p;" ";
end if
i += 2
loop until p+i >=1050 : print
- Output:
2 3 5 11 19 29 41 59 79 101 127 157 191 227 269 313 359 409 461 521 587 659 733 809 887 967 1049
Go
package main
import "fmt"
func sieve(limit int) []bool {
limit++
// True denotes composite, false denotes prime.
c := make([]bool, limit) // all false by default
c[0] = true
c[1] = true
// no need to bother with even numbers over 2 for this task
p := 3 // Start from 3.
for {
p2 := p * p
if p2 >= limit {
break
}
for i := p2; i < limit; i += 2 * p {
c[i] = true
}
for {
p += 2
if !c[p] {
break
}
}
}
return c
}
func main() {
c := sieve(1049)
fmt.Println("Special primes under 1,050:")
fmt.Println("Prime1 Prime2 Gap")
lastSpecial := 3
lastGap := 1
fmt.Printf("%6d %6d %3d\n", 2, 3, lastGap)
for i := 5; i < 1050; i += 2 {
if !c[i] && (i-lastSpecial) > lastGap {
lastGap = i - lastSpecial
fmt.Printf("%6d %6d %3d\n", lastSpecial, i, lastGap)
lastSpecial = i
}
}
}
- Output:
Special primes under 1,050: Prime1 Prime2 Gap 2 3 1 3 5 2 5 11 6 11 19 8 19 29 10 29 41 12 41 59 18 59 79 20 79 101 22 101 127 26 127 157 30 157 191 34 191 227 36 227 269 42 269 313 44 313 359 46 359 409 50 409 461 52 461 521 60 521 587 66 587 659 72 659 733 74 733 809 76 809 887 78 887 967 80 967 1049 82
J
(, 4 p: (-~ +:)/@(_2&{.))^:(1049 > {:)^:_ (2 3)
2 3 5 11 19 29 41 59 79 101 127 157 191 227 269 313 359 409 461 521 587 659 733 809 887 967 1049
Java
class SpecialPrimes {
private static boolean isPrime(int n) {
if (n < 2) return false;
if (n%2 == 0) return n == 2;
if (n%3 == 0) return n == 3;
int d = 5;
while (d*d <= n) {
if (n%d == 0) return false;
d += 2;
if (n%d == 0) return false;
d += 4;
}
return true;
}
public static void main(String[] args) {
System.out.println("Special primes under 1,050:");
System.out.println("Prime1 Prime2 Gap");
int lastSpecial = 3;
int lastGap = 1;
System.out.printf("%6d %6d %3d\n", 2, 3, lastGap);
for (int i = 5; i < 1050; i += 2) {
if (isPrime(i) && (i-lastSpecial) > lastGap) {
lastGap = i - lastSpecial;
System.out.printf("%6d %6d %3d\n", lastSpecial, i, lastGap);
lastSpecial = i;
}
}
}
}
- Output:
Special primes under 1,050: Prime1 Prime2 Gap 2 3 1 3 5 2 5 11 6 11 19 8 19 29 10 29 41 12 41 59 18 59 79 20 79 101 22 101 127 26 127 157 30 157 191 34 191 227 36 227 269 42 269 313 44 313 359 46 359 409 50 409 461 52 461 521 60 521 587 66 587 659 72 659 733 74 733 809 76 809 887 78 887 967 80 967 1049 82
jq
Works with gojq, the Go implementation of jq
This entry uses `is_primes` as can be defined as in Erdős-primes#jq.
def primes:
2, (range(3;infinite;2) | select(is_prime));
def emit_until(cond; stream): label $out | stream | if cond then break $out else . end;
def special_primes:
foreach primes as $p ({};
.emit = null
| if .p == null then .p = $p | .emit = .p
else ($p - .p) as $g
| if $g > .gap then .p = $p | .gap=$g | .emit = .p
else .
end
end;
select(.emit).emit);
# The task
# The following assumesg invocation with the -n option:
emit_until(. >= 1050; special_primes)
- Output:
Invocation example: jq -n -f program.jq
2 3 5 11 19 29 41 59 79 101 127 157 191 227 269 313 359 409 461 521 587 659 733 809 887 967 1049
Julia
using Primes
let
println("Special primes under 1050:\nPrime1 Prime2 Gap")
println(" 2 3 1")
pmask = primesmask(1, 1050)
n, gap = 3, 2
while n + gap < 1050
if pmask[n + gap]
println(lpad(n, 6), lpad(n + gap, 6), lpad(gap, 4))
n += gap
end
gap += 2
end
end
- Output:
Special primes under 1050: Prime1 Prime2 Gap 2 3 1 3 5 2 5 11 6 11 19 8 19 29 10 29 41 12 41 59 18 59 79 20 79 101 22 101 127 26 127 157 30 157 191 34 191 227 36 227 269 42 269 313 44 313 359 46 359 409 50 409 461 52 461 521 60 521 587 66 587 659 72 659 733 74 733 809 76 809 887 78 887 967 80 967 1049 82
Mathematica /Wolfram Language
max = 1050;
n = {2, 3};
Do[
If[PrimeQ[i],
lastdiff = n[[-1]] - n[[-2]];
If[i - n[[-1]] > lastdiff,
AppendTo[n, i]
]
]
,
{i, Max[n] + 1, max}
]
n
- Output:
{2, 3, 5, 11, 19, 29, 41, 59, 79, 101, 127, 157, 191, 227, 269, 313, 359, 409, 461, 521, 587, 659, 733, 809, 887, 967, 1049}
Nim
import strutils, sugar
func isPrime(n: Positive): bool =
if (n and 1) == 0: return n == 2
var m = 3
while m * m <= n:
if n mod m == 0: return false
inc m, 2
result = true
iterator nextSpecialPrimes(lim: Positive): int =
assert lim >= 3
yield 2
yield 3
var last = 3
var lastGap = 1
for n in countup(5, lim, 2):
if not n.isPrime: continue
if n - last > lastGap:
lastGap = n - last
last = n
yield n
let list = collect(newSeq, for p in nextSpecialPrimes(1050): p)
echo "List of next special primes less than 1050:"
echo list.join(" ")
- Output:
List of next special primes less than 1050: 2 3 5 11 19 29 41 59 79 101 127 157 191 227 269 313 359 409 461 521 587 659 733 809 887 967 1049
Pascal
just showing the small difference to increasing prime gaps.
LastPrime is updated outside or inside If
program NextSpecialprimes;
//increasing prime gaps see
//https://oeis.org/A002386 https://en.wikipedia.org/wiki/Prime_gap
uses
sysutils,
primTrial;
procedure GetIncreasingGaps;
var
Gap,LastPrime,p : NativeUInt;
Begin
InitPrime;
Writeln('next increasing prime gap');
writeln('Prime1':8,'Prime2':8,'Gap':4);
Gap := 0;
LastPrime := actPrime;
repeat
p := NextPrime;
if p-LastPrime > Gap then
Begin
Gap := p-LastPrime;
writeln(LastPrime:8,P:8,Gap:4);
end;
LastPrime := p;
until LastPrime > 1000;
end;
procedure NextSpecial;
var
Gap,LastPrime,p : NativeUInt;
Begin
InitPrime;
Writeln('next special prime');
writeln('Prime1':8,'Prime2':8,'Gap':4);
Gap := 0;
LastPrime := actPrime;
repeat
p := NextPrime;
if p-LastPrime > Gap then
Begin
Gap := p-LastPrime;
writeln(LastPrime:8,P:8,Gap:4);
LastPrime := p;
end;
until LastPrime > 1000;
end;
begin
GetIncreasingGaps;
writeln;
NextSpecial;
end.
- Output:
next increasing prime gap Prime1 Prime2 Gap 2 3 1 3 5 2 7 11 4 23 29 6 89 97 8 113 127 14 523 541 18 887 907 20 next special prime Prime1 Prime2 Gap 2 3 1 3 5 2 5 11 6 11 19 8 19 29 10 29 41 12 41 59 18 59 79 20 79 101 22 101 127 26 127 157 30 157 191 34 191 227 36 227 269 42 269 313 44 313 359 46 359 409 50 409 461 52 461 521 60 521 587 66 587 659 72 659 733 74 733 809 76 809 887 78 887 967 80 967 1049 82
Perl
use strict;
use warnings;
use feature <state say>;
use ntheory 'primes';
my $limit = 1050;
sub is_special {
state $previous = 2;
state $gap = 0;
state @primes = @{primes( 2*$limit )};
shift @primes while $primes[0] <= $previous + $gap;
$gap = $primes[0] - $previous;
$previous = $primes[0];
[$previous, $gap];
}
my @specials = [2, 0];
do { push @specials, is_special() } until $specials[-1][0] >= $limit;
pop @specials;
printf "%4d %4d\n", @$_ for @specials;
- Output:
2 0 3 1 5 2 11 6 19 8 29 10 41 12 59 18 79 20 101 22 127 26 157 30 191 34 227 36 269 42 313 44 359 46 409 50 461 52 521 60 587 66 659 72 733 74 809 76 887 78 967 80 1049 82
Phix
integer lastSpecial = 3, lastGap = 1 printf(1,"Special primes under 1,050:\n") printf(1,"Prime1 Prime2 Gap\n") printf(1,"%6d %6d %3d\n", {2, 3, lastGap}) for i=5 to 1050 by 2 do if is_prime(i) and (i-lastSpecial) > lastGap then lastGap = i - lastSpecial printf(1,"%6d %6d %3d\n", {lastSpecial, i, lastGap}) lastSpecial = i end if end for
- Output:
Special primes under 1,050: Prime1 Prime2 Gap 2 3 1 3 5 2 5 11 6 11 19 8 19 29 10 29 41 12 41 59 18 59 79 20 79 101 22 101 127 26 127 157 30 157 191 34 191 227 36 227 269 42 269 313 44 313 359 46 359 409 50 409 461 52 461 521 60 521 587 66 587 659 72 659 733 74 733 809 76 809 887 78 887 967 80 967 1049 82
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 = 3
i = 2
print("2 3", end = " ");
while True:
if isPrime(p + i) == 1:
p += i
print(p, end = " ");
i += 2
if p + i >= 1050:
break
Quackery
isprime
is defined at Primality by trial division#Quackery.
' [ 2 ] 1
[ over -1 peek +
[ dup isprime
not while
1+ again ]
dup 1050 < while
join
dup -2 split nip
do swap - 1+ again ]
drop
echo
- Output:
[ 2 3 5 11 19 29 41 59 79 101 127 157 191 227 269 313 359 409 461 521 587 659 733 809 887 967 1049 ]
Raku
sub is-special ( ($previous, $gap) ) {
state @primes = grep *.is-prime, 2..*;
shift @primes while @primes[0] <= $previous + $gap;
return ( @primes[0], @primes[0] - $previous );
}
my @specials = (2, 0), &is-special … *;
my $limit = @specials.first: :k, *.[0] > 1050;
say .fmt('%4d') for @specials.head($limit);
- Output:
2 0 3 1 5 2 11 6 19 8 29 10 41 12 59 18 79 20 101 22 127 26 157 30 191 34 227 36 269 42 313 44 359 46 409 50 461 52 521 60 587 66 659 72 733 74 809 76 887 78 967 80 1049 82
REXX
/*REXX program finds next special primes: difference of successive terms is increasing.*/
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 1050 /* " " " " " " */
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
call genP /*build array of semaphores for primes.*/
w= 10 /*width of a number in any column. */
@nsp= ' next special primes < ' commas(hi) ,
" such that the different of successive terms is increasing"
if cols>0 then say ' index │'center(@nsp , 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
op= @.1 /*assign oldPrime to the first prime.*/
nsp= 0; idx= 1 /*initialize number of nsp and index.*/
$= /*a list of nice primes (so far). */
do j=0; np= op + j /*assign newPrime to oldPrime + j */
if np>=hi then leave /*Is newPrimeN ≥ hi? Then leave loop.*/
if \!.np then iterate /*Is np a prime? Then skip this J.*/
nsp= nsp + 1 /*bump the number of nsp's. */
op= np /*set oldPrime to the value of newPrime*/
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 nice prime ──► list, allow big#*/
if nsp//cols\==0 then iterate /*have we populated a line of output? */
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
idx= idx + cols /*bump the index count for the output*/
end /*j*/
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
say
say 'Found ' commas(nsp) @nsp
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: !.= 0 /*placeholders for primes (semaphores).*/
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " flags. */
#=5; sq.#= @.# **2 /*number of primes so far; prime². */
/* [↓] generate more primes ≤ high.*/
do j=@.#+2 by 2 to hi /*find odd primes from here on. */
parse var j '' -1 _; if _==5 then iterate /*J ÷ by 5? (right digit).*/
if j//3==0 then iterate; if j//7==0 then iterate /*" " " 3? J ÷ by 7? */
do k=5 while sq.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; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return
- output when using the default inputs:
index │ next special primes < 1,050 such that the different of successive terms is increasing ───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── 1 │ 2 3 5 11 19 29 41 59 79 101 11 │ 127 157 191 227 269 313 359 409 461 521 21 │ 587 659 733 809 887 967 1,049 Found 27 next special primes < 1,050 such that the different of successive terms is increasing
Ring
load "stdlib.ring"
see "working..." + nl
Primes = []
limit1 = 100
oldPrime = 2
for n = 1 to limit1
nextPrime = oldPrime + n
if isprime(nextPrime)
add(Primes,nextPrime)
oldPrime = nextPrime
ok
next
see "prime1 prime2 Gap" + nl
for n = 1 to Len(Primes)-1
diff = Primes[n+1] - Primes[n]
see ""+ Primes[n] + " " + Primes[n+1] + " " + diff + nl
next
see nl + "done..." + nl
- Output:
working... prime1 prime2 Gap 3 5 2 5 11 6 11 19 8 19 29 10 29 41 12 41 59 18 59 79 20 79 101 22 101 127 26 127 157 30 157 191 34 191 227 36 227 269 42 269 313 44 313 359 46 359 409 50 409 461 52 461 521 60 521 587 66 587 659 72 659 733 74 733 809 76 809 887 78 887 967 80 967 1049 82 done...
RPL
≪ 0 → gap
≪ {2} 2 1 CF
WHILE 1 FC? REPEAT
DUP
DO NEXTPRIME UNTIL DUP2 - gap < END
IF DUP 1050 < THEN
DUP2 - 'gap' STO
NIP SWAP OVER + SWAP
ELSE 1 SF END
END DROP2
≫ ≫ 'TASK' STO
- Output:
1: {2 3 5 11 19 29 41 59 79 101 127 157 191 227 269 313 359 409 461 521 587 659 733 809 887 967 1049}
Sidef
func special_primes(upto) {
var gap = 0
var prev = 2
var list = [[prev, gap]]
loop {
var n = prev+gap
n = n.next_prime
break if (n > upto)
gap = n-prev
list << [n, gap]
prev = n
}
return list
}
special_primes(1050).each_2d {|p,gap|
say "#{'%4s' % p} #{'%4s' % gap}"
}
- Output:
2 0 3 1 5 2 11 6 19 8 29 10 41 12 59 18 79 20 101 22 127 26 157 30 191 34 227 36 269 42 313 44 359 46 409 50 461 52 521 60 587 66 659 72 733 74 809 76 887 78 967 80 1049 82
Wren
import "./math" for Int
import "./fmt" for Fmt
var primes = Int.primeSieve(1049)
System.print("Special primes under 1,050:")
System.print("Prime1 Prime2 Gap")
var lastSpecial = primes[1]
var lastGap = primes[1] - primes[0]
Fmt.print("$6d $6d $3d", primes[0], primes[1], lastGap)
for (p in primes.skip(2)) {
if ((p - lastSpecial) > lastGap) {
lastGap = p - lastSpecial
Fmt.print("$6d $6d $3d", lastSpecial, p, lastGap)
lastSpecial = p
}
}
- Output:
Special primes under 1,050: Prime1 Prime2 Gap 2 3 1 3 5 2 5 11 6 11 19 8 19 29 10 29 41 12 41 59 18 59 79 20 79 101 22 101 127 26 127 157 30 157 191 34 191 227 36 227 269 42 269 313 44 313 359 46 359 409 50 409 461 52 461 521 60 521 587 66 587 659 72 659 733 74 733 809 76 809 887 78 887 967 80 967 1049 82
XPL0
include xpllib; \for IsPrime and Print
int I, LastSpecial, LastGap;
[LastSpecial:= 3; LastGap:= 1;
Print("Special primes under 1,050:\n");
Print("Prime1 Prime2 Gap\n");
Print("%6d %6d %3d\n", 2, 3, LastGap);
for I:= 5 to 1050-1 do
[if IsPrime(I) and I-LastSpecial > LastGap then
[LastGap:= I - LastSpecial;
Print("%6d %6d %3d\n", LastSpecial, I, LastGap);
LastSpecial:= I;
];
I:= I+1;
];
]
- Output:
Special primes under 1,050: Prime1 Prime2 Gap 2 3 1 3 5 2 5 11 6 11 19 8 19 29 10 29 41 12 41 59 18 59 79 20 79 101 22 101 127 26 127 157 30 157 191 34 191 227 36 227 269 42 269 313 44 313 359 46 359 409 50 409 461 52 461 521 60 521 587 66 587 659 72 659 733 74 733 809 76 809 887 78 887 967 80 967 1049 82