diff options
| author | Ian Goodnight <goodnight.ian@gmail.com> | 2022-01-09 16:34:53 -0500 |
|---|---|---|
| committer | Ian Goodnight <goodnight.ian@gmail.com> | 2022-01-09 16:34:53 -0500 |
| commit | 329ea0e601d67c6eb37e8677aba54922a4680603 (patch) | |
| tree | c6b1c34463dc123496b231f15f19a35a323754c6 /challenge-146/iangoodnight/python/ch-1.py | |
| parent | bf0e37317122de0322d69b1c68bd07ccfc5b36a8 (diff) | |
| download | perlweeklychallenge-club-329ea0e601d67c6eb37e8677aba54922a4680603.tar.gz perlweeklychallenge-club-329ea0e601d67c6eb37e8677aba54922a4680603.tar.bz2 perlweeklychallenge-club-329ea0e601d67c6eb37e8677aba54922a4680603.zip | |
challenge 146
Diffstat (limited to 'challenge-146/iangoodnight/python/ch-1.py')
| -rwxr-xr-x | challenge-146/iangoodnight/python/ch-1.py | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/challenge-146/iangoodnight/python/ch-1.py b/challenge-146/iangoodnight/python/ch-1.py new file mode 100755 index 0000000000..840d58770f --- /dev/null +++ b/challenge-146/iangoodnight/python/ch-1.py @@ -0,0 +1,77 @@ +#!/usr/bin/python3 +# ch-1.py + +# > https://theweeklychallenge.org/blog/perl-weekly-challenge-146/ +# +# ## Task 1 > 10001st Prime Number +# ================================ +# +# Write a script to generate the 10001st prime number. + +import math +import sys + +############################################################################### +# PWC Solution ################################################################ +############################################################################### + + +def get_prime(nth): + primes = [2, 3] + cursor = 5 + while (len(primes) < nth): + is_prime = True + for prime in primes: + if prime > math.sqrt(cursor): + break + if cursor % prime == 0: + is_prime = False + break + if is_prime: + primes.append(cursor) + cursor += 2 + return primes.pop() + + +############################################################################### +# Utilities ################################################################### +############################################################################### + + +def get_suffix(num): + last_d = num % 10 + if last_d == 0 or last_d >= 4: + return 'th' + if last_d == 1: + return 'st' + if last_d == 2: + return 'nd' + if last_d == 3: + return 'rd' + return '' + + +def print_colors(string, color): + colordict = { + 'yellow': '\u001b[33m', + 'green': '\u001b[32m', + 'reset': '\u001b[0m' + } + return f"{colordict[color]}{string}{colordict['reset']}" + + +############################################################################### +# Main ######################################################################## +############################################################################### + +try: + nth = int(sys.argv[1]) +except IndexError: + nth = 10001 + +prime = get_prime(nth) +suffix = get_suffix(nth) +num_str = print_colors(str(nth) + suffix, 'yellow') +prime_str = print_colors(prime, 'green') + +print(f"The {num_str} prime number is {prime_str}") |
