Javascript中最大的回文产品 [英] Largest palindrome product in Javascript

查看:158
本文介绍了Javascript中最大的回文产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回文数字读取相同的方式。由两个两位数字产生的最大回文是9009 = 91×99。找到由两个三位数字的乘积制成的最大的回文。



我做了这个代码找到解决方案,但是在Project Euler网站上的答案仍然不正确:

pre > function Palindromic(x){
var pal = parseInt(x.toString()。split('')。reverse()。join(''));

if(pal === x)
return true;
else
返回false;
}

var x = 100,
y = 100,
product = x * y; (x;x≤999; x ++){
for(y = x; y <= 999; y ++){
product = x * y;


;
if(Palindromic(product)){
console.log(x +'*'+ y +'='+ product);





$ p有没有在我的问题码?!
无论如何,我得到的答案是888888从924 * 962

解决方案

我不认为,一个真正的问题与您的代码。你只是不过滤最大的产品,这不一定是你最后的输出。只需为最大的产品添加额外的支票,例如像这样:

  var x,y,product,max = 0;对于(y = x;y≤999; y ++){
产品= x * b),

(x = 100;x≤999; x ++) Ÿ;
if(Palindromic(product)){
if(max< product){//这是新的
max = product;
console.log(x +'*'+ y +'='+ product);






$返回


913 * 993 = 906609

作为最大的结果。


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.

i made this code to find the solution but the answer in Project Euler website still incorrect:

function Palindromic(x) {
    var pal = parseInt(x.toString().split('').reverse().join(''));

    if (pal === x)
        return true;
    else
        return false;
}

var x = 100,
    y = 100,
    product = x * y;

for (x; x <= 999; x++) {
    for (y = x; y <= 999; y++) {
        product = x * y;
        if (Palindromic(product)) {
            console.log(x + '*' + y + '=' + product);
        }
    }
}

Is there a problem in my code?! anyway, the answer that i got was 888888 from 924*962

解决方案

I don't think, there is a real problem with your code. You just do not filter for the largest product, which is not necessarily your last output. Just add an additional check for the largest product, e.g. like this:

var x, y, product, max = 0;

for (x = 100; x <= 999; x++) {
    for (y = x; y <= 999; y++) {
        product = x * y;
        if (Palindromic(product)) {
          if( max < product ) { // this is new
            max = product;
            console.log(x + '*' + y + '=' + product);
          }
        }
    }
}

This returns

913*993=906609

as the largest result.

这篇关于Javascript中最大的回文产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