Skip to content

Instantly share code, notes, and snippets.

@holladayian
Last active December 17, 2020 17:20
Show Gist options
  • Save holladayian/3e6471fc9094ddb36d3f2c3878a1c06c to your computer and use it in GitHub Desktop.
Save holladayian/3e6471fc9094ddb36d3f2c3878a1c06c to your computer and use it in GitHub Desktop.

Problem - Palindromic Numbers

Palindrome

The number 47 has an interesting characteristic.

If you take the number, plus its reverse (47 => 74) and then add them together to a number (47+74=121) the resulting sum is a palindrome

Starting at 0, find the first 25 numbers that have this same characteristic as 47, but limit it to where the palindrome is greater than 1000.

Collect the results in an array. Be sure that you're collecting the interesting numbers like 47, not the palindromic sums.

Bonus points if you can do this without converting numbers to strings/arrays.

Limit your online searches to core language documentation only.

Instructions

  1. Copy this markdown and paste in your own, private gist
  2. In your private gist, fill out the questions below
  3. Submit by the due time as instructed in Zoom

Rewrite the question in your own words:

What assumptions will you make about this problem if you cannot ask any more clarifying questions? What are your reasons for making those assumptions?

What will you assume bout this problem and why?

I think it's safe to assume the first number pair will be greater than 100. Maybe I can start this function there to skip over the first 100 iterations of trying to find two numbers.

What are your initial thoughts about this problem? (high level design, 2-3 sentences)

How would you describe your thoughts in English?

It seems simple enough at first. The hardest part seems to be reversing the number without using strings.

How would you identify the elements of this problem?

What type of problem would you classify this as?

  • Searching of Data
  • Sorting of Data
  • Pattern Recognition
  • Build/Navigate a Grid
  • Math
  • Language API knowledge
  • Optimization

Which data structure(s) do you think you'll use? What pros/cons do you see with that choice?

How will you hold you data?

I think it would be best to hold data in an array. It says we need the first 25 numbers, so I think this is a fair assumption.

Write out a few lines of initial pseudocode: (mid-level design, NOT REAL CODE)

Explain the problem in a little more depth.

I'll need a function to reverse the number. I can use this same function to check that the total is a palindrome (against itself). If it is, I'll push the origional number into an array, to save it. I'll then need a function to run this code until there are 25 numbers in the array.

Write out any implementation code OR link to repl

What does you solution look like?

let pNums = [];
function reverseNumAsString(num) {
  return ('', + num).split('').reverse().join('')
}
function findPNums(num) {
  let total = num + reverseNumAsString(num);
  if (total == reverseNumAsString(total) && total >= 1000)
  pNums.push(num)
}
function fillPNums() {
let incrimentor = 0;
  if(pNums.length < 25) {
  findPNums(incrimentor);
  incrimentor ++
  }
}

What is the Big O complexity of your solution?

What is the largest Big O value of your functions?

I'm not 100% confident on this number, but I believe it's O(n).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment