Strong and weak primes: Difference between revisions

m
(Added XPL0 example.)
 
(6 intermediate revisions by 6 users not shown)
Line 36:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">F primes_upto(limit)
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
Line 64:
print(‘The count of balanced primes below 10,000,000: ’b.len)
print("\nTOTAL primes below 1,000,000: "sum(p.filter(pr -> pr < 1'000'000).map(pr -> 1)))
print(‘TOTAL primes below 10,000,000: ’p.len)</langsyntaxhighlight>
 
{{out}}
Line 87:
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
<langsyntaxhighlight lang="algol68"># find and count strong and weak primes #
PR heap=128M PR # set heap memory size for Algol 68G #
# returns a string representation of n with commas #
Line 178:
print( ( newline ) );
print( ( " weak primes below 1,000,000: ", commatise( weak1 ), newline ) );
print( ( " weak primes below 10,000,000: ", commatise( weak10 ), newline ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 191:
</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f STRONG_AND_WEAK_PRIMES.AWK
BEGIN {
Line 256:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 269:
=={{header|C}}==
{{trans|D}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
Line 390:
free(primePtr);
return EXIT_SUCCESS;
}</langsyntaxhighlight>
{{out}}
<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
Line 402:
=={{header|C sharp}}==
{{works with|C sharp|7}}
<langsyntaxhighlight lang="csharp">using static System.Console;
using static System.Linq.Enumerable;
using System;
Line 420:
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 431:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <iterator>
Line 493:
weak_primes.print(std::cout, "weak");
return 0;
}</langsyntaxhighlight>
 
Contents of prime_sieve.hpp:
<langsyntaxhighlight lang="cpp">#ifndef PRIME_SIEVE_HPP
#define PRIME_SIEVE_HPP
 
Line 547:
}
 
#endif</langsyntaxhighlight>
 
{{out}}
Line 560:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.algorithm;
import std.array;
import std.range;
Line 653:
writefln("There are %d weak primes below 1,000,000", weakPrimes.filter!"a<1_000_000".count);
writefln("There are %d weak primes below 10,000,000", weakPrimes.filter!"a<10_000_000".count);
}</langsyntaxhighlight>
{{out}}
<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]
Line 661:
There are 37780 weak primes below 1,000,000
There are 321750 weak primes below 10,000,000</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
procedure StrongWeakPrimes(Memo: TMemo);
{Display Strong/Weak prime information}
var I,P: integer;
var Sieve: TPrimeSieve;
var S: string;
var Cnt,Cnt1,Cnt2: integer;
 
type TPrimeTypes = (ptStrong,ptWeak,ptBalanced);
 
 
function GetTypeStr(PrimeType: TPrimeTypes): string;
{Get string describing PrimeType}
begin
case PrimeType of
ptStrong: Result:='Strong';
ptWeak: Result:='Weak';
ptBalanced: Result:='Balanced';
end;
end;
 
function GetPrimeType(N: integer): TPrimeTypes;
{Return flag indicating type of prime Primes[N] is}
{Strong = Primes(N) > [Primes(N-1) + Primes(N+1)] / 2}
{Weak = Primes(N) < [Primes(N-1) + Primes(N+1)] / 2}
{Balanced = Primes(N) = [Primes(N-1) + Primes(N+1)] / 2}
var P,P1: double;
begin
P:=Sieve.Primes[N];
P1:=(Sieve.Primes[N-1] + Sieve.Primes[N+1]) / 2;
if P>P1 then Result:=ptStrong
else if P<P1 then Result:=ptWeak
else Result:=ptBalanced;
end;
 
