Next special primes: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 9:
{{trans|FreeBASIC}}
<
I a == 2
R 1B
Line 27:
p += i
print(p, end' ‘ ’)
i += 2</
{{out}}
Line 36:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<
PROC Main()
Line 56:
OD
PrintF("%E%EThere are %I next special primes",count)
RETURN</
{{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}}==
<
% 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.</
{{out}}
<pre>
Line 141:
=={{header|Arturo}}==
<
lim: 1050
lastP: 3
Line 156:
print "List of next special primes less than 1050:"
print specials</
{{out}}
Line 164:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f NEXT_SPECIAL_PRIMES.AWK
BEGIN {
Line 196:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 232:
=={{header|BASIC}}==
==={{header|BASIC256}}===
<
if v < 2 then return False
if v mod 2 = 0 then return v = 2
Line 254:
i += 2
until p + i >= 1050
end</
==={{header|PureBasic}}===
<
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
Line 289:
Until p + i >= 1050
Input()
CloseConsole()</
==={{header|Yabasic}}===
<
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</
=={{header|C}}==
<
#include <stdbool.h>
Line 348:
}
}
}</
{{out}}
Line 384:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions 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)
</syntaxhighlight>
{{out}}
<pre>
Line 420:
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
Line 430:
prevp <- p
}
</syntaxhighlight>
=={{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</
{{out}}
<pre>
Line 446:
=={{header|FreeBASIC}}==
<
dim as integer p = 3, i = 2
Line 457:
end if
i += 2
loop until p+i >=1050 : print</
{{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}}==
<
import "fmt"
Line 505:
}
}
}</
{{out}}
Line 541:
=={{header|Java}}==
{{trans|C}}
<
private static boolean isPrime(int n) {
if (n < 2) return false;
Line 570:
}
}
}</
{{out}}
Line 609:
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));
Line 628:
# The task
# The following assumesg invocation with the -n option:
emit_until(. >= 1050; special_primes)</
{{out}}
Invocation example: jq -n -f program.jq
Line 663:
=={{header|Julia}}==
<
let
Line 678:
end
end
</
<pre>
Special primes under 1050:
Line 711:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
n = {2, 3};
Do[
Line 723:
{i, Max[n] + 1, max}
]
n</
{{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}}==
<
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(" ")</
{{out}}
Line 763:
just showing the small difference to increasing prime gaps.<BR>LastPrime is updated outside or inside If
<
program NextSpecialprimes;
//increasing prime gaps see
Line 817:
writeln;
NextSpecial;
end.</
{{out}}
<pre>
Line 862:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use feature <state say>;
Line 884:
pop @specials;
printf "%4d %4d\n", @$_ for @specials;</
{{out}}
<pre> 2 0
Line 915:
=={{header|Phix}}==
<!--<
<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>
<!--</
{{out}}
<pre>
Line 962:
=={{header|Python}}==
<
def isPrime(n):
Line 982:
i += 2
if p + i >= 1050:
break</
=={{header|Raku}}==
<syntaxhighlight lang="raku"
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);</
{{out}}
<pre>
Line 1,029:
=={{header|REXX}}==
{{trans|RING}}
<
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</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,087:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 1,112:
see nl + "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,146:
=={{header|Sidef}}==
<
var gap = 0
Line 1,166:
special_primes(1050).each_2d {|p,gap|
say "#{'%4s' % p} #{'%4s' % gap}"
}</
{{out}}
<pre>
Line 1,201:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 1,216:
lastSpecial = p
}
}</
{{out}}
|