Next special primes: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
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 541:
=={{header|Java}}==
{{trans|C}}
<langsyntaxhighlight lang="java">class SpecialPrimes {
private static boolean isPrime(int n) {
if (n < 2) return false;
Line 570:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 609:
 
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:
# 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:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
let
Line 678:
end
end
</langsyntaxhighlight>{{out}}
<pre>
Special primes under 1050:
Line 711:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">max = 1050;
n = {2, 3};
Do[
Line 723:
{i, Max[n] + 1, max}
]
n</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|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils, sugar
 
func isPrime(n: Positive): bool =
Line 753:
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 763:
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 817:
writeln;
NextSpecial;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 862:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature <state say>;
Line 884:
 
pop @specials;
printf "%4d %4d\n", @$_ for @specials;</langsyntaxhighlight>
{{out}}
<pre> 2 0
Line 915:
 
=={{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 927:
<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 962:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">#!/usr/bin/python
 
def isPrime(n):
Line 982:
i += 2
if p + i >= 1050:
break</langsyntaxhighlight>
 
 
=={{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 996:
my $limit = @specials.first: :k, *.[0] > 1050;
 
say .fmt('%4d') for @specials.head($limit);</langsyntaxhighlight>
{{out}}
<pre>
Line 1,029:
=={{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,074:
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,087:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,112:
see nl + "done..." + nl
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,146:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func special_primes(upto) {
 
var gap = 0
Line 1,166:
special_primes(1050).each_2d {|p,gap|
say "#{'%4s' % p} #{'%4s' % gap}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,201:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/fmt" for Fmt
 
Line 1,216:
lastSpecial = p
}
}</langsyntaxhighlight>
 
{{out}}
10,327

edits