-1

everyone! I'm trying to solve this for school. I need to count how many steps it takes to come from any positive integer down to 1. And I'm supposed to use TypeScript. The Collatz Conjecture or 3x+1 problem can be summarized as follows: Take any positive integer n. If n is even, divide n by 2 to get n / 2. If n is odd, multiply n by 3 and add 1 to get 3n + 1. Repeat the process indefinitely. The conjecture states that no matter which number you start with, you will always reach 1 eventually. Here is my guess of code:

function steps(n: number) {
    let counter: number = 0;
    if (n > 0 && Number.isInteger(n)) {
        while (n !== 1) {
            if (n % 2 === 0) {                                      
                n = n / 2; 
                counter++;
            } else if (n % 2 !== 0) {
                n = (n * 3) + 1;
                counter++;
            }
            return counter;
        }
    } else {
        throw new Error('Only positive whole numbers are allowed');
    }
}

I wanted my code as transparent as possible, that's why I might be writing too much code. Don't mind that.

Mikhail
  • 49
  • 2
  • 10

3 Answers3

2

That's right. The return was out of place. It is supposed to be outside while loop.

function steps(n: number) {
    let counter: number = 0;
    if (n > 0 && Number.isInteger(n)) {
        while (n !== 1) {
            if (n % 2 === 0) {                                      
                n = n / 2; 
                counter++;
            } else if (n % 2 !== 0) {
                n = (n * 3) + 1;
                counter++;
            }
        }
        return counter;
    } else {
        throw new Error('Only positive numbers are allowed');
    }
}
Mikhail
  • 49
  • 2
  • 10
0

Hi i tried testing this in visual studio code why didnt it work?

function steps(n: number) {
let counter: number = 0;
if (n > 0 && Number.isInteger(n)) {
    while (n !== 1) {
        if (n % 2 === 0) {                                      
            n = n / 2; 
            counter++;
        } else if (n % 2 !== 0) {
            n = (n * 3) + 1;
            counter++;
        }
    }
    return counter;
} else {
    throw new Error('Only positive numbers are allowed');
}

}

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 15 '21 at 18:29
  • Please don't use `Post Answer` to ask a question. Use the [`Ask Question`](https://stackoverflow.com/questions/ask) button. Include these details as well as a link back to this question for context. - [From Review](https://stackoverflow.com/review/late-answers/30348509) – tdy Nov 16 '21 at 04:47
  • counter needs to be initialized with 1 for this to work in JS – Irina Nov 16 '22 at 19:36
0
  • Math problem that is never solved : Collatz conjecture or 3x+1

  • It will end up to 4-2-1 loop and its never ending

  • Apply only two rules

  • if the number is odd multiply by 3 and add 1

  • if the number is even divide by 2

  • It will eventually end up on 4-2-1 loop even if the number is quadrillionth of 2^68

    const collatz_conjecture = (number) => {

     setTimeout(() => {
         console.log(`Number: ${number}`);            
         if (number % 2 === 0) {
             //even
             console.log(`Number is even: ${number} - dividing by 2`);            
             number = (number / 2);
         } else {
             //odd
             console.log(`Number is odd: ${number} - multiplying by 3 and than adding 1`);
             number = (number * 3) + 1;
         }
    
         collatz_conjecture(number);
    
     }, 3 * 1000); // 3 seconds delay    
    

    };

    collatz_conjecture(10);