From 5d774188960c533ff59b4b9a42285b96f893f8c2 Mon Sep 17 00:00:00 2001 From: mohammad khalid anwar Date: Sun, 9 Jan 2022 03:34:07 +0530 Subject: 10001st Prime Number --- challenge-146/khalid-anwar/python/ch-1.py | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 challenge-146/khalid-anwar/python/ch-1.py (limited to 'challenge-146/khalid-anwar/python') 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 -- cgit From 9d462a54cb694c7cd78a1d1545bbcf5c430706e7 Mon Sep 17 00:00:00 2001 From: anwarthebest <65423536+anwarthebest@users.noreply.github.com> Date: Mon, 10 Jan 2022 02:28:29 +0530 Subject: Update ch-1.py --- challenge-146/khalid-anwar/python/ch-1.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'challenge-146/khalid-anwar/python') diff --git a/challenge-146/khalid-anwar/python/ch-1.py b/challenge-146/khalid-anwar/python/ch-1.py index f5c05e61f5..62e628bbdd 100644 --- a/challenge-146/khalid-anwar/python/ch-1.py +++ b/challenge-146/khalid-anwar/python/ch-1.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -""" Returns the n-th prime number By Khalid Anwar +""" Returns the nth prime number By Khalid Anwar """ def get_nth_prime_number(nth): total_number_of_primes = 0 @@ -43,4 +43,4 @@ def main(): if __name__ == "__main__": - main() \ No newline at end of file + main() -- cgit