<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<html xmlns=
"http://www.w3.org/1999/xhtml"
>
<head>
<title>运算路线查找算法</title>
<script type=
"text/javascript"
>
var
BLACKHOLENMB = 6174;
var
count = 0;
function
blackHole(nmb) {
if
(isNaN(nmb) || nmb < 1000 || nmb > 9999) {
return
-1;
}
count++;
var
tempArray = (nmb +
""
).split(
""
);
var
smallNmb = parseInt(tempArray.sort().join(
""
));
var
bigNmb = parseInt(tempArray.reverse().join(
""
));
var
d_value = bigNmb - smallNmb;
log(bigNmb, smallNmb, d_value, count);
if
(d_value != BLACKHOLENMB) {
return
blackHole(d_value);
}
else
{
return
count;
}
}
function
log(big, small, d_value, count) {
console.log(
"step "
+ count +
":"
+ big +
"-"
+ small +
"="
+ d_value);
}
console.log(blackHole(2167));
</script>
</head>
<body>
</body>
</html>