Strong and weak primes: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added zkl header)
(→‎{{header|zkl}}: added code)
Line 689: Line 689:


=={{header|zkl}}==
=={{header|zkl}}==
Using GMP (GNU Multiple Precision Arithmetic Library, probabilistic
<lang zkl></lang>
primes), because it is easy and fast to generate primes.
<lang zkl></lang>

[[Extensible prime generator#zkl]] could be used instead.
<lang zkl>var [const] BI=Import("zklBigNum"); // libGMP
const N=1e7;

pw,strong,weak := BI(1),List(),List(); // 32,0991 32,1751
ps:=(3).pump(List,'wrap{ pw.nextPrime().toInt() }).copy(); // rolling window
do{
pp,p,pn := ps;
if((z:=(pp.toFloat() + pn)/2)){ // 2,3,5 --> 3.5
if(z>p) weak .append(p);
else if(z<p) strong.append(p);
}
ps.pop(0); ps.append(pw.nextPrime().toInt());
}while(pn<=N);</lang>
<lang zkl>foreach nm,list,psz in (T(T("strong",strong,36), T("weak",weak,37))){
println("First %d %s primes:\n%s".fmt(psz,nm,list[0,psz].concat(" ")));
println("Count of %s primes <= %,10d: %,8d"
.fmt(nm,1e6,list.reduce('wrap(s,p){ s + (p<=1e6) },0)));
println("Count of %s primes <= %,10d: %,8d\n".fmt(nm,1e7,list.len()));
}</lang>
{{out}}
{{out}}
<pre>
<pre>
First 36 strong primes:
11 17 29 37 41 59 67 71 79 97 101 107 127 137 149 163 179 191 197 223 227 239 251 269 277 281 307 311 331 347 367 379 397 419 431 439
Count of strong primes <= 1,000,000: 37,723
Count of strong primes <= 10,000,000: 320,991


First 37 weak primes:
3 7 13 19 23 31 43 47 61 73 83 89 103 109 113 131 139 151 167 181 193 199 229 233 241 271 283 293 313 317 337 349 353 359 383 389 401
Count of weak primes <= 1,000,000: 37,780
Count of weak primes <= 10,000,000: 321,750
</pre>
</pre>