Task: Rhonda numbers


C++: #include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>

int digit_product(int base, int n) {
    int product = 1;
    for (; n != 0; n /= base)
        product *= n % base;
    return product;
}

int prime_factor_sum(int n) {
    int sum = 0;
    for (; (n & 1) == 0; n >>= 1)
        sum += 2;
    for (int p = 3; p * p <= n; p += 2)
        for (; n % p == 0; n /= p)
            sum += p;
    if (n > 1)
        sum += n;
    return sum;
}

bool is_prime(int n) {
    if (n < 2)
        return false;
    if (n % 2 == 0)
        return n == 2;
    if (n % 3 == 0)
        return n == 3;
    for (int p = 5; p * p <= n; p += 4) {
        if (n % p == 0)
            return false;
        p += 2;
        if (n % p == 0)
            return false;
    }
    return true;
}

bool is_rhonda(int base, int n) {
    return digit_product(base, n) == base * prime_factor_sum(n);
}

std::string to_string(int base, int n) {
    assert(base <= 36);
    static constexpr char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    std::string str;
    for (; n != 0; n /= base)
        str += digits[n % base];
    std::reverse(str.begin(), str.end());
    return str;
}

int main() {
    const int limit = 15;
    for (int base = 2; base <= 36; ++base) {
        if (is_prime(base))
            continue;
        std::cout << "First " << limit << " Rhonda numbers to base " << base
                  << ":\n";
        int numbers[limit];
        for (int n = 1, count = 0; count < limit; ++n) {
            if (is_rhonda(base, n))
                numbers[count++] = n;
        }
        std::cout << "In base 10:";
        for (int i = 0; i < limit; ++i)
            std::cout << ' ' << numbers[i];
        std::cout << "\nIn base " << base << ':';
        for (int i = 0; i < limit; ++i)
            std::cout << ' ' << to_string(base, numbers[i]);
        std::cout << "\n\n";
    }
}



Go: package main

import (
    "fmt"
    "rcu"
    "strconv"
)

func contains(a []int, n int) bool {
    for _, e := range a {
        if e == n {
            return true
        }
    }
    return false
}

func main() {
    for b := 2; b <= 36; b++ {
        if rcu.IsPrime(b) {
            continue
        }
        count := 0
        var rhonda []int
        for n := 1; count < 15; n++ {
            digits := rcu.Digits(n, b)
            if !contains(digits, 0) {
                var anyEven = false
                for _, d := range digits {
                    if d%2 == 0 {
                        anyEven = true
                        break
                    }
                }
                if b != 10 || (contains(digits, 5) && anyEven) {
                    calc1 := 1
                    for _, d := range digits {
                        calc1 *= d
                    }
                    calc2 := b * rcu.SumInts(rcu.PrimeFactors(n))
                    if calc1 == calc2 {
                        rhonda = append(rhonda, n)
                        count++
                    }
                }
            }
        }
        if len(rhonda) > 0 {
            fmt.Printf("\nFirst 15 Rhonda numbers in base %d:\n", b)
            rhonda2 := make([]string, len(rhonda))
            counts2 := make([]int, len(rhonda))
            for i, r := range rhonda {
                rhonda2[i] = fmt.Sprintf("%d", r)
                counts2[i] = len(rhonda2[i])
            }
            rhonda3 := make([]string, len(rhonda))
            counts3 := make([]int, len(rhonda))
            for i, r := range rhonda {
                rhonda3[i] = strconv.FormatInt(int64(r), b)
                counts3[i] = len(rhonda3[i])
            }
            maxLen2 := rcu.MaxInts(counts2)
            maxLen3 := rcu.MaxInts(counts3)
            maxLen := maxLen2
            if maxLen3 > maxLen {
                maxLen = maxLen3
            }
            maxLen++
            fmt.Printf("In base 10: %*s\n", maxLen, rhonda2)
            fmt.Printf("In base %-2d: %*s\n", b, maxLen, rhonda3)
        }
    }
}