Prime triangle: Difference between revisions
Content added Content deleted
m (→{{header|J}}: correctness) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 7: | Line 7: | ||
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
{{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. |
As Algol 68G under Windows is fully interpreted, a reduced number of rows is produced. |
||
< |
<syntaxhighlight 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 # |
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 # |
# construct a primesieve and from that a table of pairs of numbers whose sum is prime # |
||
Line 100: | Line 100: | ||
OD; |
OD; |
||
print( ( newline ) ) |
print( ( newline ) ) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 124: | Line 124: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <assert.h> |
||
#include <stdbool.h> |
#include <stdbool.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 207: | Line 207: | ||
printf("\nElapsed time: %f seconds\n", duration); |
printf("\nElapsed time: %f seconds\n", duration); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 239: | 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. |
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. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdint.h> |
#include <stdint.h> |
||
Line 330: | Line 330: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 2 |
<pre> 1 2 |
||
Line 355: | Line 355: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <cassert> |
||
#include <chrono> |
#include <chrono> |
||
#include <iomanip> |
#include <iomanip> |
||
Line 431: | Line 431: | ||
std::chrono::duration<double> duration(end - start); |
std::chrono::duration<double> duration(end - start); |
||
std::cout << "\nElapsed time: " << duration.count() << " seconds\n"; |
std::cout << "\nElapsed time: " << duration.count() << " seconds\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 462: | Line 462: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)] |
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)] |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Prime triangle. Nigel Galloway: April 12th., 2022 |
// 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)) |
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: | Line 471: | ||
{2..20}|>Seq.iter(fun n->(primeT>>Seq.head>>List.iter(printf "%3d"))n;printfn "");; |
{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 "" |
{2..20}|>Seq.iter(primeT>>Seq.length>>printf "%d "); printfn "" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 500: | Line 500: | ||
Takes about 0.64 seconds. |
Takes about 0.64 seconds. |
||
{{trans|Phix}} |
{{trans|Phix}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 570: | Line 570: | ||
} |
} |
||
fmt.Println() |
fmt.Println() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 603: | Line 603: | ||
Implementation: |
Implementation: |
||
< |
<syntaxhighlight lang="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 }}</ |
prime_pair_seqs=: {{ y add_plink (2}.i.y) add_plink^:(y-2) 1 }}</syntaxhighlight> |
||
Task example (displaying counts of number of valid sequences to the left, because that looks nice): |
Task example (displaying counts of number of valid sequences to the left, because that looks nice): |
||
< |
<syntaxhighlight lang="j">task=: {{ |
||
for_j.2}.i.1+y do. |
for_j.2}.i.1+y do. |
||
N=. #seqs=. prime_pair_seqs j |
N=. #seqs=. prime_pair_seqs j |
||
Line 634: | Line 634: | ||
155464 | 1 16 15 14 17 12 11 8 9 10 13 6 7 4 3 2 5 18 |
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 |
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</ |
1588162 | 1 18 19 12 17 14 15 16 13 10 9 8 11 2 5 6 7 4 3 20</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">public class PrimeTriangle { |
||
public static void main(String[] args) { |
public static void main(String[] args) { |
||
long start = System.currentTimeMillis(); |
long start = System.currentTimeMillis(); |
||
Line 711: | Line 711: | ||
return ((1L << n) & 0x28208a20a08a28acL) != 0; |
return ((1L << n) & 0x28208a20a08a28acL) != 0; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 742: | Line 742: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
=== Filter method === |
=== Filter method === |
||
< |
<syntaxhighlight lang="julia">using Combinatorics, Primes |
||
function primetriangle(nrows::Integer) |
function primetriangle(nrows::Integer) |
||
Line 773: | Line 773: | ||
@time primetriangle(16) |
@time primetriangle(16) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
1 2 |
1 2 |
||
Line 797: | Line 797: | ||
=== Generator method === |
=== Generator method === |
||
Similar to the Phix entry. |
Similar to the Phix entry. |
||
< |
<syntaxhighlight lang="julia">using Primes |
||
function solverow(row, pos, avail) |
function solverow(row, pos, avail) |
||
Line 829: | Line 829: | ||
println(" 1 2\n" * prod(rowstrings), "\n", counts) |
println(" 1 2\n" * prod(rowstrings), "\n", counts) |
||
end |
end |
||
</ |
</syntaxhighlight> {{out}} |
||
<pre> |
<pre> |
||
1 2 |
1 2 |
||
Line 856: | Line 856: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">ClearAll[FindPrimeTriangles, FindPrimeTrianglesHelper] |
||
FindPrimeTriangles[max_] := |
FindPrimeTriangles[max_] := |
||
Module[{count = 0, firstsolution, primes, primeQ}, |
Module[{count = 0, firstsolution, primes, primeQ}, |
||
Line 888: | Line 888: | ||
count |
count |
||
] |
] |
||
Table[FindPrimeTriangles[S],{S, 2, 20}]</ |
Table[FindPrimeTriangles[S],{S, 2, 20}]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{1,2} |
<pre>{1,2} |
||
Line 915: | Line 915: | ||
{{trans|Raku}} |
{{trans|Raku}} |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use feature 'say'; |
use feature 'say'; |
||
Line 949: | Line 949: | ||
} |
} |
||
say "\n" . join ' ', @count[2..$#count];</ |
say "\n" . join ' ', @count[2..$#count];</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 2 |
<pre>1 2 |
||
Line 973: | Line 973: | ||
{{libheader|Phix/online}} |
{{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. 😎<br> You can run this online [http://phix.x10.mx/p2js/prime_triangles.htm here] (expect a blank screen for about 24s). |
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. 😎<br> You can run this online [http://phix.x10.mx/p2js/prime_triangles.htm here] (expect a blank screen for about 24s). |
||
<!--< |
<!--<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;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</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: | 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: #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> |
<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,055: | 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. |
Limit the upper threshold a bit to avoid multiple hours of pointless calculations. Even just up to 17 takes over 20 minutes. |
||
<lang |
<syntaxhighlight lang="raku" line>my @count = 0, 0, 1; |
||
my $lock = Lock.new; |
my $lock = Lock.new; |
||
put (1,2); |
put (1,2); |
||
Line 1,079: | Line 1,079: | ||
} |
} |
||
} |
} |
||
put "\n", @count[2..*];</ |
put "\n", @count[2..*];</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 2 |
<pre>1 2 |
||
Line 1,102: | Line 1,102: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">fn is_prime(n: u32) -> bool { |
||
assert!(n < 64); |
assert!(n < 64); |
||
((1u64 << n) & 0x28208a20a08a28ac) != 0 |
((1u64 << n) & 0x28208a20a08a28ac) != 0 |
||
Line 1,172: | Line 1,172: | ||
let time = start.elapsed(); |
let time = start.elapsed(); |
||
println!("\nElapsed time: {} milliseconds", time.as_millis()); |
println!("\nElapsed time: {} milliseconds", time.as_millis()); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,202: | Line 1,202: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">import Foundation |
||
func isPrime(_ n: Int) -> Bool { |
func isPrime(_ n: Int) -> Bool { |
||
Line 1,278: | Line 1,278: | ||
let endTime = CFAbsoluteTimeGetCurrent() |
let endTime = CFAbsoluteTimeGetCurrent() |
||
print("\nElapsed time: \(endTime - startTime) seconds")</ |
print("\nElapsed time: \(endTime - startTime) seconds")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,309: | Line 1,309: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{Trans|ALGOL 68}} |
{{Trans|ALGOL 68}} |
||
< |
<syntaxhighlight lang="vbnet">Option Strict On |
||
Option Explicit On |
Option Explicit On |
||
Line 1,424: | Line 1,424: | ||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,453: | Line 1,453: | ||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
Takes around 18.7 seconds which is fine for Wren. |
Takes around 18.7 seconds which is fine for Wren. |
||
< |
<syntaxhighlight lang="ecmascript">import "./fmt" for Fmt |
||
var canFollow = [] |
var canFollow = [] |
||
Line 1,507: | Line 1,507: | ||
} |
} |
||
System.print() |
System.print() |
||
System.print(counts.join(" "))</ |
System.print(counts.join(" "))</syntaxhighlight> |
||
{{out}} |
{{out}} |