科拉茨猜想

Collatz conjecture

我想在 JS 中做 Collat​​z 猜想,但我遇到了一个问题,我不明白。 当我点击按钮时 return 我 0,有人可以帮我吗?

我的js代码:

let input   = document.getElementById('nombre');
let btn     = document.getElementById('submit');
let output  = document.getElementById('output');
let coups;

let nombre = input.value;

function conjecture() {
    do {
        if (nombre%2 === 0) {
            nombre /= 2;
            output.innerHTML += nombre + '<br>';
            coups ++;
        } else {
            nombre *= 3;
            nombre ++;
            output.innerHTML += nombre + '<br>';
            coups++;   
        }
    } while (nombre>1);

    output.innerHtml += `La courbe a atterit en ${coups} coups.<br>`; 
}

btn.addEventListener('click', conjecture);

您正在读取用户有机会输入数字之前的值。

let nombre = input.value;

确保该行 您的方法中,该方法在 click

上调用
function conjecture() {
     let nombre = input.value;
     ... rest of your code
}

将下列复方程组的n个正整数值return全部变为整数1:

5n +1 如果 n=奇数
n/2 如果 n=偶数
不是所有n个正整数return到1,只有代表return到1的值。
对于上述奇偶条件下的5+1复杂系统,this image