study/html/冰雹数列生成器.html
2022-08-04 12:30:02 +08:00

36 lines
1.2 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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>
<p>
随便想一个正整数。如果它是奇数则把它乘以3再加1。如果它是偶数则把它除以2。对每一个新产生的数都运用这个规则你知道会发生什么情况吗
数学家猜想它们都会变成1。
</p>
<input type="text" id="number" placeholder="输入一个正整数" />
<button onclick="go()">生成冰雹数列</button>
<p><code id="seq"></code></p>
<script>
// 冰雹数列生成器
function* hailstone(n) {
yield n;
while (n != 1) yield (n = n % 2 ? 3 * n + 1 : n / 2);
}
function go() {
if (number.value.match(/^\d+$/)) {
seq.innerText = [...hailstone(number.value)].join(", ");
number.value = "";
} else alert("请输入一个正整数");
}
</script>
</body>
</html>