-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
29 lines (23 loc) · 825 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
* Find the largest palindrome made from the product of two 3-digit numbers.
*
* Reference: https://projecteuler.net/problem=4
*/
export default function palindromeProduct(from: number, to: number): number {
const isPalindrome = (input): boolean => {
let inputString: string = input.toString();
for (let i = 0; i < inputString.length / 2; i++) {
if (inputString[i] != inputString[inputString.length - i - 1]) return false;
}
return inputString.length > 1;
}
const stash = [];
for (let i = from; i <= to; i++) {
for (let k = from; k < to; k++) {
let product = i * k;
if (isPalindrome(product)) stash.push(product);
}
}
return Math.max(...stash);
}