Solution for Euler Problem 36

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.`);

Hello there.

Thank you, for your contribution. For future contributions, please wrap your solution within :

[details]
```
code goes here...
```
[/details]

Also, provide all of the necessary code to pass the challenge.

Also, when you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor ( </> ) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

That’s definitely not an answer we can put up as something for others to read and understand unfortunately. It’s too hard to parse.

The idea is a good one though and likely can be represented simpler.

I think I’ve captured the essence of your solution and posted it as a new solution. What do you think of it?

Yeah, that is great! Follows the main idea of get the palindromic numbers first and then just searching through those instead. That way you only have a couple thousand things to check instead of a million. Looks great :slight_smile:

2 Likes