Strong and weak primes: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: syntax coloured, removed deprecated @=, made better use of builtins, plus some other minor tidying)
m (syntax highlighting fixup automation)
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 663:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting grouping kernel math math.primes sequences
tools.memory.private ;
IN: rosetta-code.strong-primes
Line 687:
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:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
#include "isprime.bas"
 
Line 762:
wend
print "There are ";c;" weak primes below ten million"
print</langsyntaxhighlight>
{{out}}<pre>
The first 36 strong primes are:
Line 776:
 
=={{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:
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:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 885:
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:
=={{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:
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:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
public class StrongAndWeakPrimes {
 
Line 1,071:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,085:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes, Formatting
 
function parseprimelist()
Line 1,117:
 
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:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">private const val MAX = 10000000 + 1000
private val primes = BooleanArray(MAX)
 
Line 1,233:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>First 36 strong primes:
Line 1,245:
 
=={{header|Ksh}}==
<langsyntaxhighlight lang="ksh">
#!/bin/ksh
 
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\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:
=={{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:
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:
 
=={{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:
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:
 
=={{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:
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:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import math, strutils
 
const
Line 1,542:
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:
If deltaAfter < deltaBefore than a strong prime is found.
<langsyntaxhighlight lang="pascal">program WeakPrim;
{$IFNDEF FPC}
{$AppType CONSOLE}
Line 1,729:
WeakOut(37);
CntWeakStrong10(CntWs);
end.</langsyntaxhighlight>
{{Out}}
<pre>
Line 1,752:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use ntheory qw(primes vecfirst);
 
sub comma {
Line 1,779:
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:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
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 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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,822:
 
=={{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,859:
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,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>.
<langsyntaxhighlight lang="python">import numpy as np
 
def primesfrom2to(n):
Line 1,911:
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,934:
{{works with|Rakudo|2018.11}}
 
<syntaxhighlight lang="raku" perl6line>sub comma { $^i.flip.comb(3).join(',').flip }
 
use Math::Primesieve;
Line 1,959:
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,977:
 
=={{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,026:
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,065:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 2,170:
see "weak primes below 1,000,000: " + primes2 + nl
see "weak primes below 10,000,000: " + primes3 + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,184:
 
=={{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,203:
puts "#{strongs} strong primes and #{weaks} weak primes below #{limit}."
end
</syntaxhighlight>
</lang>
{{out}}
<pre>First 36 strong primes:
Line 2,216:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn is_prime(n: i32) -> bool {
for i in 2..n {
if i * i > n {
Line 2,311:
}
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,323:
=={{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,340:
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,353:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">var primes = 10_000_019.primes
 
var (*strong, *weak, *balanced)
Line 2,375:
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,395:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
class PrimeSieve {
Line 2,486:
 
strongPrimes.printInfo(name: "strong")
weakPrimes.printInfo(name: "weak")</langsyntaxhighlight>
 
{{out}}
Line 2,501:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/fmt" for Fmt
 
Line 2,523:
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,543:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">proc NumOut(Num); \Output positive integer with commas
int Num, Dig, Cnt;
[Cnt:= [0];
Line 2,601:
NumOut(WeakCnt);
CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
Line 2,620:
 
[[Extensible prime generator#zkl]] could be used instead.
<langsyntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
const N=1e7;
 
Line 2,632:
}
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>
10,333

edits