# Determining primality by using a simple trial division test. # # This approach tests whether n is a multiple of an integer i # between 2 and √n. If it is a multiple of any of these integers # then it is not a prime. import math import time def isPrime(n): for i in range(2, int(math.sqrt(n))): if n % i == 0: return False return True def withTimer(n): start = time.time() prime = isPrime(n) elapsed = time.time() - start print("{0} {1} time:{2}").format(prime, n, elapsed) # Primality of 2 to 13 digit known primes withTimer(13) withTimer(715827883) withTimer(2932031007403) # test non primes withTimer(52) withTimer(5820384023) withTimer(2059384726303) # my output: # True 13 time:0.0 # True 715827883 time:0.00300002098083 # True 2932031007403 time:0.521000146866 # False 52 time:0.0 # False 5820384023 time:0.000999927520752 # False 2059384726303 time:0.029000043869
A python example based blog that shows how to accomplish python goals and how to correct python errors.
Showing posts with label range. Show all posts
Showing posts with label range. Show all posts
Tuesday, May 24, 2011
Python - Determine Primality of a number
Friday, August 20, 2010
Python - limit cpu percentage for script
# It is sometimes useful to monitor how much cpu time or # cpu percentage your script is consuming. # This script will limit the cpu usage of your script # This example demostrates how to calculate the system # and user cpu time and cpu percentage # Note: this example is in python 3.0 # however, it is easily ported back to 2.x # by replaceing print() with print import os import time def getPercentage(unew, uold, start): """ calculate the percentage of cpu time """ return 100 * (float(unew) - float(uold)) / (time.time()-float(start)) def looper(timeCount, percentageGoal): """ loop over many tasks and keep the total cpu percentage consumtion to a desired level """ start = time.time() time.sleep(0.1) keepLooping = True uold, sold, cold, c, e = os.times() percentage = 0.0 while keepLooping: unew, snew, cnew, c, e = os.times() # since we are calculating the times from before we started looping the # percentages will be averaged over the duration of the script. print ("user %", percentage) # This just toggles to stop looping # when a time has been reached. In a real # script you would check for more work and # toggle off when there is no more work to # be done. if time.time()-start > timeCount: keepLooping = False #else: # print( time.time()-start) # do work: # In order for this script to actually help limit # the cpu usage you would need to break your script into # sections. # For example: if you were going to iterate through a large # list of data and perform actions on the contents # of the list then you should perform on action here # and keep looping through until all the actions # are accomplished. # # in this case we're just eating cpu so we get some numbers print("do work...") for i in range(1,1000000): b = 8*342*i*234 # tone back cpu usage while True: percentage = getPercentage(unew, uold, start) if percentage > percentageGoal: time.sleep(0.1) else: break; if __name__ == '__main__': # loop through work (for 4 seconds) and keep the cpu % # to less than 30% looper(4, 30) ## my output: ## user % 0.0 ## do work... ## user % 0.0 ## do work... ## user % 29.6673831301 ## do work... ## user % 29.1137166495 ## do work... ## user % 29.7617156875 ## do work... ## user % 29.5707887319 ## do work... ## user % 29.8122197706 ## do work... ## user % 29.3053848216 ## do work... ## user % 29.9385051866 ## do work...
Subscribe to:
Posts (Atom)