-
Notifications
You must be signed in to change notification settings - Fork 0
/
primes_for_else_loop.py
32 lines (27 loc) · 1.21 KB
/
primes_for_else_loop.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
#!/usr/bin/env python3
'''Prints all the primes numbers up to a value supplied from the user
Note:
A prime number is an integer greater than 1 whose only factors (divisors)
are 1 and the number itself
'''
from custom_modules.get_positive_number_from_user import get_positive_num
from time import clock
print(__doc__) # Program Greeting
# init
max_value = int(get_positive_num()) # Get positive integer from user
cntr = 0 # Prime accumulator
start_stopwatch = clock() # Start timer
##------- The Algorithm -------- ##
# Try values from 2 (smallest prime) to max_value (inclusive)
for potential_prime in range(2, max_value + 1):
is_prime = True # Provisionally, assume it is prime
for trial_factor in range(2, potential_prime): # Try all possible factors from 2 to potential_prime - 1
if potential_prime % trial_factor == 0:
is_prime = False
break # no need to continue
else:
print(potential_prime, end=' ') # Display prime
cntr += 1 # Track total primes found
print() # Move cursor to the next line
stop_stopwatch = clock() - start_stopwatch # Stop timer
print('It takes {0} secs to identify {1} prime numbers'.format(stop_stopwatch, cntr))