Next special primes: Difference between revisions

m
(Next special primes in various BASIC dialents)
m (→‎{{header|Wren}}: Minor tidy)
(8 intermediate revisions by 7 users not shown)
Line 9:
{{trans|FreeBASIC}}
 
<langsyntaxhighlight lang="11l">F is_prime(a)
I a == 2
R 1B
Line 27:
p += i
print(p, end' ‘ ’)
i += 2</langsyntaxhighlight>
 
{{out}}
Line 36:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:SIEVE.ACT"
 
PROC Main()
Line 56:
OD
PrintF("%E%EThere are %I next special primes",count)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Next_special_primes.png Screenshot from Atari 8-bit computer]
Line 66:
 
=={{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 106:
end while_thisPrime_lt_MAX_NUMBER
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 141:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">specials: new [2 3]
lim: 1050
lastP: 3
Line 156:
 
print "List of next special primes less than 1050:"
print specials</langsyntaxhighlight>
 
{{out}}
Line 164:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f NEXT_SPECIAL_PRIMES.AWK
BEGIN {
Line 196:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 232:
=={{header|BASIC}}==
==={{header|BASIC256}}===
<langsyntaxhighlight BASIC256lang="basic256">function isPrime(v)
if v < 2 then return False
if v mod 2 = 0 then return v = 2
Line 254:
i += 2
until p + i >= 1050
end</langsyntaxhighlight>
 
==={{header|PureBasic}}===
<langsyntaxhighlight PureBasiclang="purebasic">Procedure isPrime(v.i)
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
Line 289:
Until p + i >= 1050
Input()
CloseConsole()</langsyntaxhighlight>
 
==={{header|Yabasic}}===
<langsyntaxhighlight lang="yabasic">sub isPrime(v)
if v < 2 then return False : fi
if mod(v, 2) = 0 then return v = 2 : fi
Line 314:
i = i + 2
until p + i >= 1050
end</langsyntaxhighlight>
 
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdbool.h>
 
Line 348:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 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 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 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 446:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
 
dim as integer p = 3, i = 2
Line 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 505:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 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 570 ⟶ 574:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 609 ⟶ 613:
 
This entry uses `is_primes` as can be defined as in [[Erd%C5%91s-primes#jq]].
<syntaxhighlight lang="jq">
<lang jq>
def primes:
2, (range(3;infinite;2) | select(is_prime));
Line 628 ⟶ 632:
# The task
# The following assumesg invocation with the -n option:
emit_until(. >= 1050; special_primes)</langsyntaxhighlight>
{{out}}
Invocation example: jq -n -f program.jq
Line 663 ⟶ 667:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
let
Line 678 ⟶ 682:
end
end
</langsyntaxhighlight>{{out}}
<pre>
Special primes under 1050:
Line 709 ⟶ 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 736 ⟶ 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 746 ⟶ 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 800 ⟶ 821:
writeln;
NextSpecial;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 845 ⟶ 866:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature <state say>;
Line 867 ⟶ 888:
 
pop @specials;
printf "%4d %4d\n", @$_ for @specials;</langsyntaxhighlight>
{{out}}
<pre> 2 0
Line 898 ⟶ 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 910 ⟶ 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 942 ⟶ 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 954 ⟶ 1,020:
my $limit = @specials.first: :k, *.[0] > 1050;
 
say .fmt('%4d') for @specials.head($limit);</langsyntaxhighlight>
{{out}}
<pre>
Line 987 ⟶ 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 1,032 ⟶ 1,098:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; sq.#= 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 1,045 ⟶ 1,111:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,070 ⟶ 1,136:
see nl + "done..." + nl
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,101 ⟶ 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}}==
<langsyntaxhighlight lang="ruby">func special_primes(upto) {
 
var gap = 0
Line 1,124 ⟶ 1,207:
special_primes(1050).each_2d {|p,gap|
say "#{'%4s' % p} #{'%4s' % gap}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,159 ⟶ 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 1,174 ⟶ 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,476

edits