Files
Python/Primes.py

47 lines
908 B
Python
Raw Permalink Normal View History

2015-12-21 09:17:50 -08:00
# prime number calculator: find all primes up to n
2015-06-14 20:47:28 -07:00
max = int(input("Find primes up to what number? : "))
primeList = []
2019-10-20 15:41:00 +05:30
#for loop for checking each number
2015-06-14 20:47:28 -07:00
for x in range(2, max + 1):
isPrime = True
index = 0
root = int(x ** 0.5) + 1
while index < len(primeList) and primeList[index] <= root:
if x % primeList[index] == 0:
isPrime = False
break
index += 1
if isPrime:
primeList.append(x)
print(primeList)
2015-12-21 09:17:50 -08:00
#-------------------------------------------------------------
# prime number calculator: find the first n primes
count = int(input("Find how many primes?: "))
primeList = []
x = 2
while len(primeList) < count:
isPrime = True
index = 0
root = int(x ** 0.5) + 1
while index < len(primeList) and primeList[index] <= root:
if x % primeList[index] == 0:
isPrime = False
break
index += 1
if isPrime:
primeList.append(x)
x += 1
2019-10-20 15:41:00 +05:30
print(primeList)