aboutsummaryrefslogtreecommitdiff
path: root/challenge-146/khalid-anwar/python/ch-1.py
blob: 62e628bbddc6a9776f806375dc20176662932b99 (plain)
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
#!/usr/bin/env python3
""" Returns the nth prime number By Khalid Anwar
"""
def get_nth_prime_number(nth):
  total_number_of_primes = 0
  factor = 2
  s = (nth * factor)
  while total_number_of_primes < nth:
    primes = make_prime(s)
    total_number_of_primes = sum(primes[2:])
    factor += 1
    s = (nth * factor)
  nth_prime_number = count_primes(primes, nth)
  return nth_prime_number

""" using the Sieve of Eratosthenes
"""
def make_prime(k):
  prime = bytearray([1]*k)
  for i in range(2, k):
      if prime[i] == 1:
        for j in range(i, k):
          if i*j < k:
            prime[i*j] = 0
          else:
            break
  return prime

""" Returns the n-th prime 
"""
def count_primes(primes, nth):
  count = 0
  for k in range(2, len(primes)):
    count += primes[k]
    if count == nth:
      return k


def main():
    NTH = 10001
    nth_prime_number = get_nth_prime_number(NTH)
    print("The {}-th prime number is => {}".format(NTH, nth_prime_number))


if __name__ == "__main__":
    main()