Next special primes: Difference between revisions

m
m (→‎{{header|Phix}}: added personal tag)
m (→‎{{header|Wren}}: Minor tidy)
(16 intermediate revisions by 13 users not shown)
Line 6:
where     '''n'''   <   '''1050'''.
<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 W}}==
<langsyntaxhighlight lang="algolw">begin % find some primes where the gap between the current prtime 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 %
Line 47 ⟶ 106:
end while_thisPrime_lt_MAX_NUMBER
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 79 ⟶ 138:
26: 967 1049 82
</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}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f NEXT_SPECIAL_PRIMES.AWK
BEGIN {
Line 112 ⟶ 196:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 144 ⟶ 228:
Next special primes 1-1050: 26
</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}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdbool.h>
 
Line 176 ⟶ 348:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 212 ⟶ 384:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// 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)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 248 ⟶ 420:
 
Here's another way of writing the mP sequence above which is (hopefully) a little clearer:
<langsyntaxhighlight lang="fsharp">
let mP = seq {
let mutable prevp, maxdiff = 2, 0
Line 258 ⟶ 430:
prevp <- p
}
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
{{works with|Factor|0.98}}
<syntaxhighlight lang="text">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</langsyntaxhighlight>
{{out}}
<pre>
Line 274 ⟶ 446:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
 
dim as integer p = 3, i = 2
Line 285 ⟶ 457:
end if
i += 2
loop until p+i >=1050 : print</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 333 ⟶ 505:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 366 ⟶ 538:
967 1049 82
</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}}==
{{trans|C}}
<langsyntaxhighlight lang="java">class SpecialPrimes {
private static boolean isPrime(int n) {
if (n < 2) return false;
Line 398 ⟶ 574:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 431 ⟶ 607:
967 1049 82
</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}}==
<langsyntaxhighlight lang="julia">using Primes
 
let
Line 448 ⟶ 682:
end
end
</langsyntaxhighlight>{{out}}
<pre>
Special primes under 1050:
Line 479 ⟶ 713:
967 1049 82
</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}}==
<langsyntaxhighlight Nimlang="nim">import strutils, sugar
 
func isPrime(n: Positive): bool =
Line 506 ⟶ 757:
let list = collect(newSeq, for p in nextSpecialPrimes(1050): p)
echo "List of next special primes less than 1050:"
echo list.join(" ")</langsyntaxhighlight>
 
{{out}}
Line 516 ⟶ 767:
just showing the small difference to increasing prime gaps.<BR>LastPrime is updated outside or inside If
 
<langsyntaxhighlight lang="pascal">
program NextSpecialprimes;
//increasing prime gaps see
Line 570 ⟶ 821:
writeln;
NextSpecial;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 615 ⟶ 866:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature <state say>;
Line 637 ⟶ 888:
 
pop @specials;
printf "%4d %4d\n", @$_ for @specials;</langsyntaxhighlight>
{{out}}
<pre> 2 0
Line 668 ⟶ 919:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="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: #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 ⟶ 931:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 712 ⟶ 963:
967 1049 82
</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}}==
<syntaxhighlight lang="raku" perl6line>sub is-special ( ($previous, $gap) ) {
state @primes = grep *.is-prime, 2..*;
shift @primes while @primes[0] <= $previous + $gap;
Line 724 ⟶ 1,020:
my $limit = @specials.first: :k, *.[0] > 1050;
 
say .fmt('%4d') for @specials.head($limit);</langsyntaxhighlight>
{{out}}
<pre>
Line 757 ⟶ 1,053:
=={{header|REXX}}==
{{trans|RING}}
<langsyntaxhighlight lang="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 /* " " " " " " */
Line 793 ⟶ 1,089:
@.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; ssq.#= @.# **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 if _==5 then iterate /*J divisible÷ by 5? (right digdigit).*/
if j//3==0 then iterate; if j// 37==0 then iterate /*" " " 3? J ÷ by 7? */
do k=5 while sq.k<=j if j// 7==0 then iterate /*" " " 7? [↓] 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. ___ */
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; ssq.#= 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 817 ⟶ 1,111:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 842 ⟶ 1,136:
see nl + "done..." + nl
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 873 ⟶ 1,167:
967 1049 82
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>
 
Line 878 ⟶ 1,242:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./fmt" for Fmt
 
var primes = Int.primeSieve(1049)
Line 893 ⟶ 1,257:
lastSpecial = p
}
}</langsyntaxhighlight>
 
{{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}}
<pre>
9,482

edits