function decompose(n) { if (typeof(n) !== 'number') throw new Error('must be an integer'); const factorial = getFactorial(n); const factors = factorize(factorial); const factorFrequencyArray = parseFactors(factors); const formattedStr = stringify(factorFrequencyArray); return formattedStr; } function stringify(exponents) { let str = ''; let i = 0; exponents.forEach(exponent => { if (exponents[i] === 1) str += `${i} * `; else if (exponents[i] !== 0) str += `${i}^${exponents[i]} * `; i++; }); return str.substring(0, str.length - 3); } // O(n) function getFactorial(n) { let factorial = 1n; for(let i = 1n; i <= n; i ++) factorial *= i; return factorial; } // O(n) function parseFactors(factors) { let exponents = Array(57).fill(0); factors.forEach(factor => exponents[factor]++); return exponents; } // O(log(n)) function factorize(n) { const primes = [2n,3n,5n,7n,11n,13n,17n,19n,23n,29n, 31n,37n,43n,47n,53n,59n,61n,67n,71n,73n,79n,83n,89n,97n]; let primeIndex = 0; let factors = []; while(!primes.includes(n)) { if (n % primes[primeIndex] === 0n) { factors.push(primes[primeIndex]); n = n / primes[primeIndex]; primeIndex = 0; } else primeIndex++; } factors.push(n); return factors; }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Discussion on: Daily Challenge #7 - Factorial Decomposition
For further actions, you may consider blocking this person and/or reporting abuse