aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-01-09 10:32:42 +0000
committerGitHub <noreply@github.com>2022-01-09 10:32:42 +0000
commit20f6184ea6d038c1ae93b7e5a2b15078d75b8318 (patch)
treeb5f09f09bf5dda586db907af16213fb9b0de159d
parentb9e72da0989a505989e82f7478f2a7809e9b931d (diff)
parent5d774188960c533ff59b4b9a42285b96f893f8c2 (diff)
downloadperlweeklychallenge-club-20f6184ea6d038c1ae93b7e5a2b15078d75b8318.tar.gz
perlweeklychallenge-club-20f6184ea6d038c1ae93b7e5a2b15078d75b8318.tar.bz2
perlweeklychallenge-club-20f6184ea6d038c1ae93b7e5a2b15078d75b8318.zip
Merge pull request #5487 from anwarthebest/branch-for-challenge-145
10001st Prime Number in Python
-rw-r--r--challenge-146/khalid-anwar/python/ch-1.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/challenge-146/khalid-anwar/python/ch-1.py b/challenge-146/khalid-anwar/python/ch-1.py
new file mode 100644
index 0000000000..f5c05e61f5
--- /dev/null
+++ b/challenge-146/khalid-anwar/python/ch-1.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python3
+""" Returns the n-th 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() \ No newline at end of file