Skip to content

Instantly share code, notes, and snippets.

@kzsg
Created September 18, 2008 04:25
Show Gist options
  • Save kzsg/11372 to your computer and use it in GitHub Desktop.
Save kzsg/11372 to your computer and use it in GitHub Desktop.
/*
var Y_MAX = "99999";
var X_MAX = "99999";
initialize();
*/
function PalindromeFinder(x, y) {
this.Y_MAX = x;
this.X_MAX = y;
this.doit = function(){
start = new Date();
var result = this.calc(this.X_MAX,this.Y_MAX);
end = new Date();
print(result[0]*result[1] + " = " + result[0] + " * " +result[1] +
" ( " + (end.getTime() - start.getTime()) + " msec)");
};
this.calc = function(x, y) {
var maxset = [0, 0];
for (i=x; i>0; i--) {
if (maxset[0]*maxset[1] > (i*y)) {
break;
}
for (k=y; k>0; k--) {
//print("("+i+","+k+")");
if (this.isPalindrome(i*k)) {
//print("isPalindrome!");
if (maxset[0]*maxset[1] < (i*k)) {
maxset = [i, k];
}
break;
}
}
}
return maxset;
};
this.isPalindrome = function(value) {
value = new String(value);
var center = value.length / 2;
var first = value.substring(0, center);
var last = value.substring(center, value.length);
if (first == this.inverse(last)) {
return true;
} else {
return false;
}
};
this.inverse = function(value) {
var rtnVal = "";
for (index = value.length; index > 0 ; index--) {
rtnVal += value.charAt(index-1);
}
return rtnVal;
};
}
var Palindrome = {
doit: function(){
start = new Date();
var result = calc3(X_MAX,Y_MAX);
end = new Date();
print(result[0]*result[1] + " = " + result[0] + " + " +result[1] + " ( " + (end.getTime() - start.getTime()) + " msec)");
},
calc3: function(x, y) {
var maxset = [0, 0];
for (i=x; i>0; i--) {
if (maxset[0]*maxset[1] > (i*y)) {
break;
}
for (k=y; k>0; k--) {
//print("("+i+","+k+")");
if (isPalindrome(i*k)) {
//print("isPalindrome!");
if (maxset[0]*maxset[1] < (i*k)) {
maxset = [i, k];
}
break;
}
}
}
return maxset;
},
calc2: function( x, y) {
var maxset = [0,0];
while (true) {
if (x < 1 || y < 1) {
break;
}
// $("#calc").append(x + " * " + y + " = " + (x*y) + "<br>");
if (isPalindrome(x*y)) {
if ((maxset[0]*maxset[1]) < (x*y)) {
maxset = [x, y];
}
x--;
y = Y_MAX;
} else {
if (x == y) {
x--;
y = Y_MAX;
} else {
y--;
}
}
}
return maxset;
},
calc: function(x, y) {
var maxset = [];
(function f(x, y) {
//$("#calc").append(x + " * " + y + " = " + (x*y) + "<br>");
if (x < 0) {
// xが0以下になったら終了
return;
}
if (y < 0) {
return f(x-1, Y_MAX);
}
if (isPalindrome(x*y)) {
if ((maxset[0]*maxset[1]) < (x*y)) {
maxset = [x,y];
}
} else {
f(x, y-1);
}
})(x, y);
return maxset;
},
isPalindrome: function(value) {
value = new String(value);
var center = value.length / 2;
for (var i=0;i<center;i++) {
//print(value);
if (value.charAt(i)!=value.charAt(value.length-(i+1))) {
//print(value + " => " + value.charAt(i) + " , " + value.charAt(value.length-(i+1)) );
//print("return false");
return false;
}
}
//print("return true");
return true;
},
isPalindrome2: function(value) {
value = new String(value);
var center = value.length / 2;
var first = value.substring(0, center);
var last = value.substring(center, value.length);
if (first == inverse(last)) {
return true;
} else {
return false;
}
},
inverse: function(value) {
var rtnVal = "";
for (index = value.length; index > 0 ; index--) {
rtnVal += value.charAt(index-1);
}
return rtnVal;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment