Strong and weak primes: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: syntax coloured, removed deprecated @=, made better use of builtins, plus some other minor tidying)
m (syntax highlighting fixup automation)
Line 36: Line 36:


=={{header|11l}}==
=={{header|11l}}==
<lang 11l>F primes_upto(limit)
<syntaxhighlight lang="11l">F primes_upto(limit)
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
Line 64: Line 64:
print(‘The count of balanced primes below 10,000,000: ’b.len)
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("\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)</lang>
print(‘TOTAL primes below 10,000,000: ’p.len)</syntaxhighlight>


{{out}}
{{out}}
Line 87: Line 87:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
<lang algol68># find and count strong and weak primes #
<syntaxhighlight lang="algol68"># find and count strong and weak primes #
PR heap=128M PR # set heap memory size for Algol 68G #
PR heap=128M PR # set heap memory size for Algol 68G #
# returns a string representation of n with commas #
# returns a string representation of n with commas #
Line 178: Line 178:
print( ( newline ) );
print( ( newline ) );
print( ( " weak primes below 1,000,000: ", commatise( weak1 ), newline ) );
print( ( " weak primes below 1,000,000: ", commatise( weak1 ), newline ) );
print( ( " weak primes below 10,000,000: ", commatise( weak10 ), newline ) )</lang>
print( ( " weak primes below 10,000,000: ", commatise( weak10 ), newline ) )</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 191: Line 191:
</pre>
</pre>
=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f STRONG_AND_WEAK_PRIMES.AWK
# syntax: GAWK -f STRONG_AND_WEAK_PRIMES.AWK
BEGIN {
BEGIN {
Line 256: Line 256:
return(1)
return(1)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 269: Line 269:
=={{header|C}}==
=={{header|C}}==
{{trans|D}}
{{trans|D}}
<lang c>#include <stdbool.h>
<syntaxhighlight lang="c">#include <stdbool.h>
#include <stdint.h>
#include <stdint.h>
#include <stdio.h>
#include <stdio.h>
Line 390: Line 390:
free(primePtr);
free(primePtr);
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}</lang>
}</syntaxhighlight>
{{out}}
{{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
<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: Line 402:
=={{header|C sharp}}==
=={{header|C sharp}}==
{{works with|C sharp|7}}
{{works with|C sharp|7}}
<lang csharp>using static System.Console;
<syntaxhighlight lang="csharp">using static System.Console;
using static System.Linq.Enumerable;
using static System.Linq.Enumerable;
using System;
using System;
Line 420: Line 420:
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 431: Line 431:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <algorithm>
<syntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <iostream>
#include <iterator>
#include <iterator>
Line 493: Line 493:
weak_primes.print(std::cout, "weak");
weak_primes.print(std::cout, "weak");
return 0;
return 0;
}</lang>
}</syntaxhighlight>


Contents of prime_sieve.hpp:
Contents of prime_sieve.hpp:
<lang cpp>#ifndef PRIME_SIEVE_HPP
<syntaxhighlight lang="cpp">#ifndef PRIME_SIEVE_HPP
#define PRIME_SIEVE_HPP
#define PRIME_SIEVE_HPP


Line 547: Line 547:
}
}


#endif</lang>
#endif</syntaxhighlight>


{{out}}
{{out}}
Line 560: Line 560:


