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

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

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

推荐阅读:
作坊“老字号”竟用工业盐腌制烤鸭  网传男人应远离的八大败“性”食物  导致食物致癌多是人为因素造成  颜色鲜亮的枸杞可能导致肝肾损伤  不是所有食物都适合用保鲜膜“保鲜”  新鲜水果的最佳食用时间是何时?  食用水果时应该注意的一些问题  男人不能常喝豆浆的传言荒谬至极  健康饮食:哪些人不宜食用豆制品?  饮食保健:如何补充膳食纤维合适? 
评论列表
添加评论