Task: Strange unique prime triplets



C#: 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; } }






VB: Imports DT = System.DateTime

Module Module1

    Iterator Function Primes(lim As Integer) As IEnumerable(Of Integer)
        Dim flags(lim) As Boolean

        Dim j = 2

        Dim d = 3
        Dim sq = 4
        While sq <= lim
            If Not flags(j) Then
                Yield j
                For k = sq To lim Step j
                    flags(k) = True
                Next
            End If

            j += 1
            d += 2
            sq += d
        End While

        While j <= lim
            If Not flags(j) Then
                Yield j
            End If
            j += 1
        End While
    End Function

    Sub Main()
        For Each lmt In {90, 300, 3000, 30000, 111000}
            Dim pr = Primes(lmt).Skip(1).ToList()
            Dim st = DT.Now
            Dim f = 0
            Dim r As New List(Of String)
            Dim i = -1
            Dim m = lmt \ 3
            Dim h = m
            While i < 0
                i = pr.IndexOf(h)
                h -= 1
            End While
            Dim j = i - 1
            Dim k = j - 1
            For a = 0 To k
                Dim pra = pr(a)
                For b = a + 1 To j
                    Dim prab = pra + pr(b)
                    For c = b + 1 To i
                        Dim d = prab + pr(c)
                        If Not pr.Contains(d) Then
                            Continue For
                        End If
                        f += 1
                        If lmt < 100 Then
                            r.Add(String.Format("{3,5} = {0,2} + {1,2} + {2,2}", pra, pr(b), pr(c), d))
                        End If
                    Next
                Next
            Next
            Dim s = "s.u.p.t.s under "
            r.Sort()
            If r.Count > 0 Then
                Console.WriteLine("{0}{1}:" + vbNewLine + "{2}", s, m, String.Join(vbNewLine, r))
            End If
            If lmt > 100 Then
                Console.WriteLine("Count of {0}{1,6:n0}: {2,13:n0}  {3} sec", s, m, f, (DT.Now - st).ToString().Substring(6))
            End If
        Next
    End Sub

End Module