aboutsummaryrefslogtreecommitdiff
path: root/challenge-146/iangoodnight/python/ch-1.py
blob: 840d58770fdb650417df80865d95ef919fbcc793 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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}")