Matt O'leary
Well-known member
- Location
- Netherlands
Me and my friend were testing our python skills by writing a simple code that calculates prime numbers. It also shows how long it took and how many calculations it did.
Here is the src:
import time
calc = 0
def calculate(max):
global calc
for a in range(1,max):
for b in range(1,max):
calc = calc + 1
if a*b == max:
return False
return True
max = int(input('Enter max number: '))
for i in range(1,max):
sec = time.time()
if calculate(i):
print('I found the prime number {} in {}! It took {} calculations to find!'.format(i, time.strftime('%H:%M:%S',time.gmtime(time.time()-sec)), calc))
This outputs: I found the prime number x in 00:00:00! It took x calculations to find!
What we want to add are the milliseconds behind 00:00:00. Does anyone know how?
Any help is appreciated!
Matt Jeevus
Here is the src:
import time
calc = 0
def calculate(max):
global calc
for a in range(1,max):
for b in range(1,max):
calc = calc + 1
if a*b == max:
return False
return True
max = int(input('Enter max number: '))
for i in range(1,max):
sec = time.time()
if calculate(i):
print('I found the prime number {} in {}! It took {} calculations to find!'.format(i, time.strftime('%H:%M:%S',time.gmtime(time.time()-sec)), calc))
This outputs: I found the prime number x in 00:00:00! It took x calculations to find!
What we want to add are the milliseconds behind 00:00:00. Does anyone know how?
Any help is appreciated!
Matt Jeevus
Last edited by a moderator: