Collatzの問題(BigInt版)
(例:19638399)
出力欄:
Collatzの問題のBigInt(大きな整数)版です。どんな大きな整数でも正確に表せます。数値を直接書くときは 123n のように n を付けます。
function collatz() {
let x = BigInt(document.getElementById("input").value);
let s = "";
while (x > 1n) {
s += x + " → ";
if (x % 2n == 0n) {
x = x / 2n;
} else {
x = 3n * x + 1n;
}
}
s += x + " 終了";
document.getElementById("output").textContent = s;
}