Python 3.3 vs. MSDOS Basic

Serhiy Storchaka storchaka at
Tue Feb 19 12:13:30 CET 2013

On 18.02.13 21:13, John Immarino wrote:
> max=0
> m=0
> while m<=1000000:
>      m+=1
>      count=0
>      n=m
>      while n!=1:
>          count+=1
>          if n%2==0:
>              n=n//2
>          else:
>              n=3*n+1
>      if count>max:
>           max=count
>           num=m
> print(num,max)

Some minor tips:

1. Use range() for m iteration.
2. Instead of "if n%2==0:" use just "if n%2:".
3. Convert all you code to a function. Python is a little faster with 
locals than with globals.

In sum all this tips will speedup your code about 2x.

And one big tip:

Use cashing (and recursion). This will speedup your code more than 10x.

More information about the Python-list mailing list