study/html/派生成器.html
2022-08-04 12:30:02 +08:00

52 lines
1.4 KiB
HTML

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="" />
<title>派生成器</title>
</head>
<body>
<h1>派生成器</h1>
<h2>作者:赵海洋</h2>
<input type="text" id="number" placeholder="生成位数" />
<button onclick="go()">生成派</button>
<p>🥧 = <code id="seq" style="line-break: anywhere">3.14</code></p>
<script>
// 派生成器
function* piGenerator(n = 10) {
n = BigInt(n);
let p, q, d, d1;
let k = 3n,
a = 0n,
b = 4n,
a1 = 40n,
b1 = 24n;
while (n > 0n) {
[p, q, k] = [k * k, 2n * k + 1n, k + 1n];
[a, b, a1, b1] = [a1, b1, p * a + q * a1, p * b + q * b1];
d = ~~(a / b);
d1 = ~~(a1 / b1);
while (d === d1) {
yield Number(d);
a = (a % b) * 10n;
a1 = (a1 % b1) * 10n;
d = ~~(a / b);
d1 = ~~(a1 / b1);
n--;
}
}
}
function go() {
if (number.value.match(/^\d+$/)) {
seq.innerText = "3.3." + [...piGenerator(number.value)].join("");
number.value = "";
} else alert("请输入一个正整数");
}
</script>
</body>
</html>