Can anyone tell me what"s wrong through the code. Find the largest palindrome made from the product of 2 3-digit numbers.

You are watching: Find the largest palindrome made from the product of two 3-digit numbers

function largestPalindrome() for(var i =999; i>100; i--) for(var j = 999; j>100; j--) var mul = j*i; if(isPalin(mul)) return i * j; function isPalin(i) return i.toString() == i.toString().split("").reverse().join("");console.log(largestPalindrome());This answer to be close to my questionbut still ns feel the means i to be doing the loop it should return me the largest product.

javascript
re-publishing
enhance this question
monitor
edited might 23 "17 at 12:34 CommunityBot
asked jan 22 "14 at 15:21 ShaneShane
2

32
Yours doesn"t occupational properly due to the fact that it check 999*999, climate 999*998, climate 999*997 till it reaches around 999*583. While that doesn"t examine 997*995 or something closer to the topwhich generates a larger number

function largestPalindrome() var arr = <>; for(var i =999; i>100; i--) for(var j = 999; j>100; j--) var mul = j*i; if(isPalin(mul)) arr.push(j * i); return Math.max.apply(Math, arr);function isPalin(i) return i.toString() == i.toString().split("").reverse().join("");console.log(largestPalindrome());Here is another approach, store all palindrome produced by 3 numbers in one array, then usage Math.max on the selection to get the largest palindrome

re-superstructure
improve this prize
monitor
answered jan 22 "14 in ~ 16:01 ThalaivarThalaivar
7
21
I think if you apply maths to the trouble you deserve to decrease the guesswork really significantly.

I will certainly write the three digit numbers as 1000 - a and also 1000 - b which way the palindrome is 1 000 000 - 1000(a+b) + ab.

First, let"s discover solutions where ab . Then the 3 leftmost digits room 1000 - (a+b) and the three rightmost digits space ab.

See more: What Is The Sugar Found In Rna, What Sugar Is Found In Rna And Dna

Then I will say this is a palindrome with digits x,y,z:

100x+10y+z=ab100z+10y+x=1000-a-bthus

99x-99z = ab+a+b-1000x-z = 1/99(ab+a+b-10)-10So climate (ab+a+b-10) is divisible by 99 and also we additionally know the x and z being digits the left next is between -9 and 0 (the whole shebang is symmetrical so we can presume x 1/99(ab+a+b-10) is between 1 and also 9. We deserve to rewrite ab+a+b-10 as ab+a+b+1-11=99p so (a+1)(b+1)=99p+11=11*(9p+1) where p runs in between 1 and also 9. That"s yes, really easy:

for (\$p = 1; \$p currently this prints just one equipment which is the exactly one.

Now we recognize 906609 is a systems so then is there a systems where abdominal > 1000 and 1000(a+b) - ab        