Strange unique prime triplets: Difference between revisions

m
syntax highlighting fixup automation
(Added Sidef)
m (syntax highlighting fixup automation)
Line 12:
{{trans|Python}}
 
<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 37:
 
V mx = 1'000
print("\nIf n, m, p < #. finds #.".format(mx, strange_triplets(mx).len))</langsyntaxhighlight>
 
{{out}}
Line 89:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:SIEVE.ACT"
 
PROC Main()
Line 122:
OD
PrintF("%EThere are %I prime triplets",count)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Strange_unique_prime_triplets.png Screenshot from Atari 8-bit computer]
Line 147:
{{Trans|Algol W}} which is based on {{Trans|Wren}}
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68">BEGIN # find some strange unique primes - triplets of primes n, m, p #
# where n + m + p is also prime and n =/= m =/= p #
# we need to find the strange unique prime triplets below 1000 #
Line 188:
print( ( "Found ", whole( c30, -6 ), " strange unique prime triplets up to 30", newline ) );
print( ( "Found ", whole( s count, -6 ), " strange unique prime triplets up to 1000", newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 239:
=={{header|ALGOL W}}==
Based on {{Trans|Wren}}
<langsyntaxhighlight lang="algolw">begin % find some strange unique primes - triplets of primes n, m, p %
% where n + m + p is also prime and n =/= m =/= p %
% sets p( 1 :: n ) to a sieve of primes up to n %
Line 289:
write( i_w := 3, s_w := 0, "Found ", sCount, " strange unique prime triplets up to 1000" );
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 339:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f STRANGE_UNIQUE_PRIME_TRIPLETS.AWK
# converted from Go
Line 380:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 431:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <string.h>
Line 524:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Strange unique prime triplets < 30:
Line 575:
=={{header|C#|CSharp}}==
Just for fun, <30 sorted by sum, instead of order generated. One might think one should include the sieve generation time, but it is orders of magnitude smaller than the permute/sum time for these relatively low numbers.
<langsyntaxhighlight lang="csharp">using System; using System.Collections.Generic; using static System.Console; using System.Linq; using DT = System.DateTime;
 
class Program { static void Main(string[] args) { string s;
Line 597:
if (!flags[j]) { yield return j;
for (int k = sq; k <= lim; k += j) flags[k] = true; }
for (; j <= lim; j++) if (!flags[j]) yield return j; } }</langsyntaxhighlight>
{{out}}
Timings from tio.run
Line 650:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <vector>
Line 710:
strange_unique_prime_triplets(1000, false);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 766:
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Strange_primes;
 
Line 852:
writeln('There are ', cs, ' unique prime triples under 1,000 which sum to a prime.');
readln;
end.</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
This task uses [[Extensible_prime_generator#The_functions|Extensible Prime Generator (F#)]].<br>
<langsyntaxhighlight lang="fsharp">
// Strange unique prime triplets. Nigel Galloway: March 12th., 2021
let sP n=let N=primes32()|>Seq.takeWhile((>)n)|>Array.ofSeq
Line 863:
printfn "%d" (Seq.length(sP 1000))
printfn "%d" (Seq.length(sP 10000))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 870:
</pre>
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting io kernel math math.combinatorics math.primes
sequences tools.memory.private ;
 
Line 886:
30 strange
1,000 count-strange commas nl
"Found %s strange prime triplets with n, m, p < 1,000.\n" printf</langsyntaxhighlight>
{{out}}
<pre>
Line 936:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">Function IsSUPT(n,m,p) =
if Isprime(n) and Isprime(m) and Isprime(p) and Isprime(n+m+p) then 1 else 0 fi.
 
Line 945:
od;
od;
od</langsyntaxhighlight>
I'll leave the stretch goal for someone else.
 
=={{header|FreeBASIC}}==
Use the function at [[Primality by trial division#FreeBASIC]] as an include; I can't be bothered reproducing it here.
<langsyntaxhighlight lang="freebasic">#include"isprime.bas"
 
dim as uinteger c = 0
Line 968:
next p
 
print "There are ";c;" triples below 1000."</langsyntaxhighlight>
{{out}}<pre>3 + 5 + 11 = 19
3 + 5 + 23 = 31
Line 1,015:
=={{header|Forth}}==
{{works with|Gforth}}
<langsyntaxhighlight lang="forth">: prime? ( n -- ? ) here + c@ 0= ;
: notprime! ( n -- ) here + 1 swap c! ;
 
Line 1,084:
." Count of strange unique prime triplets < 1000: "
1000 count_strange_unique_prime_triplets . cr
bye</langsyntaxhighlight>
 
{{out}}
Line 1,145:
===Basic===
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,220:
cs := commatize(strangePrimes(999, true))
fmt.Printf("\nThere are %s unique prime triples under 1,000 which sum to a prime.\n", cs)
}</langsyntaxhighlight>
 
{{out}}
Line 1,273:
===Faster===
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,357:
cs := commatize(strangePrimes(999, true))
fmt.Printf("\nThere are %s unique prime triples under 1,000 which sum to a prime.\n", cs)
}</langsyntaxhighlight>
 
{{out}}
Line 1,363:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.*;
 
public class StrangeUniquePrimeTriplets {
Line 1,423:
return sieve;
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,481:
 
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`.
<langsyntaxhighlight lang="jq">def count(s): reduce s as $x (null; .+1);
 
def task($n):
Line 1,493:
 
task(30),
"\nStretch goal: \(count(task(1000)))"</langsyntaxhighlight>
{{out}}
<pre>
Line 1,542:
</pre>
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function prime_sum_prime_triplets_to(N, verbose=false)
Line 1,569:
@time prime_sum_prime_triplets_to(10000)
@time prime_sum_prime_triplets_to(100000)
</langsyntaxhighlight>{{out}}
<pre>
Triplet Sum
Line 1,632:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">p = Prime[Range@PrimePi[30]];
Select[Subsets[p, {3}], Total/*PrimeQ]
 
p = Prime[Range@PrimePi[1000]];
Length[Select[Subsets[p, {3}], Total/*PrimeQ]]</langsyntaxhighlight>
{{out}}
<pre>{{3,5,11},{3,5,23},{3,5,29},{3,7,13},{3,7,19},{3,11,17},{3,11,23},{3,11,29},{3,17,23},{5,7,11},{5,7,17},{5,7,19},{5,7,29},{5,11,13},{5,13,19},{5,13,23},{5,13,29},{5,17,19},{5,19,23},{5,19,29},{7,11,13},{7,11,19},{7,11,23},{7,11,29},{7,13,17},{7,13,23},{7,17,19},{7,17,23},{7,17,29},{7,23,29},{11,13,17},{11,13,19},{11,13,23},{11,13,29},{11,17,19},{11,19,23},{11,19,29},{13,17,23},{13,17,29},{13,19,29},{17,19,23},{19,23,29}}
Line 1,642:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strformat, strutils, sugar
 
func isPrime(n: Positive): bool =
Line 1,680:
var count = 0
for _ in Primes1000.triplets(): inc count
echo "Count of strange unique prime triplets for n < m < p < 1000: ", ($count).insertSep()</langsyntaxhighlight>
 
{{out}}
Line 1,731:
=={{header|Pascal}}==
{{works with|Free Pascal}}
<langsyntaxhighlight lang="pascal">program PrimeTriplets;
//Free Pascal Compiler version 3.2.1 [2020/11/03] for x86_64fpc 3.2.1
{$IFDEF FPC}
Line 1,874:
Check_Limit(10000);
//Check_Limit(MAXZAHL);
END.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,929:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use List::Util 'sum';
Line 1,938:
printf "Found %d strange unique prime triplets up to $n.\n",
scalar grep { is_prime(sum @$_) } combinations(primes($n), 3);
}</langsyntaxhighlight>
{{out}}
<pre>Found 42 strange unique prime triplets up to 30.
Line 1,944:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0.8.4"</span><span style="color: #0000FF;">)</span>
Line 2,014:
<span style="color: #000000;">strange_triplets</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">strange_triplets</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,034:
Using [https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.generate.Sieve.primerange sympy.primerange].
 
<langsyntaxhighlight lang="python">from sympy import primerange
 
def strange_triplets(mx: int = 30) -> None:
Line 2,049:
 
mx = 1_000
print(f"\nIf n, m, p < {mx:_} finds {sum(1 for _ in strange_triplets(mx)):_}")</langsyntaxhighlight>
 
{{out}}
Line 2,099:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line># 20210312 Raku programming solution
 
for 30, 1000 -> \k {
Line 2,105:
say "Found ", +$_, " strange unique prime triplets up to ", k
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,113:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds/lists triplet strange primes (<HI) where the triplets' sum is prime*/
parse arg hi . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 30 /*Not specified? Then use the default.*/
Line 2,153:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 2,211:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 2,233:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,285:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn prime_sieve(limit: usize) -> Vec<bool> {
let mut sieve = vec![true; limit];
if limit > 0 {
Line 2,355:
strange_unique_prime_triplets(30, true);
strange_unique_prime_triplets(1000, false);
}</langsyntaxhighlight>
 
{{out}}
Line 2,407:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">for n in (30, 1000) {
var triplets = []
combinations(n.primes, 3, {|*a|
Line 2,417:
}
printf("Found %d strange unique prime triplets up to %s.\n", triplets.len, n)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,433:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
func primeSieve(limit: Int) -> [Bool] {
Line 2,501:
 
strangeUniquePrimeTriplets(limit: 30, verbose: true)
strangeUniquePrimeTriplets(limit: 1000, verbose: false)</langsyntaxhighlight>
 
{{out}}
Line 2,556:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports DT = System.DateTime
 
Module Module1
Line 2,630:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Same as C#</pre>
Line 2,639:
{{libheader|Wren-trait}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/trait" for Stepped
import "/fmt" for Fmt
Line 2,666:
strangePrimes.call(29, false)
var c = strangePrimes.call(999, true)
Fmt.print("\nThere are $,d unique prime triples under 1,000 which sum to a prime.", c)</langsyntaxhighlight>
 
{{out}}
Line 2,719:
===Faster===
The following version uses a prime sieve and is about 17 times faster than the 'basic' version.
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/fmt" for Fmt
 
Line 2,749:
strangePrimes.call(29, false)
var c = strangePrimes.call(999, true)
Fmt.print("\nThere are $,d unique prime triples under 1,000 which sum to a prime.", c)</langsyntaxhighlight>
 
{{out}}
Line 2,755:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
Line 2,788:
];
];
]</langsyntaxhighlight>
 
{{out}}
10,339

edits