Python 3.3 vs. MSDOS Basic
John Immarino
johimm at gmail.com
Mon Feb 18 20:47:44 EST 2013
>
> > max=0
>
>
>
> "max" is a bad name -- it masks the built-in max() function
>
>
>
> > m=0
>
> > while m<=1000000:
>
> > m+=1
>
>
>
> Since "m" is only modified here and has a value of 1 for the first
>
> pass through, you can replace those three lines with
>
>
>
> for m in xrange(1, 1000001): #python 2.x, just use range() for 3.x
>
>
>
> > count=0
>
> > n=m
>
>
>
> > while n!=1:
>
> > count+=1
>
> > if n%2==0:
>
> > n=n//2
>
> > else:
>
> > n=3*n+1
>
>
>
> Avoid the comparison to 0 by reversing the then/else actions... Any
>
> 0 result is false.
>
>
>
> -=-=-=-=-
>
> import time
>
>
>
> mx = 0
>
>
>
> start = time.time()
>
> for m in xrange(1, 1000001):
>
> count = 0
>
> n = m
>
> while n > 1:
>
> count += 1
>
> if n % 2: # 0 means false
>
> n = 3 * n + 1
>
> else:
>
> n = n // 2
>
>
>
> if count > mx:
>
> mx, num = count, m
>
>
>
> end = time.time()
>
>
>
> print num, mx
>
> print end-start
>
> -=-=-=-=-
>
> Microsoft Windows XP [Version 5.1.2600]
>
> (C) Copyright 1985-2001 Microsoft Corp.
>
>
>
> E:\UserData\Wulfraed\My Documents>cd "Python Progs"
>
>
>
> E:\UserData\Wulfraed\My Documents\Python Progs>Script1.py
>
> 837799 524
>
> 83.2030000687
>
>
>
> E:\UserData\Wulfraed\My Documents\Python Progs>
>
>
>
>
>
>
>
>
>
>
>
>
>
> --
>
> Wulfraed Dennis Lee Bieber AF6VN
>
> wlfraed at ix.netcom.com HTTP://wlfraed.home.netcom.com/
Thanks, your suggestions are well taken.
More information about the Python-list
mailing list