Prime triangle: Difference between revisions

(→‎{{header|C}}: add second method)
Line 888:
25.818809 seconds (249.58 M allocations: 22.295 GiB, 15.56% gc time)
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ClearAll[FindPrimeTriangles, FindPrimeTrianglesHelper]
FindPrimeTriangles[max_] :=
Module[{count = 0, firstsolution, primes, primeQ},
primes = PrimeQ[Range[2 max]];
primeQ[n_] := primes[[n]];
ClearAll[FindPrimeTrianglesHelper];
FindPrimeTrianglesHelper[start_, remainder_, mxx_] :=
Module[{last, nexts, r, newstart, newremainder},
If[Length[remainder] > 0,
last = Last[start];
Do[
r = remainder[[ri]];
If[primeQ[last + r],
newstart = Append[start, r];
newremainder = Delete[remainder, ri];
FindPrimeTrianglesHelper[newstart, newremainder, mxx]
]
,
{ri, Length[remainder]}
]
,
If[primeQ[Last[start] + mxx],
count++;
If[count == 1,
Print[Append[start, mxx]]
]
]
]
];
FindPrimeTrianglesHelper[{1}, Range[2, max - 1], max];
count
]
Table[FindPrimeTriangles[S],{S, 2, 20}]</lang>
{{out}}
<pre>{1,2}
{1,2,3}
{1,2,3,4}
{1,4,3,2,5}
{1,4,3,2,5,6}
{1,4,3,2,5,6,7}
{1,2,3,4,7,6,5,8}
{1,2,3,4,7,6,5,8,9}
{1,2,3,4,7,6,5,8,9,10}
{1,2,3,4,7,10,9,8,5,6,11}
{1,2,3,4,7,10,9,8,5,6,11,12}
{1,2,3,4,7,6,5,12,11,8,9,10,13}
{1,2,3,4,7,6,13,10,9,8,11,12,5,14}
{1,2,3,4,7,6,13,10,9,8,11,12,5,14,15}
{1,2,3,4,7,6,5,12,11,8,15,14,9,10,13,16}
{1,2,3,4,7,6,5,12,11,8,9,10,13,16,15,14,17}
{1,2,3,4,7,6,5,8,9,10,13,16,15,14,17,12,11,18}
{1,2,3,4,7,6,5,8,9,10,13,16,15,14,17,12,11,18,19}
{1,2,3,4,7,6,5,8,9,10,13,16,15,14,17,12,19,18,11,20}
 
{1, 1, 1, 1, 1, 2, 4, 7, 24, 80, 216, 648, 1304, 3392, 13808, 59448, 155464, 480728, 1588162}</pre>
 
=={{header|Perl}}==
1,111

edits