Javascript中最大的回文产品 [英] Largest palindrome product in Javascript
问题描述
我做了这个代码找到解决方案,但是在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屋!