# pgm35: prints all primes between 0 and 10 million preceded by the sequence # number of each print("1: 2") print("2: 3") print("3: 5") print("4: 7") counter=4 import math for x in range (9,9999999,2): root=int(math.sqrt(x)) # make sure that 'root' is an integer if root%2==0: # make sure that root is odd root=root+1 else: root=root+2 d=3 # starting point for division by odd numbers while x%d!=0: d=d+2 if d>root: counter=counter+1 print(str(counter)+": "+str(x)) break
Page 1 of 1
5 Replies - 586 Views - Last Post: 22 November 2012 - 01:27 PM
Timing the execution of a Python program
Posted 22 November 2012 - 07:09 AM
I'm fairly sure that in Python there is some way of making a call to my computer clock before and after the execution of a program in order to determine how many minutes and seconds have elapsed. I would like to add such code to the following program to do that. It doesn't have to be anything fancy, like down to the millisecond -- in this case, minutes and seconds would be sufficient. The program would be run using IDLE, which is the only way I know how to run Python programs at this point. Any help or suggestions would be appreciated.
Replies To: Timing the execution of a Python program
Re: Timing the execution of a Python program
Posted 22 November 2012 - 11:17 AM
Your suggestion was helpful in one sense but not in another. I'm aware of the 'time' module because it's mentioned on page 516 of one of my Python textbooks. I'm fairly new to Python and to programming PC's in general. I'm taking an online tutorial provided by the University of Waterloo (Ontario), where I'm on chapter 12 of 18 chapters. The next chapter is on lists, which I'm excited about because I can use a list to make the above program execute somewhat faster. Although the tutorial has covered the defining of a function, my understanding of Python is not advanced enough to put to good use the previously mentioned "snippet" about timing a function. I do hope to get there one day in the not-too-distant-future. In the meantime, I’d like to put what Python I have learned to some use. That's one reason I wrote the program and why I plan to upgrade it once I learn about lists. I could time the execution of the program with a watch and get an accurate enough estimate to satisfy my curiosity; but I was hoping a few simple lines added to the beginning and end of the program would help me time not only this program but similar ones I may write in the near future...as well as illustrate how the 'time' module can be put to use.
Page 1 of 1