Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm getting an "Execution Timed Out" error?

I’m trying to improve my algorithm skills. When I run my code, I get an "Execution Timed Out" error.

Pseudocode

[This is writen in pseudocode]
if(number is even) number = number / 2
if(number is odd) number = 3*number + 1

My Code


def hotpo(n):
    calculator = 0
    while n >= 1:
        if n % 2 == 0:
            n = n / 2
        else:
            n = 3 * n + 1
        calculator = calculator + 1
    return calculator

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

you are dividing number by 2 if number is even but multiplying it by 3 and adding 1 into it.

so for any number it will keep doing this
2,1,4,2,1,4,2,1,4,2,1,4,2,1,4,…..

you just have to change condition to n>1 in while loop

because at last 2 will come and it got divided by 2, then n becomes 1 then again it will consider 1 as odd as per your condition then again 1*3+1=4 and again 4/2=2 and so on…

I hope you understood..

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading