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

  • 时间:2020-09-10 12:55:33
  • 分类:网络文摘
  • 阅读:126 次

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) —

推荐阅读:
数学题:哥哥和弟弟进行100米赛跑  数学题:把14分成若干个自然数的和  数学题:张王李赵刘5人合作完成一项工程  数学题:姐姐8年后的年龄是妹妹3年前的5倍  数学题:一个直角三角形以它的斜边为轴旋转一周  数学题:一个三角形被一个长方形挡住了  摘桃子的数学题  如图平行四边形ABCD的周长为72厘米  每个人都和其他人握了一次手  客车和货车同时从甲、乙两地的中点反向行驶 
评论列表
添加评论