我对两个 3 位数乘积的最大回文的解决方案需要工作
My Solution to the Largest Palindrome of the product of two 3 digit numbers needs work
我得到的答案不正确(正确答案是 906609)。请帮助我了解我哪里出错了。我希望 while 循环从 100 变为 999,同时在循环递增之前将自身乘以当前 i
值。
// 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.
var pali = [];
function palindrome() {
for (var i = 100; i <= 999; i++) {
var counter = 100;
while (counter <= 999) {
var result = counter * i;
if (result.toString() === result.toString().split("").reverse().join("")) {
pali.push(result);
}
counter++;
}
}
return pali[pali.length - 1];
}
console.log(palindrome());
如果您希望最后一个成为最高的,则必须按升序对数组进行排序:
pali.sort(function(a, b){return a-b});
使用它,我得到 906609。
更长,但更快。从最大值开始,向下工作,当我们找不到更高的值时短路:
function largestPalindromeProduct(lower, upper) {
var palindrome = 0;
var outerLow = lower;
var outer = upper;
while (outer > outerLow) {
var inner = upper;
var innerLow = lower;
while (inner > innerLow) {
var result = inner * outer;
if (result + "" === (result + "").split("").reverse().join("")) {
if (result > palindrome) {
palindrome = result;
outerLow = inner; // Don't go lower than this value in the outer, no need!
}
inner = innerLow; // short-circuit this loop
}
inner--;
}
outer--;
}
return palindrome;
}
console.log(largestPalindromeProduct(100, 999))
我认为最简单的方法是添加 If 语句。
function palindrome()
{
var max = 0;
for (var i = 999; i >= 100; i--)
{
var counter = 999;
while (counter >= 100)
{
var result = counter * i;
if (result.toString() === result.toString().split("").reverse().join(""))
{
if(result>max)
{
max = result;
}
}
counter++;
}
}
return max;
}
我得到的答案不正确(正确答案是 906609)。请帮助我了解我哪里出错了。我希望 while 循环从 100 变为 999,同时在循环递增之前将自身乘以当前 i
值。
// 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.
var pali = [];
function palindrome() {
for (var i = 100; i <= 999; i++) {
var counter = 100;
while (counter <= 999) {
var result = counter * i;
if (result.toString() === result.toString().split("").reverse().join("")) {
pali.push(result);
}
counter++;
}
}
return pali[pali.length - 1];
}
console.log(palindrome());
如果您希望最后一个成为最高的,则必须按升序对数组进行排序:
pali.sort(function(a, b){return a-b});
使用它,我得到 906609。
更长,但更快。从最大值开始,向下工作,当我们找不到更高的值时短路:
function largestPalindromeProduct(lower, upper) {
var palindrome = 0;
var outerLow = lower;
var outer = upper;
while (outer > outerLow) {
var inner = upper;
var innerLow = lower;
while (inner > innerLow) {
var result = inner * outer;
if (result + "" === (result + "").split("").reverse().join("")) {
if (result > palindrome) {
palindrome = result;
outerLow = inner; // Don't go lower than this value in the outer, no need!
}
inner = innerLow; // short-circuit this loop
}
inner--;
}
outer--;
}
return palindrome;
}
console.log(largestPalindromeProduct(100, 999))
我认为最简单的方法是添加 If 语句。
function palindrome()
{
var max = 0;
for (var i = 999; i >= 100; i--)
{
var counter = 999;
while (counter >= 100)
{
var result = counter * i;
if (result.toString() === result.toString().split("").reverse().join(""))
{
if(result>max)
{
max = result;
}
}
counter++;
}
}
return max;
}