Usuário:Gustavob/teste.js
Nota: Depois de publicar, poderá ter de contornar a cache do seu navegador para ver as alterações.
- Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
- Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
- Internet Explorer / Edge: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5
- Opera: Pressione Ctrl-F5.
<script type="text/javascript">
// the algorithm is implemented inside out so we can control our CPU usage
// rather than the main loop squareing the current guess we run one iteration
// and call result() when we are finished
function write(digit) {
document.body.appendChild(document.createTextNode(digit));
}
function writeLine(str) {
document.body.appendChild(document.createElement("br"));
document.body.appendChild(document.createTextNode(str));
}
var r = [];
// the working result of n * n, used to check estimates of n * n
var i = 0;
// index into n as we compute n * n
var j = 0;
// index into n as we compute n * n
var min = 0;
// min next last digit
var max = 10;
// one larger than max last digit
// min and max used to binary search for next digit
var n = [Math.floor((min + max)/2)];
// the sqrt(2) computed so far, last digit is working value
// base ten array, e.g. 1.414 = [1, 4, 1, 4]
function run() {
// work for 1/10 sec then pause for a while
// so we don't use up all available CPU and make
// the browser unresponsive
var startTime = new Date().getTime();
var startLen = n.length;
while (true) {
for (var steps = 0; steps < 100; ++steps) {
process();
if (n.length != startLen) {
// setTimeout if results changes so we see early digits immediately
break;
}
}
var now = new Date().getTime();
var ms = now - startTime;
if (ms > 100 || n.length != startLen) {
//;;writeLine(ms);
setTimeout("run();", ms * (Math.SQRT2 - 1));
// pause for sqrt(2)-1 of the time we have taken
// this will make us use 1/sqrt(2) of the one CPU (70.7%)
return;
}
}
}
function process() {
// 1.4 = [1, 4]
// 1x1 = 1, 0.4*0.4 = .16, 0.01*0.01 = 0.0001, 0.1*0.02 = 0.002
//;;writeLine("");
//;;writeLine("process()");
//;;writeLine("n = [" + n + "]");
//;;writeLine("r = [" + r + "]");
//;;writeLine("i = " + i);
//;;writeLine("j = " + j);
var dest = i + j;
//;;writeLine("dest = " + dest);
var e = n[i] * n[j];
while (r.length <= dest) {
r.push(0);
}
//;;writeLine("e = " + e)
for (; e != 0; --dest) {
//;;writeLine("dest = " + dest);
if (dest < 0) {
// only one whole number digit
// e.g. 25.2 = [25, 2]
r[0] += e * 10;
break;
}
r[dest] += e;
//;;writeLine("r[dest] = " + r[dest]);
e = Math.floor(r[dest] / 10);
//;;writeLine("e = " + e)
r[dest] = r[dest] % 10;
//;;writeLine("r[dest] = " + r[dest])
} // while carrying digits
//;;writeLine("r = [" + r + "]");
if (++j == n.length) {
j = 0;
if (++i == n.length) {
result(r);
r = [];
i = 0;
}
}
} // function squareStep()
function result(r) {
//;;writeLine("");
//;;writeLine("");
//;;writeLine("result(" + r + ")");
var digit = n.length - 1;
if (r[0] >= 2) {
max = n[digit];
//;;writeLine("too big, max = " + max);
} else {
min = n[digit];
//;;writeLine("ok, at least, min = " + min);
}
//;;writeLine("min = " + min);
//;;writeLine("max = " + max);
if (min + 1 == max) {
n[digit] = min;
gotDigit(n.length, n[digit]);
min = 0;
max = 10;
n.push(Math.floor((min + max)/2));
} else {
n[digit] = Math.floor((min + max)/2);
}
//;;writeLine("next try = last digit = " + n[n.length - 1]);
}
var val = document.getElementById("val")
var setElementWorks = true;
// with IE document.createElement("font").setElement("style", "color: white") is ignored
// have to use non-standard createElement("<font style=\"color: white\">") which doesn't work in Firefox
var shyWorks = true;
// with Firefox soft-hyphen "\xAD"/­ is ignored and won't break into lines
// have to use " " with font-size 0.1
if (navigator.userAgent.indexOf("MSIE") != -1) {
// IE: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)
setElementWorks = false;
} else if (navigator.userAgent.indexOf("Gecko") != -1) {
// Firefox: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.0.3) Gecko/20060523 Ubuntu/dapper Firefox/1.5.0.3
shyWorks = false;
} else {
// others
// be pesamistic about ­ working (using workaround works in all browers)
shyWorks = false;
// but assume standards compliant with .setElement()
}
function gotDigit(num, d) {
//;;writeLine("");
//;;writeLine("");
//;;writeLine("");
//;;writeLine("gotDigit(" + num + ", " + d + ")");
var t = document.createTextNode(d)
//t.appendData("\xAD");
val.appendChild(t);
//val.appendChild(document.createElement("br"));
//document.write(n[digit]);
///document.write("­");
// ­ soft hyphen is ignored in firefox and won't be broken into lines
// text in font-size < 0.1 is removed and won't be broken into lines
if (setElementWorks) {
shy = document.createElement("font");
shy.setAttribute("style", "font-size: 0.1; color: white");
} else {
shy = document.createElement("<font style=\"font-size: 0.1; color: white\">");
}
if (shyWorks) {
shy.appendChild(document.createTextNode("\xAD"));
// U+AD = HTML ­ = soft-hyphen
// createTextNode() takes plain text not HTML
// createEntityReference() is W2C but not in IE
} else {
shy.appendChild(document.createTextNode(" "));
}
val.appendChild(shy);
if (num == 1) {
val.appendChild(document.createTextNode("."));
}
}
run()
</script>