diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-01-10 09:18:04 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-01-10 09:18:04 +0000 |
| commit | 54edabaf2afeb407d01d88c19d894823f85d5125 (patch) | |
| tree | 4e4a4fbda6b3023c3d3840a1daae50a49fca9c86 /challenge-146/iangoodnight/python/ch-1.py | |
| parent | 29b9eeeb95555dbcf1f375c89910c83ac83abd8d (diff) | |
| parent | e9411bdc7658179af3f23d3ada7970323547a7d7 (diff) | |
| download | perlweeklychallenge-club-54edabaf2afeb407d01d88c19d894823f85d5125.tar.gz perlweeklychallenge-club-54edabaf2afeb407d01d88c19d894823f85d5125.tar.bz2 perlweeklychallenge-club-54edabaf2afeb407d01d88c19d894823f85d5125.zip | |
Merge remote-tracking branch 'upstream/master'
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}") |
