Task: Addition chains


C++: #include <iostream>
#include <tuple>
#include <vector>

std::pair<int, int> tryPerm(int, int, const std::vector<int>&, int, int);

std::pair<int, int> checkSeq(int pos, const std::vector<int>& seq, int n, int minLen) {
    if (pos > minLen || seq[0] > n) return { minLen, 0 };
    else if (seq[0] == n)           return { pos, 1 };
    else if (pos < minLen)          return tryPerm(0, pos, seq, n, minLen);
    else                            return { minLen, 0 };
}

std::pair<int, int> tryPerm(int i, int pos, const std::vector<int>& seq, int n, int minLen) {
    if (i > pos) return { minLen, 0 };

    std::vector<int> seq2{ seq[0] + seq[i] };
    seq2.insert(seq2.end(), seq.cbegin(), seq.cend());
    auto res1 = checkSeq(pos + 1, seq2, n, minLen);
    auto res2 = tryPerm(i + 1, pos, seq, n, res1.first);

    if (res2.first < res1.first)       return res2;
    else if (res2.first == res1.first) return { res2.first, res1.second + res2.second };
    else                               throw std::runtime_error("tryPerm exception");
}

std::pair<int, int> initTryPerm(int x) {
    return tryPerm(0, 0, { 1 }, x, 12);
}

void findBrauer(int num) {
    auto res = initTryPerm(num);
    std::cout << '\n';
    std::cout << "N = " << num << '\n';
    std::cout << "Minimum length of chains: L(n)= " << res.first << '\n';
    std::cout << "Number of minimum length Brauer chains: " << res.second << '\n';
}

int main() {
    std::vector<int> nums{ 7, 14, 21, 29, 32, 42, 64, 47, 79, 191, 382, 379 };
    for (int i : nums) {
        findBrauer(i);
    }

    return 0;
}







VB: Module Module1

    Function Prepend(n As Integer, seq As List(Of Integer)) As List(Of Integer)
        Dim result As New List(Of Integer) From {
            n
        }
        result.AddRange(seq)
        Return result
    End Function

    Function CheckSeq(pos As Integer, seq As List(Of Integer), n As Integer, min_len As Integer) As Tuple(Of Integer, Integer)
        If pos > min_len OrElse seq(0) > n Then
            Return Tuple.Create(min_len, 0)
        End If
        If seq(0) = n Then
            Return Tuple.Create(pos, 1)
        End If
        If pos < min_len Then
            Return TryPerm(0, pos, seq, n, min_len)
        End If
        Return Tuple.Create(min_len, 0)
    End Function

    Function TryPerm(i As Integer, pos As Integer, seq As List(Of Integer), n As Integer, min_len As Integer) As Tuple(Of Integer, Integer)
        If i > pos Then
            Return Tuple.Create(min_len, 0)
        End If

        Dim res1 = CheckSeq(pos + 1, Prepend(seq(0) + seq(i), seq), n, min_len)
        Dim res2 = TryPerm(i + 1, pos, seq, n, res1.Item1)

        If res2.Item1 < res1.Item1 Then
            Return res2
        End If
        If res2.Item1 = res1.Item1 Then
            Return Tuple.Create(res2.Item1, res1.Item2 + res2.Item2)
        End If

        Throw New Exception("TryPerm exception")
    End Function

    Function InitTryPerm(x As Integer) As Tuple(Of Integer, Integer)
        Return TryPerm(0, 0, New List(Of Integer) From {1}, x, 12)
    End Function

    Sub FindBrauer(num As Integer)
        Dim res = InitTryPerm(num)
        Console.WriteLine("N = {0}", num)
        Console.WriteLine("Minimum length of chains: L(n) = {0}", res.Item1)
        Console.WriteLine("Number of minimum length Brauer chains: {0}", res.Item2)
        Console.WriteLine()
    End Sub

    Sub Main()
        Dim nums() = {7, 14, 21, 29, 32, 42, 64, 47, 79, 191, 382, 379}
        Array.ForEach(nums, Sub(n) FindBrauer(n))
    End Sub

End Module