Prime triangle: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|J}}: correctness) |
Thundergnat (talk | contribs) 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.
<
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</
{{out}}
<pre>
Line 124:
=={{header|C}}==
<
#include <stdbool.h>
#include <stdio.h>
Line 207:
printf("\nElapsed time: %f seconds\n", duration);
return 0;
}</
{{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.
<
#include <stdint.h>
Line 330:
return 0;
}</
{{out}}
<pre> 1 2
Line 355:
=={{header|C++}}==
<
#include <chrono>
#include <iomanip>
Line 431:
std::chrono::duration<double> duration(end - start);
std::cout << "\nElapsed time: " << duration.count() << " seconds\n";
}</
{{out}}
Line 462:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<
// 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>
{{out}}
<pre>
Line 500:
Takes about 0.64 seconds.
{{trans|Phix}}
<
import "fmt"
Line 570:
}
fmt.Println()
}</
{{out}}
Line 603:
Implementation:
<
prime_pair_seqs=: {{ y add_plink (2}.i.y) add_plink^:(y-2) 1 }}</
Task example (displaying counts of number of valid sequences to the left, because that looks nice):
<
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</
=={{header|Java}}==
<
public static void main(String[] args) {
long start = System.currentTimeMillis();
Line 711:
return ((1L << n) & 0x28208a20a08a28acL) != 0;
}
}</
{{out}}
Line 742:
=={{header|Julia}}==
=== Filter method ===
<
function primetriangle(nrows::Integer)
Line 773:
@time primetriangle(16)
</
<pre>
1 2
Line 797:
=== Generator method ===
Similar to the Phix entry.
<
function solverow(row, pos, avail)
Line 829:
println(" 1 2\n" * prod(rowstrings), "\n", counts)
end
</
<pre>
1 2
Line 856:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
FindPrimeTriangles[max_] :=
Module[{count = 0, firstsolution, primes, primeQ},
Line 888:
count
]
Table[FindPrimeTriangles[S],{S, 2, 20}]</
{{out}}
<pre>{1,2}
Line 915:
{{trans|Raku}}
{{libheader|ntheory}}
<
use warnings;
use feature 'say';
Line 949:
}
say "\n" . join ' ', @count[2..$#count];</
{{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. 😎<br> You can run this online [http://phix.x10.mx/p2js/prime_triangles.htm here] (expect a blank screen for about 24s).
<!--<
<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>
<!--</
{{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"
my $lock = Lock.new;
put (1,2);
Line 1,079:
}
}
put "\n", @count[2..*];</
{{out}}
<pre>1 2
Line 1,102:
=={{header|Rust}}==
<
assert!(n < 64);
((1u64 << n) & 0x28208a20a08a28ac) != 0
Line 1,172:
let time = start.elapsed();
println!("\nElapsed time: {} milliseconds", time.as_millis());
}</
{{out}}
Line 1,202:
=={{header|Swift}}==
<
func isPrime(_ n: Int) -> Bool {
Line 1,278:
let endTime = CFAbsoluteTimeGetCurrent()
print("\nElapsed time: \(endTime - startTime) seconds")</
{{out}}
Line 1,309:
=={{header|Visual Basic .NET}}==
{{Trans|ALGOL 68}}
<
Option Explicit On
Line 1,424:
End Sub
End Module</
{{out}}
<pre>
Line 1,453:
{{libheader|Wren-fmt}}
Takes around 18.7 seconds which is fine for Wren.
<
var canFollow = []
Line 1,507:
}
System.print()
System.print(counts.join(" "))</
{{out}}
|