Task: Prime triangle





Java: public class PrimeTriangle {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        for (int i = 2; i <= 20; ++i) {
            int[] a = new int[i];
            for (int j = 0; j < i; ++j)
                a[j] = j + 1;
            if (findRow(a, 0, i))
                printRow(a);                
        }
        System.out.println();
        StringBuilder s = new StringBuilder();
        for (int i = 2; i <= 20; ++i) {
            int[] a = new int[i];
            for (int j = 0; j < i; ++j)
                a[j] = j + 1;
            if (i > 2)
                s.append(" ");
            s.append(countRows(a, 0, i));
        }
        System.out.println(s);
        long finish = System.currentTimeMillis();
        System.out.printf("\nElapsed time: %d milliseconds\n", finish - start);
    }

    private static void printRow(int[] a) {
        for (int i = 0; i < a.length; ++i) {
            if (i != 0)
                System.out.print(" ");
            System.out.printf("%2d", a[i]);
        }
        System.out.println();
    }

    private static boolean findRow(int[] a, int start, int length) {
        if (length == 2)
            return isPrime(a[start] + a[start + 1]);
        for (int i = 1; i + 1 < length; i += 2) {
            if (isPrime(a[start] + a[start + i])) {
                swap(a, start + i, start + 1);
                if (findRow(a, start + 1, length - 1))
                    return true;
                swap(a, start + i, start + 1);
            }
        }
        return false;
    }

    private static int countRows(int[] a, int start, int length) {
        int count = 0;
        if (length == 2) {
            if (isPrime(a[start] + a[start + 1]))
                ++count;
        } else {
            for (int i = 1; i + 1 < length; i += 2) {
                if (isPrime(a[start] + a[start + i])) {
                    swap(a, start + i, start + 1);
                    count += countRows(a, start + 1, length - 1);
                    swap(a, start + i, start + 1);
                }
            }
        }
        return count;
    }

    private static void swap(int[] a, int i, int j) {
        int tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }

    private static boolean isPrime(int n) {
        return ((1L << n) & 0x28208a20a08a28acL) != 0;
    }
}




VB: Option Strict On
Option Explicit On

Imports System.IO


Module vMain

    Public Const maxNumber As Integer = 20 
    Dim prime(2 * maxNumber) As Boolean    

    
    Public Function countArrangements(ByVal n As Integer) As Integer
        If n < 2 Then 
            Return 0
        ElseIf n < 4 Then 
            
            For i As Integer = 1 To n
                Console.Out.Write(i.ToString.PadLeft(3))
            Next i
            Console.Out.WriteLine()
            Return 1
        Else
            
            Dim printSolution As Boolean = true
            Dim used(n) As Boolean
            Dim number(n) As Integer
            
            
            For i As Integer = 0 To n - 1
                number(i) = i Mod 2
            Next i
            used(1) = True
            number(n) = n
            used(n) = True
            
            Dim count As Integer = 0
            Dim p As Integer = 2
            Do While p > 0
                Dim p1 As Integer = number(p - 1)
                Dim current As Integer = number(p)
                Dim [next] As Integer = current + 2
                Do While [next] < n AndAlso (Not prime(p1 + [next]) Or used([next]))
                    [next] += 2
                Loop
                If [next] >= n Then
                    [next] = 0
                End If
                If p = n - 1 Then
                    
                    
                    If [next] <> 0 Then
                        
                        If prime([next] + n) Then
                            
                            count += 1
                            If printSolution Then
                                For i As Integer = 1 To n - 2
                                     Console.Out.Write(number(i).ToString.PadLeft(3))
                                Next i
                                Console.Out.WriteLine([next].ToString.PadLeft(3) & n.ToString.PadLeft(3))
                                printSolution = False
                            End If
                        End If
                        [next] = 0
                    End If
                    
                    p -= 1
                    
                End If
                If [next] <> 0 Then
                    
                    used(current) = False
                    used([next]) = True
                    number(p) = [next]
                    
                    p += 1
                ElseIf p <= 2 Then
                    
                    p = 0
                Else
                    
                    used(number(p)) = False
                    number(p) = p Mod 2
                    p -= 1
                End If
            Loop
            Return count
        End If
    End Function

    Public Sub Main
        prime(2) = True
        For i As Integer = 3 To UBound(prime) Step  2
            prime(i) = True
        Next i
        For i As Integer = 3 To Convert.ToInt32(Math.Floor(Math.Sqrt(Ubound(prime)))) Step 2
            If prime(i) Then
                For s As Integer = i * i To Ubound(prime) Step i + i
                    prime(s) = False
                Next s
            End If
        Next i

        Dim  arrangements(maxNumber) As Integer
        For n As Integer = 2 To UBound(arrangements)
            arrangements(n) = countArrangements(n)
        Next n
        For n As Integer = 2 To UBound(arrangements)
            Console.Out.Write(" " & arrangements(n))
        Next n
        Console.Out.WriteLine()

    End Sub

End Module