Strange unique prime triplets: Difference between revisions
Content added Content deleted
Catskill549 (talk | contribs) |
(Added C# version) |
||
Line 199: | Line 199: | ||
Unique prime triples 2-999 which sum to a prime: 241,580 |
Unique prime triples 2-999 which sum to a prime: 241,580 |
||
</pre> |
|||
=={{header|C#|CSharp}}== |
|||
Just for fun, <30 sorted by sum, instead of order generated. |
|||
<lang csharp>using System; using System.Collections.Generic; using static System.Console; using System.Linq; using DT = System.DateTime; |
|||
class Program { static void Main(string[] args) { string s; |
|||
foreach (int lmt in new int[]{ 90, 300, 3000, 30000, 111000 }) { |
|||
var pr = PG.Primes(lmt).Skip(1).ToList(); DT st = DT.Now; |
|||
int d, f = 0; var r = new List<string>(); |
|||
int i = -1, m, h = (m = lmt / 3), j, k, pra, prab; |
|||
while (i < 0) i = pr.IndexOf(h--); k = (j = i - 1) - 1; |
|||
for (int a = 0; a <= k; a++) { pra = pr[a]; |
|||
for (int b = a + 1; b <= j; b++) { prab = pra + pr[b]; |
|||
for (int c = b + 1; c <= i; c++) { |
|||
if (PG.flags[d = prab + pr[c]]) continue; f++; |
|||
if (lmt < 100) r.Add(string.Format("{3,5} = {0,2} + {1,2} + {2,2}", pra, pr[b], pr[c], d)); } } } s = "s.u.p.t.s under "; |
|||
r.Sort(); if (r.Count > 0) WriteLine("{0}{1}:\n{2}", s, m, string.Join("\n", r)); |
|||
if (lmt > 100) WriteLine("Count of {0}{1,6:n0}: {2,13:n0} {3} sec", s, m, f, (DT.Now - st).ToString().Substring(6)); } } } |
|||
class PG { public static bool[] flags; |
|||
public static IEnumerable<int> Primes(int lim) { |
|||
flags = new bool[lim + 1]; int j = 2; |
|||
for (int d = 3, sq = 4; sq <= lim; j++, sq += d += 2) |
|||
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; } }</lang> |
|||
{{out}} |
|||
Timings from tio.run |
|||
<pre>s.u.p.t.s under 30: |
|||
19 = 3 + 5 + 11 |
|||
23 = 3 + 7 + 13 |
|||
23 = 5 + 7 + 11 |
|||
29 = 3 + 7 + 19 |
|||
29 = 5 + 7 + 17 |
|||
29 = 5 + 11 + 13 |
|||
31 = 3 + 5 + 23 |
|||
31 = 3 + 11 + 17 |
|||
31 = 5 + 7 + 19 |
|||
31 = 7 + 11 + 13 |
|||
37 = 3 + 5 + 29 |
|||
37 = 3 + 11 + 23 |
|||
37 = 5 + 13 + 19 |
|||
37 = 7 + 11 + 19 |
|||
37 = 7 + 13 + 17 |
|||
41 = 5 + 7 + 29 |
|||
41 = 5 + 13 + 23 |
|||
41 = 5 + 17 + 19 |
|||
41 = 7 + 11 + 23 |
|||
41 = 11 + 13 + 17 |
|||
43 = 3 + 11 + 29 |
|||
43 = 3 + 17 + 23 |
|||
43 = 7 + 13 + 23 |
|||
43 = 7 + 17 + 19 |
|||
43 = 11 + 13 + 19 |
|||
47 = 5 + 13 + 29 |
|||
47 = 5 + 19 + 23 |
|||
47 = 7 + 11 + 29 |
|||
47 = 7 + 17 + 23 |
|||
47 = 11 + 13 + 23 |
|||
47 = 11 + 17 + 19 |
|||
53 = 5 + 19 + 29 |
|||
53 = 7 + 17 + 29 |
|||
53 = 11 + 13 + 29 |
|||
53 = 11 + 19 + 23 |
|||
53 = 13 + 17 + 23 |
|||
59 = 7 + 23 + 29 |
|||
59 = 11 + 19 + 29 |
|||
59 = 13 + 17 + 29 |
|||
59 = 17 + 19 + 23 |
|||
61 = 13 + 19 + 29 |
|||
71 = 19 + 23 + 29 |
|||
Count of s.u.p.t.s under 100: 891 00.0000243 sec |
|||
Count of s.u.p.t.s under 1,000: 241,580 00.0054753 sec |
|||
Count of s.u.p.t.s under 10,000: 74,588,542 01.8159964 sec |
|||
Count of s.u.p.t.s under 37,000: 2,141,379,201 55.0369689 sec |
|||
</pre> |
</pre> |
||