Strange unique prime triplets: Difference between revisions
m
syntax highlighting fixup automation
(Added Sidef) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 12:
{{trans|Python}}
<
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))</
{{out}}
Line 89:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<
PROC Main()
Line 122:
OD
PrintF("%EThere are %I prime triplets",count)
RETURN</
{{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}}
<
# 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</
{{out}}
<pre>
Line 239:
=={{header|ALGOL W}}==
Based on {{Trans|Wren}}
<
% 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.</
{{out}}
<pre>
Line 339:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f STRANGE_UNIQUE_PRIME_TRIPLETS.AWK
# converted from Go
Line 380:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 431:
=={{header|C}}==
<
#include <stdio.h>
#include <string.h>
Line 524:
return 0;
}</
{{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.
<
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; } }</
{{out}}
Timings from tio.run
Line 650:
=={{header|C++}}==
<
#include <iostream>
#include <vector>
Line 710:
strange_unique_prime_triplets(1000, false);
return 0;
}</
{{out}}
Line 766:
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program Strange_primes;
Line 852:
writeln('There are ', cs, ' unique prime triples under 1,000 which sum to a prime.');
readln;
end.</
=={{header|F_Sharp|F#}}==
This task uses [[Extensible_prime_generator#The_functions|Extensible Prime Generator (F#)]].<br>
<
// 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>
{{out}}
<pre>
Line 870:
</pre>
=={{header|Factor}}==
<
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</
{{out}}
<pre>
Line 936:
=={{header|Fermat}}==
<
if Isprime(n) and Isprime(m) and Isprime(p) and Isprime(n+m+p) then 1 else 0 fi.
Line 945:
od;
od;
od</
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.
<
dim as uinteger c = 0
Line 968:
next p
print "There are ";c;" triples below 1000."</
{{out}}<pre>3 + 5 + 11 = 19
3 + 5 + 23 = 31
Line 1,015:
=={{header|Forth}}==
{{works with|Gforth}}
<
: notprime! ( n -- ) here + 1 swap c! ;
Line 1,084:
." Count of strange unique prime triplets < 1000: "
1000 count_strange_unique_prime_triplets . cr
bye</
{{out}}
Line 1,145:
===Basic===
{{trans|Wren}}
<
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)
}</
{{out}}
Line 1,273:
===Faster===
{{trans|Wren}}
<
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)
}</
{{out}}
Line 1,363:
=={{header|Java}}==
<
public class StrangeUniquePrimeTriplets {
Line 1,423:
return sieve;
}
}</
{{out}}
Line 1,481:
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`.
<
def task($n):
Line 1,493:
task(30),
"\nStretch goal: \(count(task(1000)))"</
{{out}}
<pre>
Line 1,542:
</pre>
=={{header|Julia}}==
<
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)
</
<pre>
Triplet Sum
Line 1,632:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Select[Subsets[p, {3}], Total/*PrimeQ]
p = Prime[Range@PrimePi[1000]];
Length[Select[Subsets[p, {3}], Total/*PrimeQ]]</
{{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}}==
<
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()</
{{out}}
Line 1,731:
=={{header|Pascal}}==
{{works with|Free Pascal}}
<
//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.</
{{out}}
<pre>
Line 1,929:
=={{header|Perl}}==
{{libheader|ntheory}}
<
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);
}</
{{out}}
<pre>Found 42 strange unique prime triplets up to 30.
Line 1,944:
=={{header|Phix}}==
<!--<
<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>
<!--</
{{out}}
<pre>
Line 2,034:
Using [https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.generate.Sieve.primerange sympy.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)):_}")</
{{out}}
Line 2,099:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
for 30, 1000 -> \k {
Line 2,105:
say "Found ", +$_, " strange unique prime triplets up to ", k
}
}</
{{out}}
<pre>
Line 2,113:
=={{header|REXX}}==
<
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</
{{out|output|text= when using the default input:}}
<pre>
Line 2,211:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 2,233:
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 2,285:
=={{header|Rust}}==
<
let mut sieve = vec![true; limit];
if limit > 0 {
Line 2,355:
strange_unique_prime_triplets(30, true);
strange_unique_prime_triplets(1000, false);
}</
{{out}}
Line 2,407:
=={{header|Sidef}}==
<
var triplets = []
combinations(n.primes, 3, {|*a|
Line 2,417:
}
printf("Found %d strange unique prime triplets up to %s.\n", triplets.len, n)
}</
{{out}}
<pre>
Line 2,433:
=={{header|Swift}}==
<
func primeSieve(limit: Int) -> [Bool] {
Line 2,501:
strangeUniquePrimeTriplets(limit: 30, verbose: true)
strangeUniquePrimeTriplets(limit: 1000, verbose: false)</
{{out}}
Line 2,556:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Module Module1
Line 2,630:
End Sub
End Module</
{{out}}
<pre>Same as C#</pre>
Line 2,639:
{{libheader|Wren-trait}}
{{libheader|Wren-fmt}}
<
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)</
{{out}}
Line 2,719:
===Faster===
The following version uses a prime sieve and is about 17 times faster than the 'basic' version.
<
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)</
{{out}}
Line 2,755:
=={{header|XPL0}}==
<
int N, I;
[if N <= 2 then return N = 2;
Line 2,788:
];
];
]</
{{out}}
|