Not sure if this is the correct place to post this, and apologies if it isn’t. Regarding the solution that is listed in the hints section. It works, but it isn’t the most efficient, because it requires every single number to be checked. It is much more efficient (I am seeing the function perform in about 4 ms, vs the listed solution, close to 100ms, for n = 1000000) to come up with every palindromic number first and check only those to see if the binary is also palindromic. I’ve copied my solution below. Apologies, I am new to this, only been coding for about a year, and really only doing it on a regular basis for the past 4 months, so the code itself probably isn’t the prettiest (if anybody has any suggestions on simplifying it, I’m sure this could be rewritten better). And again, apologies if this is not the correct section to post this.
Summary
function doubleBasePalindromes(n) {
let answerArr
if (n < 10) {
answerArr = [];
for (let i = 1; i <= n; i++) {
answerArr.push(i)
} console.log(answerArr)
} else {
let baseNum = 11
answerArr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
recursivePart3(n)
function recursivePart3(n) {
if (baseNum + 1 > n) return;
let arr = Array.from(String(baseNum), Number);
let i = 0;
recursivePart(arr, i)
function recursivePart(arr, i) {
if (i + 1 <= Math.ceil(arr.length/2) - 1) {
recursivePart(arr, i + 1)
} else {
while (arr[i] < 10) {
let answer = Number(arr.join(""))
if (answer > n) return;
answerArr.push(answer)
if (i === arr.length - 1 - i) {
arr[i]++
} else {
arr[i]++
arr[arr.length - 1 - i]++
}
}
arr[i] = 0;
arr[arr.length - 1 - i] = 0
recursivePart2(arr, i)
}
}
function recursivePart2(arr, i) {
if (i - 1 < 0) {
return;
} else {
i -= 1
if (arr[i] < 9) {
arr[i]++
arr[arr.length - 1 - i]++
recursivePart(arr, i)
} else {
arr[i] = 0
arr[arr.length - 1 - i] = 0
recursivePart2(arr, i)
}
}
} baseNum = answerArr[answerArr.length - 1] + 2
recursivePart3(n)
}
}
answerArr = answerArr.filter(elem => elem.toString(2) === elem.toString(2).split("").reverse().join(""))
let sum = 0;
for (let i = 0; i < answerArr.length; i++) {
sum += answerArr[i]
} return sum
}
const start = performance.now()
doubleBasePalindromes(1000000);
const end = performance.now();
const elapsed = end - start;
console.log(`Function took ${elapsed} milliseconds to execute.`);