re: Daily Challenge #7 - Factorial Decomposition VIEW POST

FULL DISCUSSION
 

Solution

const revexp = (x, y) => x % y ? 0 : 1 + revexp(x/y, y);
const decomp = n => {
    let seive = Array(n+1).fill(1);
    for (let i=2; i<=n; i++) {
        if (seive[i]) for(let j=2*i; j<=n; j+=i) {
            seive[j] = 0;
            seive[i] += revexp(j, i);
        }
    }
    return seive
        .slice(2)
        .map((exp, ind) => exp > 1 ? `${ind+2}^${exp}` : exp ? `${ind+2}` : '')
        .filter(x => !!x)
        .join(' * ');
}

Test

const test = require('./tester');
const decomp = require('./challenge-7');
test (decomp, [
    {
        in: [12],
        out: '2^10 * 3^5 * 5^2 * 7 * 11'
    },
    {
        in: [22],
        out: '2^19 * 3^9 * 5^4 * 7^3 * 11^2 * 13 * 17 * 19'
    },
    {
        in: [25],
        out:'2^22 * 3^10 * 5^6 * 7^3 * 11^2 * 13 * 17 * 19 * 23'
    }
]);

Result

[PASSED]  Case #1: 2^10 * 3^5 * 5^2 * 7 * 11
[PASSED]  Case #2: 2^19 * 3^9 * 5^4 * 7^3 * 11^2 * 13 * 17 * 19
[PASSED]  Case #3: 2^22 * 3^10 * 5^6 * 7^3 * 11^2 * 13 * 17 * 19 * 23
code of conduct - report abuse