Prime triangle: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|J}}: correctness)
m (syntax highlighting fixup automation)
Line 7:
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
As Algol 68G under Windows is fully interpreted, a reduced number of rows is produced.
<langsyntaxhighlight lang="algol68">BEGIN # find solutions to the "Prime Triangle" - a triangle of numbers that sum to primes #
INT max number = 18; # largest number we will consider #
# construct a primesieve and from that a table of pairs of numbers whose sum is prime #
Line 100:
OD;
print( ( newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 124:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
Line 207:
printf("\nElapsed time: %f seconds\n", duration);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 239:
Number combinations are all stored in bit positions here. The <code>bpos()</code> functions returns the position index of the least significant bit in an integer. This code gains some speed, at the cost of total loss of readability. On the plus side, it has some bit manipulation tricks that may be interesting to some.
 
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdint.h>
 
Line 330:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> 1 2
Line 355:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <cassert>
#include <chrono>
#include <iomanip>
Line 431:
std::chrono::duration<double> duration(end - start);
std::cout << "\nElapsed time: " << duration.count() << " seconds\n";
}</langsyntaxhighlight>
 
{{out}}
Line 462:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Prime triangle. Nigel Galloway: April 12th., 2022
let fN i (g,(e,l))=e|>Seq.map(fun n->let n=i n in (n::g,List.partition(i>>(=)n) l))
Line 471:
{2..20}|>Seq.iter(fun n->(primeT>>Seq.head>>List.iter(printf "%3d"))n;printfn "");;
{2..20}|>Seq.iter(primeT>>Seq.length>>printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 500:
Takes about 0.64 seconds.
{{trans|Phix}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 570:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 603:
Implementation:
 
<langsyntaxhighlight Jlang="j">add_plink=: [:;{{ <y,"1 0 x #~ 1 p:+/|:(x=. x-. y),"0/{: y }}"1
prime_pair_seqs=: {{ y add_plink (2}.i.y) add_plink^:(y-2) 1 }}</langsyntaxhighlight>
 
Task example (displaying counts of number of valid sequences to the left, because that looks nice):
 
<langsyntaxhighlight Jlang="j">task=: {{
for_j.2}.i.1+y do.
N=. #seqs=. prime_pair_seqs j
Line 634:
155464 | 1 16 15 14 17 12 11 8 9 10 13 6 7 4 3 2 5 18
480728 | 1 18 13 16 15 14 17 12 11 8 9 10 7 6 5 2 3 4 19
1588162 | 1 18 19 12 17 14 15 16 13 10 9 8 11 2 5 6 7 4 3 20</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public class PrimeTriangle {
public static void main(String[] args) {
long start = System.currentTimeMillis();
Line 711:
return ((1L << n) & 0x28208a20a08a28acL) != 0;
}
}</langsyntaxhighlight>
 
{{out}}
Line 742:
=={{header|Julia}}==
=== Filter method ===
<langsyntaxhighlight lang="julia">using Combinatorics, Primes
 
function primetriangle(nrows::Integer)
Line 773:
 
@time primetriangle(16)
</langsyntaxhighlight>{{out}}
<pre>
1 2
Line 797:
=== Generator method ===
Similar to the Phix entry.
<langsyntaxhighlight lang="julia">using Primes
 
function solverow(row, pos, avail)
Line 829:
println(" 1 2\n" * prod(rowstrings), "\n", counts)
end
</langsyntaxhighlight> {{out}}
<pre>
1 2
Line 856:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[FindPrimeTriangles, FindPrimeTrianglesHelper]
FindPrimeTriangles[max_] :=
Module[{count = 0, firstsolution, primes, primeQ},
Line 888:
count
]
Table[FindPrimeTriangles[S],{S, 2, 20}]</langsyntaxhighlight>
{{out}}
<pre>{1,2}
Line 915:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 949:
}
 
say "\n" . join ' ', @count[2..$#count];</langsyntaxhighlight>
{{out}}
<pre>1 2
Line 973:
{{libheader|Phix/online}}
Not sure this counts as particularly clever but by the sound of things it's quite a bit faster than the other entries so far. &#128526;<br> You can run this online [http://phix.x10.mx/p2js/prime_triangles.htm here] (expect a blank screen for about 24s).
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
Line 1,026:
<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;">"%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">))</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,055:
Limit the upper threshold a bit to avoid multiple hours of pointless calculations. Even just up to 17 takes over 20 minutes.
 
<syntaxhighlight lang="raku" perl6line>my @count = 0, 0, 1;
my $lock = Lock.new;
put (1,2);
Line 1,079:
}
}
put "\n", @count[2..*];</langsyntaxhighlight>
{{out}}
<pre>1 2
Line 1,102:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn is_prime(n: u32) -> bool {
assert!(n < 64);
((1u64 << n) & 0x28208a20a08a28ac) != 0
Line 1,172:
let time = start.elapsed();
println!("\nElapsed time: {} milliseconds", time.as_millis());
}</langsyntaxhighlight>
 
{{out}}
Line 1,202:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
func isPrime(_ n: Int) -> Bool {
Line 1,278:
 
let endTime = CFAbsoluteTimeGetCurrent()
print("\nElapsed time: \(endTime - startTime) seconds")</langsyntaxhighlight>
 
{{out}}
Line 1,309:
=={{header|Visual Basic .NET}}==
{{Trans|ALGOL 68}}
<langsyntaxhighlight lang="vbnet">Option Strict On
Option Explicit On
 
Line 1,424:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>
Line 1,453:
{{libheader|Wren-fmt}}
Takes around 18.7 seconds which is fine for Wren.
<langsyntaxhighlight lang="ecmascript">import "./fmt" for Fmt
 
var canFollow = []
Line 1,507:
}
System.print()
System.print(counts.join(" "))</langsyntaxhighlight>
 
{{out}}
10,333

edits