Task: Zumkeller numbers


C++: #include <iostream">
#include <cmath>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <numeric>

using namespace std;


const uint* binary(uint n, uint length);



uint sum_subset_unrank_bin(const vector<uint>& d, uint r);

vector<uint> factors(uint x);

bool isPrime(uint number);

bool isZum(uint n);

ostream& operator<<(ostream& os, const vector<uint>& zumz) {
    for (uint i = 0; i < zumz.size(); i++) {
        if (i % 10 == 0)
            os << endl;
        os << setw(10) << zumz[i] << ' ';
    }
    return os;
}

int main() {
    cout << "First 220 Zumkeller numbers:" << endl;
    vector<uint> zumz;
    for (uint n = 2; zumz.size() < 220; n++)
        if (isZum(n))
            zumz.push_back(n);
    cout << zumz << endl << endl;

    cout << "First 40 odd Zumkeller numbers:" << endl;
    vector<uint> zumz2;
    for (uint n = 2; zumz2.size() < 40; n++)
        if (n % 2 && isZum(n))
            zumz2.push_back(n);
    cout << zumz2 << endl << endl;

    cout << "First 40 odd Zumkeller numbers not ending in 5:" << endl;
    vector<uint> zumz3;
    for (uint n = 2; zumz3.size() < 40; n++)
        if (n % 2 && (n % 10) !=  5 && isZum(n))
            zumz3.push_back(n);
    cout << zumz3 << endl << endl;

    return 0;
}


const uint* binary(uint n, uint length) {
    uint* bin = new uint[length];	    
    fill(bin, bin + length, 0);         
    
    for (uint i = 0; n > 0; i++) {
        uint rem = n % 2;
        n /= 2;
        if (rem)
            bin[length - 1 - i] = 1;
    }

    return bin;
}



uint sum_subset_unrank_bin(const vector<uint>& d, uint r) {
    vector<uint> subset;
    
    const uint* bits = binary(r, d.size() - 1);

    
    for (uint i = 0; i < d.size() - 1; i++)
        if (bits[i])
            subset.push_back(d[i]);

    delete[] bits;

    return accumulate(subset.begin(), subset.end(), 0u);
}

vector<uint> factors(uint x) {
    vector<uint> result;
    
    for (uint i = 1; i * i <= x; i++) {
        
        if (x % i == 0) {
            result.push_back(i);

            if (x / i != i)
                result.push_back(x / i);
        }
    }

    
    sort(result.begin(), result.end());
    return result;
}

bool isPrime(uint number) {
    if (number < 2) return false;
    if (number == 2) return true;
    if (number % 2 == 0) return false;
    for (uint i = 3; i * i <= number; i += 2)
        if (number % i == 0) return false;

    return true;
}

bool isZum(uint n) {
    
    if (isPrime(n))
        return false;

    
    const auto d = factors(n);
    uint s = accumulate(d.begin(), d.end(), 0u);

    
    if (s % 2 || s < 2 * n)
        return false;

    
    
    
    if (n % 2 || d.size() >= 24)
        return true;

    if (!(s % 2) && d[d.size() - 1] <= s / 2)
        for (uint x = 2; (uint) log2(x) < (d.size() - 1); x++) 
            if (sum_subset_unrank_bin(d, x) == s / 2)
                return true; 

    
    return false;
}







VB: Module Module1
    Function GetDivisors(n As Integer) As List(Of Integer)
        Dim divs As New List(Of Integer) From {
            1, n
        }
        Dim i = 2
        While i * i <= n
            If n Mod i = 0 Then
                Dim j = n \ i
                divs.Add(i)
                If i <> j Then
                    divs.Add(j)
                End If
            End If
            i += 1
        End While
        Return divs
    End Function

    Function IsPartSum(divs As List(Of Integer), sum As Integer) As Boolean
        If sum = 0 Then
            Return True
        End If
        Dim le = divs.Count
        If le = 0 Then
            Return False
        End If
        Dim last = divs(le - 1)
        Dim newDivs As New List(Of Integer)
        For i = 1 To le - 1
            newDivs.Add(divs(i - 1))
        Next
        If last > sum Then
            Return IsPartSum(newDivs, sum)
        End If
        Return IsPartSum(newDivs, sum) OrElse IsPartSum(newDivs, sum - last)
    End Function

    Function IsZumkeller(n As Integer) As Boolean
        Dim divs = GetDivisors(n)
        Dim sum = divs.Sum()
        REM if sum is odd can
        If sum Mod 2 = 1 Then
            Return False
        End If
        REM if n is odd use 
        If n Mod 2 = 1 Then
            Dim abundance = sum - 2 * n
            Return abundance > 0 AndAlso abundance Mod 2 = 0
        End If
        REM if n and sum are both even check if there
        Return IsPartSum(divs, sum \ 2)
    End Function

    Sub Main()
        Console.WriteLine("The first 220 Zumkeller numbers are:")
        Dim i = 2
        Dim count = 0
        While count < 220
            If IsZumkeller(i) Then
                Console.Write("{0,3} ", i)
                count += 1
                If count Mod 20 = 0 Then
                    Console.WriteLine()
                End If
            End If
            i += 1
        End While
        Console.WriteLine()

        Console.WriteLine("The first 40 odd Zumkeller numbers are:")
        i = 3
        count = 0
        While count < 40
            If IsZumkeller(i) Then
                Console.Write("{0,5} ", i)
                count += 1
                If count Mod 10 = 0 Then
                    Console.WriteLine()
                End If
            End If
            i += 2
        End While
        Console.WriteLine()

        Console.WriteLine("The first 40 odd Zumkeller numbers which don
        i = 3
        count = 0
        While count < 40
            If i Mod 10 <> 5 AndAlso IsZumkeller(i) Then
                Console.Write("{0,7} ", i)
                count += 1
                If count Mod 8 = 0 Then
                    Console.WriteLine()
                End If
            End If
            i += 2
        End While
    End Sub
End Module