Find the largest palindrome From the product of two 3-digit numb

  • Time:2020-09-10 12:55:33
  • Class:Weblog
  • Read:43

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.

To check if a number in Javascript is palindrome, we can convert it to String, then split into char array, reverse the array, and join as a string, then a palindrome is a string that its reverse is the same.

1
2
3
4
5
6
7
8
9
10
11
12
13
let ans = 0;
for (let i = 999; i >= 100; i --) {
    for (let j = 999; j >= 100; j --) {
        let num = i * j;
        let s = String(num);
        let rs = s.split('').reverse().join('');
        if (s === rs) {
            ans = Math.max(ans, num);
        }
    }
}
 
console.log(ans);
let ans = 0;
for (let i = 999; i >= 100; i --) {
    for (let j = 999; j >= 100; j --) {
        let num = i * j;
        let s = String(num);
        let rs = s.split('').reverse().join('');
        if (s === rs) {
            ans = Math.max(ans, num);
        }
    }
}

console.log(ans);

Two loops each range from 100 to 999 for 3-digit number. Then we check the product and record the maximum palindrome.

The answer is: 906609.

–EOF (The Ultimate Computing & Technology Blog) —

Recommend:
8 Ways To Get A Perfect Site Health Score In WordPress
The Right (and Wrong) Ways to Approach Controversy in Blogging
7 Ways To Take Advantage of 404 Error Pages
7 Ways to Enhance Your Blog with Video Advertising
California Consumer Privacy Act (CCPA) Compliance Guide For AdSe
What Is WordPress Live Chat and How Do You Implement It?
5 Beginner’s Tips to Building a WordPress Blog
8 Best Affiliate Marketing Plugins For Your Blog
How Many Squares/Rectangles Does a Rubik Cube Have?
Find the 10001st Prime Number
Share:Facebook Twitter
Comment list
Comment add