Tell us what’s happening:
The program is not returning the correct solution with this layout format 1. permutationOf3SmoothNumbers() should return a string.
Failed:2. permutationOf3SmoothNumbers() should return the string 5.5350769703e1512. Instead, it’s returning 3.5 as a answer.
Your code so far
function count3SmoothNumbers(N) {
const threeSmooth = new Set();
const aMax = Math.floor(Math.log(N) / Math.log(2));
const bMax = Math.floor(Math.log(N) / Math.log(3));
for (let a = 0; a <= aMax; a++) {
for (let b = 0; b <= bMax; b++) {
const number = Math.pow(2, a) * Math.pow(3, b);
if (number <= N) {
threeSmooth.add(number);
}
}
}
return Array.from(threeSmooth).sort((a, b) => a - b);
}
function countValidPermutations(N) {
const threeSmooth = count3SmoothNumbers(N);
const length = threeSmooth.length;
// DP array to count permutations
const dp = new Array(length).fill(1);
for (let i = 1; i < length; i++) {
let count = 0;
for (let j = 0; j < i; j++) {
if (threeSmooth[i] % threeSmooth[j] === 0) {
count += dp[j];
}
}
dp[i] = count;
}
return dp.reduce((sum, value) => sum + value, 0);
}
function permutationOf3SmoothNumbers() {
const N = 10 ** 18;
const result = countValidPermutations(N);
// Ensure the result is formatted as required
return result.toExponential(10).replace('+', '');
}
console.log(permutationOf3SmoothNumbers()); // Expected: "5.5350769703e1512"
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0
Challenge Information:
Project Euler Problems 401 to 480 - Problem 462: Permutation of 3-smooth numbers