=={{header|D}}==
=={{header|D}}==
<lang d>import std.algorithm;
<syntaxhighlight lang="d">import std.algorithm;
import std.array;
import std.array;
import std.range;
import std.range;
Line 653: 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 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);
writefln("There are %d weak primes below 10,000,000", weakPrimes.filter!"a<10_000_000".count);
}</lang>
}</syntaxhighlight>
{{out}}
{{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]
<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 663: Line 663:


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: formatting grouping kernel math math.primes sequences
<syntaxhighlight lang="factor">USING: formatting grouping kernel math math.primes sequences
tools.memory.private ;
tools.memory.private ;
IN: rosetta-code.strong-primes
IN: rosetta-code.strong-primes
Line 687: Line 687:
First 37 weak primes:\n%[%d, %]
First 37 weak primes:\n%[%d, %]
%s weak primes below 1,000,000
%s weak primes below 1,000,000
%s weak primes below 10,000,000\n" printf</lang>
%s weak primes below 10,000,000\n" printf</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 702: Line 702:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>
<syntaxhighlight lang="freebasic">
#include "isprime.bas"
#include "isprime.bas"


Line 762: Line 762:
wend
wend
print "There are ";c;" weak primes below ten million"
print "There are ";c;" weak primes below ten million"
print</lang>
print</syntaxhighlight>
{{out}}<pre>
{{out}}<pre>
The first 36 strong primes are:
The first 36 strong primes are:
Line 776: Line 776:


=={{header|Frink}}==
=={{header|Frink}}==
<lang frink>
<syntaxhighlight lang="frink">
strongPrimes[end=undef] := select[primes[3,end], {|p| p > (previousPrime[p] + nextPrime[p])/2 }]
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 }]
weakPrimes[end=undef] := select[primes[3,end], {|p| p < (previousPrime[p] + nextPrime[p])/2 }]
Line 787: Line 787:
println["Weak primes below 1,000,000: " + length[weakPrimes[1_000_000]]]
println["Weak primes below 1,000,000: " + length[weakPrimes[1_000_000]]]
println["Weak primes below 10,000,000: " + length[weakPrimes[10_000_000]]]
println["Weak primes below 10,000,000: " + length[weakPrimes[10_000_000]]]
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 800: Line 800:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 885: Line 885:
fmt.Println("\nThe number of weak primes below 1,000,000 is", commatize(count))
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)))
fmt.Println("\nThe number of weak primes below 10,000,000 is", commatize(len(weak)))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 905: Line 905:
=={{header|Haskell}}==
=={{header|Haskell}}==
Uses primes library: http://hackage.haskell.org/package/primes-0.2.1.0/docs/Data-Numbers-Primes.html
Uses primes library: http://hackage.haskell.org/package/primes-0.2.1.0/docs/Data-Numbers-Primes.html
<lang haskell>import Text.Printf (printf)
<syntaxhighlight lang="haskell">import Text.Printf (printf)
import Data.Numbers.Primes (primes)
import Data.Numbers.Primes (primes)


Line 923: Line 923:
printf "Weak primes below 10,000,000: %d\n\n" . length . takeWhile (<10000000) $ weakPrimes
printf "Weak primes below 10,000,000: %d\n\n" . length . takeWhile (<10000000) $ weakPrimes
where strongPrimes = xPrimes (>) primes
where strongPrimes = xPrimes (>) primes
weakPrimes = xPrimes (<) primes</lang>
weakPrimes = xPrimes (<) primes</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 966: Line 966:


=={{header|Java}}==
=={{header|Java}}==
<lang java>
<syntaxhighlight lang="java">
public class StrongAndWeakPrimes {
public class StrongAndWeakPrimes {


Line 1,071: Line 1,071:


}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,085: Line 1,085:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Primes, Formatting
<syntaxhighlight lang="julia">using Primes, Formatting


function parseprimelist()
function parseprimelist()
Line 1,117: Line 1,117:


parseprimelist()
parseprimelist()
</lang> {{output}} <pre>
</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 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.
There are 37,723 stromg primes less than 1 million.
Line 1,131: Line 1,131:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Java}}
{{trans|Java}}
<lang scala>private const val MAX = 10000000 + 1000
<syntaxhighlight lang="scala">private const val MAX = 10000000 + 1000
private val primes = BooleanArray(MAX)
private val primes = BooleanArray(MAX)


Line 1,233: Line 1,233:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>First 36 strong primes:
<pre>First 36 strong primes:
Line 1,245: Line 1,245:


=={{header|Ksh}}==
=={{header|Ksh}}==
<lang ksh>
<syntaxhighlight lang="ksh">
#!/bin/ksh
#!/bin/ksh


