Files
  • main.py
main.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
def isPrime(number):
  # assume prime unless we find a factor
  prime = True
  # primes (after 5) don't end in 2,4,5,6,8 or 0
  if number > 5 and str(number)[-1] in "245680":
    prime = False
  else:
    # starting with 2...
    factor = 2
    # ...and going up to the square root
    root = int(number**0.5)
    # keep going until we find a factor or we've tried all numbers
    while prime and factor <= root:
      # try factor to see whether it divides exactly...
      if number % factor == 0:
        # ...if so, then it's not prime
        prime = False
      factor += 1
  return prime

print("Number\tPrime\n======\t=====")
# count from 2 to 100 and show whether number is prime
for n in range(2,101):
  print(n,"\t"+str(isPrime(n)))
Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux