Identify the first 25 numbers who's reverse plus their own adds up to a palindrome and the number is greater than 1000.
- This will need to be a funciton so that when it is called, I can return those numbers.
- I can keep the variable I need to return global if i want to because no rules have been made about scope.
- It will be easier to convert numbers to strings and arrays for the initial solution. Because an array can be held in a single return statement but have all the values I need
- I am going to need a way to count how many numbers I have so I know when to stop the function.
- off hand, the first number I want to evaluate is 99 because I know for a fact every number below that + its reverse will not be equal to 1000. I can change that number to the lowest return value though once I have this working. That will reduce Big O complexity.
There will be one major conditions that defines if I keep looking for numbers or return my final value. It will involve a lot of litter mutator methods and addition to evaluate those conditions. I think I can use recursion instead of a loop as long as I keep one variable out of the function to keep track.
- Math - to figure out the palindrome
- Optimization - So that something stops my recursion or loop eventually.
I will use multiple arrays.
- One array to hold the values I will return
- Arrays to convert the numbers and reverse them to see if they are a palindrome
I will convert numbers into strings to get those numbers pushed into arrays one character at a time. I will then need to parse those strings to get them back into numbers for re-evaluation
- Create a global variable assigned to an empty array
- Create a function and call it with the number
99
- Get the reverse number of the param
- Create a variable assigned to the sum of the param and its reverse
- Create a variable assigned to the reverse of the sum
- Create a condition where if the reverse sum and the sum are the same and the sum is greater than 1000
- push the param into the global variable.
- Create a new condtion that either triggers recursion or returns the global variable
- That condition is based on whether or not the length of the global variable is 25.
const getPalindromicNums = (numToEval, palindromicNums) => {
const sum = numToEval + getReversedNum(numToEval)
const reversedSum = getReversedNum(sum)
if (reversedSum === sum && sum > 1000) {
palindromicNums.push(numToEval)
}
if (palindromicNums.length === 25) {
return palindromicNums
} else {
return getPalindromicNums(numToEval + 1, palindromicNums)
}
}
const getReversedNum = (number) => {
return parseInt(number.toString().split('').reverse().join(''))
}
getPalindromicNums(209, [])
I don't understand the Big O complexity of a recursion, although I would imagine it is less than if I created a for loop for this. I am technically iterating through my global variable at each function invocation but that will never be more than 25. One thing I did to reduce complexity was change my initial function invocation from 99
to 209
because that was the actual first number who's reverse sum was a palindrome.