procedure GetPrimeCounts(PT: TPrimeTypes; var Cnt1,Cnt2: integer);
{Get number of primes of type "PT" below 1 million and 10 million}
var I: integer;
begin
Cnt1:=0; Cnt2:=0;
for I:=1 to 1000000-1 do
begin
if GetPrimeType(I)=PT then
begin
if Sieve.Primes[I]>10000000 then break;
Inc(Cnt2);
if Sieve.Primes[I]<1000000 then Inc(Cnt1);
end;
end;
end;
 
 
function GetPrimeList(PT: TPrimeTypes; Limit: integer): string;
{Get a list of primes of type PT up to Limit}
var I,Cnt: integer;
begin
Result:='';
Cnt:=0;
for I:=1 to Sieve.PrimeCount-1 do
if GetPrimeType(I)=PT then
begin
Inc(Cnt);
P:=Sieve.Primes[I];
Result:=Result+Format('%5d',[P]);
if Cnt>=Limit then break;
if (Cnt mod 10)=0 then Result:=Result+CRLF;
end;
end;
 
 
 
procedure ShowPrimeTypeData(PT: TPrimeTypes; Limit: Integer);
{Display information about specified PrimeType, listing items up to Limit}
var S,TS: string;
begin
S:=GetPrimeList(PT,Limit);
TS:=GetTypeStr(PT);
Memo.Lines.Add(Format('First %d %s primes are:',[Limit,TS]));
Memo.Lines.Add(S);
 
GetPrimeCounts(PT,Cnt1,Cnt2);
Memo.Lines.Add(Format('Number %s primes <1,000,000: %8.0n', [TS,Cnt1+0.0]));
Memo.Lines.Add(Format('Number %s primes <10,000,000: %8.0n', [TS,Cnt2+0.0]));
Memo.Lines.Add('');
end;
 
 
begin
Sieve:=TPrimeSieve.Create;
try
Sieve.Intialize(200000000);
Memo.Lines.Add('Primes in Sieve : '+IntToStr(Sieve.PrimeCount));
ShowPrimeTypeData(ptStrong,36);
ShowPrimeTypeData(ptWeak,37);
ShowPrimeTypeData(ptBalanced,28);
finally Sieve.Free; end;
end;
 
</syntaxhighlight>
{{out}}
<pre>
Primes in Sieve : 11078937
First 36 Strong primes are:
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
Number Strong primes <1,000,000: 37,723
Number Strong primes <10,000,000: 320,991
 
First 37 Weak primes are:
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
Number Weak primes <1,000,000: 37,780
Number Weak primes <10,000,000: 321,750
 
First 28 Balanced primes are:
5 53 157 173 211 257 263 373 563 593
607 653 733 947 977 1103 1123 1187 1223 1367
1511 1747 1753 1907 2287 2417 2677 2903
Number Balanced primes <1,000,000: 2,994
Number Balanced primes <10,000,000: 21,837
 
Elapsed Time: 2.947 Sec.
 
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
i = 3
while i <= sqrt num
if num mod i = 0
return 0
.
i += 2
.
return 1
.
func nextprim n .
repeat
n += 2
until isprim n = 1
.
return n
.
proc strwkprimes ncnt sgn . .
write "First " & ncnt & ": "
pr2 = 2
pr3 = 3
repeat
pr1 = pr2
pr2 = pr3
until pr2 >= 10000000
pr3 = nextprim pr3
if pr1 < 1000000 and pr2 > 1000000
print ""
print "Count lower 10e6: " & cnt
.
if sgn * pr2 > sgn * (pr1 + pr3) / 2
cnt += 1
if cnt <= ncnt
write pr2 & " "
.
.
.
print "Count lower 10e7: " & cnt
print ""
.
print "Strong primes:"
strwkprimes 36 1
print "Weak primes:"
strwkprimes 37 -1
</syntaxhighlight>
 
{{out}}
<pre>
Strong primes:
First 36: 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 lower 10e6: 37723
Count lower 10e7: 320991
 
Weak primes:
First 37: 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 lower 10e6: 37780
Count lower 10e7: 321750
</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting grouping kernel math math.primes sequences
tools.memory.private ;
IN: rosetta-code.strong-primes
Line 687 ⟶ 883:
First 37 weak primes:\n%[%d, %]
%s weak primes below 1,000,000
%s weak primes below 10,000,000\n" printf</langsyntaxhighlight>
{{out}}
<pre>
Line 702 ⟶ 898:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
#include "isprime.bas"
 
