forked from ckuehnel/pycom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindprime.py
47 lines (42 loc) · 1.27 KB
/
findprime.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Generate prime numbers
# Found at http://www.raspberrypi-spy.co.uk/2012/06/overclocking-benchmarking-the-raspberry-pi/
# Based on an article from http://dunningrb.wordpress.com/
import utime, sys
# Function to print message
def print_prime(x):
print (" Prime : %7i " %x)
# Function to search for prime numbers
# within number range
def find_primes(upper_limit):
count = 0
candidate = 3
while(candidate <= upper_limit):
trial_divisor = 2
prime = 1 # assume it's prime
while(trial_divisor**2 <= candidate and prime):
if(candidate%trial_divisor == 0):
prime = 0 # it isn't prime
trial_divisor+=1
if(prime):
#print_prime(candidate)
count += 1
candidate += 2
return count
# Check if the script was called with a
# parameter. Use that as the upper limit
# of numbers to search
if len(sys.argv) != 2:
upper_limit=50000
else:
upper_limit=int(sys.argv[1])
# Record the current time
startTime = utime.ticks_ms()
# Start the process
print("")
print ("Sieve of Eratosthenes - LoPy")
print ("%d Iterations" %(upper_limit))
print ("")
count = find_primes(upper_limit)
# Measure and print the elapsed time
elapsed=utime.ticks_ms()-startTime
print (" Found %d primes in %s ms" %(count,elapsed))