Line 1,334: Line 1,334:
printf "Number of Weak Primes under %d is: %d\n" $GOAL1 ${goal1_weak}
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 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}</lang>
printf "Number of Balanced Primes under %d is: %d\n\n\n" $MAX_INT ${bpcnt}</syntaxhighlight>
{{out}}<pre>
{{out}}<pre>
Total primes under 10000000 = 664579
Total primes under 10000000 = 664579
Line 1,353: Line 1,353:
=={{header|Lua}}==
=={{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.
This could be made faster but favours readability. It runs in about 3.3 seconds in LuaJIT on a 2.8 GHz core.
<lang lua>-- Return a table of the primes up to n, then one more
<syntaxhighlight lang="lua">-- Return a table of the primes up to n, then one more
function primeList (n)
function primeList (n)
local function isPrime (x)
local function isPrime (x)
Line 1,405: Line 1,405:
for i = 1, 37 do io.write(weak[i] .. " ") end
for i = 1, 37 do io.write(weak[i] .. " ") end
print("\n\nThere are " .. wCount .. " weak primes below one million.")
print("\n\nThere are " .. wCount .. " weak primes below one million.")
print("\nThere are " .. #weak .. " weak primes below ten million.")</lang>
print("\nThere are " .. #weak .. " weak primes below ten million.")</syntaxhighlight>
{{out}}
{{out}}
<pre>The first 36 strong primes are:
<pre>The first 36 strong primes are:
Line 1,423: Line 1,423:


=={{header|Maple}}==
=={{header|Maple}}==
<lang maple>isStrong := proc(n::posint) local holder;
<syntaxhighlight lang="maple">isStrong := proc(n::posint) local holder;
holder := false;
holder := false;
if isprime(n) and 1/2*prevprime(n) + 1/2*nextprime(n) < n then
if isprime(n) and 1/2*prevprime(n) + 1/2*nextprime(n) < n then
Line 1,466: Line 1,466:
countStrong(10000000)
countStrong(10000000)
countWeak(1000000)
countWeak(1000000)
countWeak(10000000)</lang>
countWeak(10000000)</syntaxhighlight>
{{Out}}
{{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]
<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: Line 1,476:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>p = Prime[Range[PrimePi[10^3]]];
<syntaxhighlight lang="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, 36, Overlaps -> True]
SequenceCases[p, ({a_, b_, c_}) /; (a + c > 2 b) :> b, 37, Overlaps -> True]
SequenceCases[p, ({a_, b_, c_}) /; (a + c > 2 b) :> b, 37, Overlaps -> True]
Line 1,484: Line 1,484:
p = Prime[Range[PrimePi[10^7] + 1]];
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]] &]]
Length[Select[Partition[p, 3, 1], #[[3]] + #[[1]] > 2 #[[2]] &]]</lang>
Length[Select[Partition[p, 3, 1], #[[3]] + #[[1]] > 2 #[[2]] &]]</syntaxhighlight>
{{out}}
{{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}
<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: Line 1,495:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import math, strutils
<syntaxhighlight lang="nim">import math, strutils


const
const
Line 1,542: Line 1,542:
echo " ", weakPrimes[0..36].join(" ")
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 1_000_000: ", weakPrimes.count(1_000_000)
echo "Count of weak primes below 10_000_000: ", weakPrimes.count(10_000_000)</lang>
echo "Count of weak primes below 10_000_000: ", weakPrimes.count(10_000_000)</syntaxhighlight>


{{out}}
{{out}}
Line 1,561: Line 1,561:
If deltaAfter < deltaBefore than a strong prime is found.
If deltaAfter < deltaBefore than a strong prime is found.
<lang pascal>program WeakPrim;
<syntaxhighlight lang="pascal">program WeakPrim;
{$IFNDEF FPC}
{$IFNDEF FPC}
{$AppType CONSOLE}
{$AppType CONSOLE}
Line 1,729: Line 1,729:
WeakOut(37);
WeakOut(37);
CntWeakStrong10(CntWs);
CntWeakStrong10(CntWs);
end.</lang>
end.</syntaxhighlight>
{{Out}}
{{Out}}
<pre>
<pre>
Line 1,752: Line 1,752:
{{trans|Raku}}
{{trans|Raku}}
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use ntheory qw(primes vecfirst);
<syntaxhighlight lang="perl">use ntheory qw(primes vecfirst);


sub comma {
sub comma {
Line 1,779: Line 1,779:
print "Count of $type primes <= @{[comma $c1]}: " . comma below($c1,@$pr) . "\n";
print "Count of $type primes <= @{[comma $c1]}: " . comma below($c1,@$pr) . "\n";
print "Count of $type primes <= @{[comma $c2]}: " . comma scalar @$pr . "\n";
print "Count of $type primes <= @{[comma $c2]}: " . comma scalar @$pr . "\n";
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,799: Line 1,799:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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>
Line 1,812: Line 1,812:
<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>
<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>
<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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,822: Line 1,822:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>#MAX=10000000+20
<syntaxhighlight lang="purebasic">#MAX=10000000+20
Global Dim P.b(#MAX) : FillMemory(@P(),#MAX,1,#PB_Byte)
Global Dim P.b(#MAX) : FillMemory(@P(),#MAX,1,#PB_Byte)
Global NewList Primes.i()
Global NewList Primes.i()
Line 1,859: Line 1,859:
PrintN("Number of weak primes below 1'000'000 = "+FormatNumber(c2,0,".","'"))
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,".","'"))
PrintN("Number of weak primes below 10'000'000 = "+FormatNumber(ListSize(Weak()),0,".","'"))
Input()</lang>
Input()</syntaxhighlight>
{{out}}
{{out}}
<pre>First 36 strong primes:
<pre>First 36 strong primes:
Line 1,875: Line 1,875:


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>.
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>.
<lang python>import numpy as np
<syntaxhighlight lang="python">import numpy as np


def primesfrom2to(n):
def primesfrom2to(n):
Line 1,911: Line 1,911:
print('\nTOTAL primes below 1,000,000:',
print('\nTOTAL primes below 1,000,000:',
sum(1 for pr in p if pr < 1_000_000))
sum(1 for pr in p if pr < 1_000_000))
print('TOTAL primes below 10,000,000:', len(p))</lang>
print('TOTAL primes below 10,000,000:', len(p))</syntaxhighlight>


{{out}}
{{out}}
Line 1,934: Line 1,934:
{{works with|Rakudo|2018.11}}
{{works with|Rakudo|2018.11}}


<lang perl6>sub comma { $^i.flip.comb(3).join(',').flip }
<syntaxhighlight lang="raku" line>sub comma { $^i.flip.comb(3).join(',').flip }


use Math::Primesieve;
use Math::Primesieve;
Line 1,959: Line 1,959:
say "Count of $type primes <= {comma 1e6}: ", comma +@pr[^(@pr.first: * > 1e6,:k)];
say "Count of $type primes <= {comma 1e6}: ", comma +@pr[^(@pr.first: * > 1e6,:k)];
say "Count of $type primes <= {comma 1e7}: ", comma +@pr;
say "Count of $type primes <= {comma 1e7}: ", comma +@pr;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>First 36 strong primes:
<pre>First 36 strong primes:
Line 1,977: Line 1,977:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program lists a sequence (or a count) of ──strong── or ──weak── primes. */
<syntaxhighlight 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*/
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.*/
if N=='' | N=="," then N= 36 /*Not specified? Then assume default.*/
Line 2,026: Line 2,026:
else return 0 /*not " " " */
else return 0 /*not " " " */
else if y>s then return add() /*is an strong prime.*/
else if y>s then return add() /*is an strong prime.*/
return 0 /*not " " " */</lang>
return 0 /*not " " " */</syntaxhighlight>
This REXX program makes use of &nbsp; '''LINESIZE''' &nbsp; REXX program (or
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
BIF) which is used to determine the screen width (or linesize) of the
Line 2,065: Line 2,065:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "stdlib.ring"


Line 2,170: Line 2,170:
see "weak primes below 1,000,000: " + primes2 + nl
see "weak primes below 1,000,000: " + primes2 + nl
see "weak primes below 10,000,000: " + primes3 + nl
see "weak primes below 10,000,000: " + primes3 + nl
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 2,184: Line 2,184:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>require 'prime'
<syntaxhighlight lang="ruby">require 'prime'


strong_gen = Enumerator.new{|y| Prime.each_cons(3){|a,b,c|y << b if a+c-b<b} }
strong_gen = Enumerator.new{|y| Prime.each_cons(3){|a,b,c|y << b if a+c-b<b} }
Line 2,203: Line 2,203:
puts "#{strongs} strong primes and #{weaks} weak primes below #{limit}."
puts "#{strongs} strong primes and #{weaks} weak primes below #{limit}."
end
end
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>First 36 strong primes:
<pre>First 36 strong primes:
Line 2,216: Line 2,216:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>fn is_prime(n: i32) -> bool {
<syntaxhighlight lang="rust">fn is_prime(n: i32) -> bool {
for i in 2..n {
for i in 2..n {
if i * i > n {
if i * i > n {
Line 2,311: Line 2,311:
}
}
println!("weak primes below 10,000,000: {}", n);
println!("weak primes below 10,000,000: {}", n);
}</lang>
}</syntaxhighlight>
<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
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,323: Line 2,323:
=={{header|Scala}}==
=={{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.
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.
<lang scala>object StrongWeakPrimes {
<syntaxhighlight lang="scala">object StrongWeakPrimes {
def main(args: Array[String]): Unit = {
def main(args: Array[String]): Unit = {
val bnd = 1000000
val bnd = 1000000
Line 2,340: Line 2,340:
def primeTrips: Iterator[LazyList[Int]] = primes.sliding(3)
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))
def primes: LazyList[Int] = 2 #:: LazyList.from(3, 2).filter(n => !Iterator.range(3, math.sqrt(n).toInt + 1, 2).exists(n%_ == 0))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,353: Line 2,353:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Raku}}
{{trans|Raku}}
<lang ruby>var primes = 10_000_019.primes
<syntaxhighlight lang="ruby">var primes = 10_000_019.primes


var (*strong, *weak, *balanced)
var (*strong, *weak, *balanced)
Line 2,375: Line 2,375:
say ("Count of #{type} primes <= #{c1.commify}: ", pr.first_index { _ > 1e6 }.commify)
say ("Count of #{type} primes <= #{c1.commify}: ", pr.first_index { _ > 1e6 }.commify)
say ("Count of #{type} primes <= #{c2.commify}: " , pr.len.commify)
say ("Count of #{type} primes <= #{c2.commify}: " , pr.len.commify)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,395: Line 2,395:


=={{header|Swift}}==
=={{header|Swift}}==
<lang swift>import Foundation
<syntaxhighlight lang="swift">import Foundation


class PrimeSieve {
class PrimeSieve {
Line 2,486: Line 2,486:


strongPrimes.printInfo(name: "strong")
strongPrimes.printInfo(name: "strong")
weakPrimes.printInfo(name: "weak")</lang>
weakPrimes.printInfo(name: "weak")</syntaxhighlight>


{{out}}
{{out}}
Line 2,501: Line 2,501:
{{libheader|Wren-math}}
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/math" for Int
<syntaxhighlight lang="ecmascript">import "/math" for Int
import "/fmt" for Fmt
import "/fmt" for Fmt


Line 2,523: Line 2,523:
Fmt.print("$d", weak.take(37))
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.", 1e6, weak.count{ |n| n < 1e6 })
Fmt.print("\nThe count of the weak primes below $,d is $,d.", 1e7, weak.count{ |n| n < 1e7 })</lang>
Fmt.print("\nThe count of the weak primes below $,d is $,d.", 1e7, weak.count{ |n| n < 1e7 })</syntaxhighlight>


{{out}}
{{out}}
Line 2,543: Line 2,543:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>proc NumOut(Num); \Output positive integer with commas
<syntaxhighlight lang="xpl0">proc NumOut(Num); \Output positive integer with commas
int Num, Dig, Cnt;
int Num, Dig, Cnt;
[Cnt:= [0];
[Cnt:= [0];
Line 2,601: Line 2,601:
NumOut(WeakCnt);
NumOut(WeakCnt);
CrLf(0);
CrLf(0);
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 2,620: Line 2,620:


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


Line 2,632: Line 2,632:
}
}
ps.pop(0); ps.append(pw.nextPrime().toInt());
ps.pop(0); ps.append(pw.nextPrime().toInt());
}while(pn<=N);</lang>
}while(pn<=N);</syntaxhighlight>
<lang zkl>foreach nm,list,psz in (T(T("strong",strong,36), T("weak",weak,37))){
<syntaxhighlight 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("First %d %s primes:\n%s".fmt(psz,nm,list[0,psz].concat(" ")));
println("Count of %s primes <= %,10d: %,8d"
println("Count of %s primes <= %,10d: %,8d"
.fmt(nm,1e6,list.reduce('wrap(s,p){ s + (p<=1e6) },0)));
.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()));
println("Count of %s primes <= %,10d: %,8d\n".fmt(nm,1e7,list.len()));
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>