Line 762 ⟶ 958:
wend
print "There are ";c;" weak primes below ten million"
print</langsyntaxhighlight>
{{out}}<pre>
The first 36 strong primes are:
Line 776 ⟶ 972:
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">
strongPrimes[end=undef] := select[primes[3,end], {|p| p > (previousPrime[p] + nextPrime[p])/2 }]
weakPrimes[end=undef] := select[primes[3,end], {|p| p < (previousPrime[p] + nextPrime[p])/2 }]
Line 787 ⟶ 983:
println["Weak primes below 1,000,000: " + length[weakPrimes[1_000_000]]]
println["Weak primes below 10,000,000: " + length[weakPrimes[10_000_000]]]
</syntaxhighlight>
</lang>
 
{{out}}
Line 800 ⟶ 996:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 885 ⟶ 1,081:
fmt.Println("\nThe number of weak primes below 1,000,000 is", commatize(count))
fmt.Println("\nThe number of weak primes below 10,000,000 is", commatize(len(weak)))
}</langsyntaxhighlight>
 
{{out}}
Line 905 ⟶ 1,101:
=={{header|Haskell}}==
Uses primes library: http://hackage.haskell.org/package/primes-0.2.1.0/docs/Data-Numbers-Primes.html
<langsyntaxhighlight lang="haskell">import Text.Printf (printf)
import Data.Numbers.Primes (primes)
 
