-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpythonPrimes.py
More file actions
50 lines (40 loc) · 1.11 KB
/
pythonPrimes.py
File metadata and controls
50 lines (40 loc) · 1.11 KB
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
48
49
50
#title :pythonPrimes.py
#author :Alex Ciaramella, Greg Suner
#date :10/22/14
#usage :python pythonPrimes.py
#Main:
# Prompts user to enter a number and then loops
# from 0 to the entered number, checking if each
# intermediate number in the range is prime.
# If so, print out that number
from math import sqrt
def Main():
#Retrieve input from user
n = input("Please enter a number: ")
#Loop from 0 to entered number
for x in range (0,n+1):
#check if number is prime
if isPrime(x):
print x
#isPrime(x):
# @param: an integer
# @return: True if the parameter is prime
# False if the parameter is composite
def isPrime(x):
#0, 1, and negative integers are not prime
if x < 2:
return False
lastdivisor = int(sqrt(x))
#loop from 2 to the last possible divisor
for i in range(2, lastdivisor+1):
#if x is evenly divisible(no remainder)
#by any number i < x, then it is not prime
#therefore return false
if x%i == 0:
return False
#x must only be evenly divisble by itself and 1
#and must be prime
#therefore return True
return True
#Call the main function when starting the program
Main()