Next special primes: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (→{{header|Wren}}: Minor tidy) |
|||
(6 intermediate revisions by 5 users not shown) | |||
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 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}}
<
private static boolean isPrime(int n) {
if (n < 2) return false;
Line 570 ⟶ 574:
}
}
}</
{{out}}
Line 609 ⟶ 613:
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 ⟶ 632:
# 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 ⟶ 667:
=={{header|Julia}}==
<
let
Line 678 ⟶ 682:
end
end
</
<pre>
Special primes under 1050:
Line 711 ⟶ 715:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
n = {2, 3};
Do[
Line 723 ⟶ 727:
{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 ⟶ 757:
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 ⟶ 767:
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 ⟶ 821:
writeln;
NextSpecial;
end.</
{{out}}
<pre>
Line 862 ⟶ 866:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use feature <state say>;
Line 884 ⟶ 888:
pop @specials;
printf "%4d %4d\n", @$_ for @specials;</
{{out}}
<pre> 2 0
Line 915 ⟶ 919:
=={{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 ⟶ 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>
<!--</
{{out}}
<pre>
Line 962 ⟶ 966:
=={{header|Python}}==
<
def isPrime(n):
Line 982 ⟶ 986:
i += 2
if p + i >= 1050:
break</
=={{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"
state @primes = grep *.is-prime, 2..*;
shift @primes while @primes[0] <= $previous + $gap;
Line 996 ⟶ 1,020:
my $limit = @specials.first: :k, *.[0] > 1050;
say .fmt('%4d') for @specials.head($limit);</
{{out}}
<pre>
Line 1,029 ⟶ 1,053:
=={{header|REXX}}==
{{trans|RING}}
<
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 1050 /* " " " " " " */
Line 1,074 ⟶ 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</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,087 ⟶ 1,111:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 1,112 ⟶ 1,136:
see nl + "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,143 ⟶ 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}}==
<
var gap = 0
Line 1,166 ⟶ 1,207:
special_primes(1050).each_2d {|p,gap|
say "#{'%4s' % p} #{'%4s' % gap}"
}</
{{out}}
<pre>
Line 1,201 ⟶ 1,242:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<
import "./fmt" for Fmt
var primes = Int.primeSieve(1049)
Line 1,216 ⟶ 1,257:
lastSpecial = p
}
}</
{{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>
|