Line 923 ⟶ 1,119:
printf "Weak primes below 10,000,000: %d\n\n" . length . takeWhile (<10000000) $ weakPrimes
where strongPrimes = xPrimes (>) primes
weakPrimes = xPrimes (<) primes</langsyntaxhighlight>
{{out}}
<pre>
Line 966 ⟶ 1,162:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
public class StrongAndWeakPrimes {
 
Line 1,071 ⟶ 1,267:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,082 ⟶ 1,278:
Number of weak primes below 1,000,000 = 37,780
Number of weak primes below 10,000,000 = 321,750
</pre>
 
=={{header|jq}}==
{{Works with|jq}}
'''Also works with gojq, the Go implementation of jq'''
 
The following assumes that `primes` generates a stream of primes less
than or equal to `.`
as defined, for example, at [[Sieve_of_Eratosthenes#jq|Sieve of Eratosthenes]]]].
 
<syntaxhighlight lang=jq>
def count(s): reduce s as $_ (0; .+1);
 
# Emit {strong, weak} primes up to and including $n
def strong_weak_primes:
. as $n
| primes as $primes
| ("\nCheck: last prime generated: \($primes[-1])" | debug) as $debug
| reduce range(1; $primes|length-1) as $p ({};
(($primes[$p-1] + $primes[$p+1]) / 2) as $x
| if $primes[$p] > $x
then .strong += [$primes[$p]]
elif $primes[$p] < $x
then .weak += [$primes[$p]]
else .
end );
 
(1e7 + 19)
| strong_weak_primes as {$strong, $weak}
| "The first 36 strong primes are:",
$strong[:36],
"\nThe count of the strong primes below 1e6: \(count($strong[]|select(. < 1e6 )))",
"\nThe count of the strong primes below 1e7: \(count($strong[]|select(. < 1e7 )))",
 
"\nThe first 37 weak primes are:",
$weak[:37],
"\nThe count of the weak primes below 1e6: \(count($weak[]|select(. < 1e6 )))",
"\nThe count of the weak primes below 1e7: \(count($weak[]|select(. < 1e7 )))"
</syntaxhighlight>
{{output}}
<pre>
The first 36 strong primes are:
[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]
 
The count of the strong primes below 1e6: 37723
 
The count of the strong primes below 1e7: 320991
 
The first 37 weak primes are:
[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]
 
The count of the weak primes below 1e6: 37780
 
The count of the weak primes below 1e7: 321750
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes, Formatting
 
function parseprimelist()
Line 1,117 ⟶ 1,367:
 
parseprimelist()
</langsyntaxhighlight> {{output}} <pre>
The first 36 strong primes are: [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]
There are 37,723 stromg primes less than 1 million.
Line 1,131 ⟶ 1,381:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">private const val MAX = 10000000 + 1000
private val primes = BooleanArray(MAX)
 
Line 1,233 ⟶ 1,483:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>First 36 strong primes:
Line 1,245 ⟶ 1,495:
 
=={{header|Ksh}}==
<langsyntaxhighlight lang="ksh">
#!/bin/ksh
 
Line 1,334 ⟶ 1,584:
printf "Number of Weak Primes under %d is: %d\n" $GOAL1 ${goal1_weak}
printf "Number of Weak Primes under %d is: %d\n\n\n" $MAX_INT ${wpcnt}
printf "Number of Balanced Primes under %d is: %d\n\n\n" $MAX_INT ${bpcnt}</langsyntaxhighlight>
{{out}}<pre>
Total primes under 10000000 = 664579
Line 1,353 ⟶ 1,603:
=={{header|Lua}}==
This could be made faster but favours readability. It runs in about 3.3 seconds in LuaJIT on a 2.8 GHz core.
<langsyntaxhighlight lang="lua">-- Return a table of the primes up to n, then one more
function primeList (n)
local function isPrime (x)
Line 1,405 ⟶ 1,655:
for i = 1, 37 do io.write(weak[i] .. " ") end
print("\n\nThere are " .. wCount .. " weak primes below one million.")
print("\nThere are " .. #weak .. " weak primes below ten million.")</langsyntaxhighlight>
{{out}}
<pre>The first 36 strong primes are:
Line 1,423 ⟶ 1,673:
 
=={{header|Maple}}==
<langsyntaxhighlight lang="maple">isStrong := proc(n::posint) local holder;
holder := false;
if isprime(n) and 1/2*prevprime(n) + 1/2*nextprime(n) < n then
Line 1,466 ⟶ 1,716:
countStrong(10000000)
countWeak(1000000)
countWeak(10000000)</langsyntaxhighlight>
{{Out}}
<pre>[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]
Line 1,476 ⟶ 1,726:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">p = Prime[Range[PrimePi[10^3]]];
SequenceCases[p, ({a_, b_, c_}) /; (a + c < 2 b) :> b, 36, Overlaps -> True]
SequenceCases[p, ({a_, b_, c_}) /; (a + c > 2 b) :> b, 37, Overlaps -> True]
Line 1,484 ⟶ 1,734:
p = Prime[Range[PrimePi[10^7] + 1]];
Length[Select[Partition[p, 3, 1], #[[3]] + #[[1]] < 2 #[[2]] &]]
Length[Select[Partition[p, 3, 1], #[[3]] + #[[1]] > 2 #[[2]] &]]</langsyntaxhighlight>
{{out}}
<pre>{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}
Line 1,495 ⟶ 1,745:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import math, strutils
 
const
Line 1,542 ⟶ 1,792:
echo " ", weakPrimes[0..36].join(" ")
echo "Count of weak primes below 1_000_000: ", weakPrimes.count(1_000_000)
echo "Count of weak primes below 10_000_000: ", weakPrimes.count(10_000_000)</langsyntaxhighlight>
 
{{out}}
Line 1,561 ⟶ 1,811:
If deltaAfter < deltaBefore than a strong prime is found.
<langsyntaxhighlight lang="pascal">program WeakPrim;
{$IFNDEF FPC}
{$AppType CONSOLE}
Line 1,729 ⟶ 1,979:
WeakOut(37);
CntWeakStrong10(CntWs);
end.</langsyntaxhighlight>
{{Out}}
<pre>
Line 1,752 ⟶ 2,002:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use ntheory qw(primes vecfirst);
 
sub comma {
Line 1,779 ⟶ 2,029:
print "Count of $type primes <= @{[comma $c1]}: " . comma below($c1,@$pr) . "\n";
print "Count of $type primes <= @{[comma $c2]}: " . comma scalar @$pr . "\n";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,799 ⟶ 2,049:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
Using [[Extensible_prime_generator#Phix]]
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<lang Phix>while sieved<10_000_000 do add_block() end while
<span style="color: #004080;">sequence</span> <span style="color: #000000;">strong</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000000;">weak</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
sequence {strong, weak} @= {}
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">get_maxprime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1e14</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- (ie idx of primes &lt; (sqrt(1e14)==1e7), bar 1st)</span>
for i=2 to abs(binary_search(10_000_000,primes))-1 do
<span style="color: #004080;">integer</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">),</span>
integer p = primes[i],
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">compare</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,(</span><span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)+</span><span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
c = compare(p,(primes[i-1]+primes[i+1])/2)
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=+</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #000000;">strong</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">p</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if c=+1 then strong &= p
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #000000;">weak</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">p</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
elsif c=-1 then weak &= p
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<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;">"The first thirty six strong primes: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">strong</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">36</span><span style="color: #0000FF;">],</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d"</span><span style="color: #0000FF;">),</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span>
end for
<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;">"The first thirty seven weak primes: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">weak</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">37</span><span style="color: #0000FF;">],</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d"</span><span style="color: #0000FF;">),</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span>
printf(1,"The first 36 strong primes:") ?strong[1..36]
<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;">"There are %,d strong primes below %,d and %,d below %,d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">binary_search</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1e6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">strong</span><span style="color: #0000FF;">))-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1e6</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">strong</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1e7</span><span style="color: #0000FF;">})</span>
printf(1,"The first 37 weak primes:") ?weak[1..37]
<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;">"There are %,d weak primes below %,d and %,d below %,d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">binary_search</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1e6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">weak</span><span style="color: #0000FF;">))-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1e6</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">weak</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1e7</span><span style="color: #0000FF;">})</span>
printf(1,"%,7d strong primes below 1,000,000\n",abs(binary_search(1_000_000,strong))-1)
<!--</syntaxhighlight>-->
printf(1,"%,7d strong primes below 10,000,000\n",length(strong))
printf(1,"%,7d weak primes below 1,000,000\n",abs(binary_search(1_000_000,weak))-1)
printf(1,"%,7d weak primes below 10,000,000\n",length(weak))</lang>
{{out}}
<pre>
<pre style="font-size: 11px">
The first 36thirty six 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}
The first 37thirty seven 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}
There are 37,723 strong primes below 1,000,000 and 320,991 below 10,000,000
320There are 37,991780 strong weak primes below 1,000,000 and 321,750 below 10,000,000
37,780 weak primes below 1,000,000
321,750 weak primes below 10,000,000
</pre>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">#MAX=10000000+20
Global Dim P.b(#MAX) : FillMemory(@P(),#MAX,1,#PB_Byte)
Global NewList Primes.i()
Line 1,863 ⟶ 2,109:
PrintN("Number of weak primes below 1'000'000 = "+FormatNumber(c2,0,".","'"))
PrintN("Number of weak primes below 10'000'000 = "+FormatNumber(ListSize(Weak()),0,".","'"))
Input()</langsyntaxhighlight>
{{out}}
<pre>First 36 strong primes:
Line 1,879 ⟶ 2,125:
 
COmputes and shows the requested output then adds similar output for the "balanced" case where <code>prime(p) == [prime(p-1) + prime(p+1)] ÷ 2</code>.
<langsyntaxhighlight lang="python">import numpy as np
 
def primesfrom2to(n):
Line 1,915 ⟶ 2,161:
print('\nTOTAL primes below 1,000,000:',
sum(1 for pr in p if pr < 1_000_000))
print('TOTAL primes below 10,000,000:', len(p))</langsyntaxhighlight>
 
{{out}}
Line 1,938 ⟶ 2,184:
{{works with|Rakudo|2018.11}}
 
<syntaxhighlight lang="raku" perl6line>sub comma { $^i.flip.comb(3).join(',').flip }
 
use Math::Primesieve;
Line 1,963 ⟶ 2,209:
say "Count of $type primes <= {comma 1e6}: ", comma +@pr[^(@pr.first: * > 1e6,:k)];
say "Count of $type primes <= {comma 1e7}: ", comma +@pr;
}</langsyntaxhighlight>
{{out}}
<pre>First 36 strong primes:
Line 1,981 ⟶ 2,227:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program lists a sequence (or a count) of ──strong── or ──weak── primes. */
parse arg N kind _ . 1 . okind; upper kind /*obtain optional arguments from the CL*/
if N=='' | N=="," then N= 36 /*Not specified? Then assume default.*/
Line 2,030 ⟶ 2,276:
else return 0 /*not " " " */
else if y>s then return add() /*is an strong prime.*/
return 0 /*not " " " */</langsyntaxhighlight>
This REXX program makes use of &nbsp; '''LINESIZE''' &nbsp; REXX program (or
BIF) which is used to determine the screen width (or linesize) of the
Line 2,069 ⟶ 2,315:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 2,174 ⟶ 2,420:
see "weak primes below 1,000,000: " + primes2 + nl
see "weak primes below 10,000,000: " + primes3 + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,188 ⟶ 2,434:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'prime'
 
strong_gen = Enumerator.new{|y| Prime.each_cons(3){|a,b,c|y << b if a+c-b<b} }
Line 2,207 ⟶ 2,453:
puts "#{strongs} strong primes and #{weaks} weak primes below #{limit}."
end
</syntaxhighlight>
</lang>
{{out}}
<pre>First 36 strong primes:
Line 2,220 ⟶ 2,466:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn is_prime(n: i32) -> bool {
for i in 2..n {
if i * i > n {
Line 2,315 ⟶ 2,561:
}
println!("weak primes below 10,000,000: {}", n);
}</langsyntaxhighlight>
<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
Line 2,327 ⟶ 2,573:
=={{header|Scala}}==
This example works entirely with lazily evaluated lists. It starts with a list of primes, and generates a sliding iterator that looks at each triplet of primes. Lists of strong and weak primes are built by applying the given filters then selecting the middle term from each triplet.
<langsyntaxhighlight lang="scala">object StrongWeakPrimes {
def main(args: Array[String]): Unit = {
val bnd = 1000000
Line 2,344 ⟶ 2,590:
def primeTrips: Iterator[LazyList[Int]] = primes.sliding(3)
def primes: LazyList[Int] = 2 #:: LazyList.from(3, 2).filter(n => !Iterator.range(3, math.sqrt(n).toInt + 1, 2).exists(n%_ == 0))
}</langsyntaxhighlight>
 
{{out}}
Line 2,357 ⟶ 2,603:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">var primes = 10_000_019.primes
 
var (*strong, *weak, *balanced)
Line 2,379 ⟶ 2,625:
say ("Count of #{type} primes <= #{c1.commify}: ", pr.first_index { _ > 1e6 }.commify)
say ("Count of #{type} primes <= #{c2.commify}: " , pr.len.commify)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,399 ⟶ 2,645:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
class PrimeSieve {
Line 2,490 ⟶ 2,736:
 
strongPrimes.printInfo(name: "strong")
weakPrimes.printInfo(name: "weak")</langsyntaxhighlight>
 
{{out}}
Line 2,505 ⟶ 2,751:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./fmt" for Fmt
 
var primes = Int.primeSieve(1e7 + 19) // next prime above 10 million
Line 2,527 ⟶ 2,773:
Fmt.print("$d", weak.take(37))
Fmt.print("\nThe count of the weak primes below $,d is $,d.", 1e6, weak.count{ |n| n < 1e6 })
Fmt.print("\nThe count of the weak primes below $,d is $,d.", 1e7, weak.count{ |n| n < 1e7 })</langsyntaxhighlight>
 
{{out}}
Line 2,547 ⟶ 2,793:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">proc NumOut(Num); \Output positive integer with commas
int Num, Dig, Cnt;
[Cnt:= [0];
Line 2,605 ⟶ 2,851:
NumOut(WeakCnt);
CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
Line 2,624 ⟶ 2,870:
 
[[Extensible prime generator#zkl]] could be used instead.
<langsyntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
const N=1e7;
 
Line 2,636 ⟶ 2,882:
}
ps.pop(0); ps.append(pw.nextPrime().toInt());
}while(pn<=N);</langsyntaxhighlight>
<langsyntaxhighlight 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()));
}</langsyntaxhighlight>
{{out}}
<pre>
2,056

edits