Skip to content

Instantly share code, notes, and snippets.

@nelsonpecora
Created January 17, 2012 21:06
Show Gist options
  • Select an option

  • Save nelsonpecora/1628868 to your computer and use it in GitHub Desktop.

Select an option

Save nelsonpecora/1628868 to your computer and use it in GitHub Desktop.
Project Euler Problem 4
Array.prototype.max = function() {
var max = this[0];
var len = this.length;
for (var i = 0; i < len; i++) {
if (this[i] > max) {
max = this[i];
}
}
return max;
}
String.prototype.reverse = function() {
return this.split("").reverse().join("");
}
var pal = [],
b,
a,
ans,
strans,
l,
h,
strans1,
strans2;
for(b = 999; b > 0; b--) {
for(a = 999; a > 0; a--) {
ans = b * a;
strans = ans.toString();
if(strans.length % 2 === 0) {
l = strans.length / 2
strans1 = strans.substr(0,l);
strans2 = strans.substr(l);
if(strans2 === strans1.reverse()) {
pal.push(ans);
}
} else if(strans.length % 2 !== 0) {
h = Math.floor(strans.length/2);
strans1 = strans.substr(0,h);
strans2 = strans.substr(h+1);
if(strans2 === strans1.reverse()) {
pal.push(ans);
}
}
}
}
console.log(pal.max()); //gives 906609
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment