Task: Rhonda numbers




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)
        }
    }
}


Java: public class RhondaNumbers {
    public static void main(String[] args) {
        final int limit = 15;
        for (int base = 2; base <= 36; ++base) {
            if (isPrime(base))
                continue;
            System.out.printf("First %d Rhonda numbers to base %d:\n", limit, base);
            int numbers[] = new int[limit];
            for (int n = 1, count = 0; count < limit; ++n) {
                if (isRhonda(base, n))
                    numbers[count++] = n;
            }
            System.out.printf("In base 10:");
            for (int i = 0; i < limit; ++i)
                System.out.printf(" %d", numbers[i]);
            System.out.printf("\nIn base %d:", base);
            for (int i = 0; i < limit; ++i)
                System.out.printf(" %s", Integer.toString(numbers[i], base));
            System.out.printf("\n\n");
        }
    }
    
    private static int digitProduct(int base, int n) {
        int product = 1;
        for (; n != 0; n /= base)
            product *= n % base;
        return product;
    }
     
    private static int primeFactorSum(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;
    }
     
    private static boolean isPrime(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;
    }
     
    private static boolean isRhonda(int base, int n) {
        return digitProduct(base, n) == base * primeFactorSum(n);
    }
}