aboutsummaryrefslogtreecommitdiff
path: root/challenge-169/sgreen/python/ch-2.py
blob: 3cd0323f3aa7797f2703b8953b60a54cf16cc149 (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
#!/usr/bin/env python3

def is_achilles_number(num):
    '''Determine if this number is an achilles numbers'''

    # Get the prime factors of the number
    factors = {}

    i = 2
    while num > 1:
        if num % i == 0:
            num /= i
            if i in factors:
                factors[i] += 1
            else:
                factors[i] = 1
        else:
            i += 1

    # If there is only one prime factor, it is not an achilles number
    if len(factors) == 1:
        return False

    # Get a list of unique powers
    powers = set(factors.values())

    # It's also not an achilles number if any of the powers were 1, or the
    #  powers are all the same (a perfect sqaure)
    if 1 in powers or len(powers) == 1:
        return False

    # Finally, the greatest common divisor of these numbers has to be one
    for i in range(2, min(powers)+1):
        if all(x % i == 0 for x in powers):
            return False

    return True


def main():
    '''Find the first 20 achilles numbers'''
    solutions = []
    num = 2

    # Get the first 20 achilles numbers
    while len(solutions) < 20:
        if is_achilles_number(num):
            solutions.append(num)
        num += 1

    # Print the list
    print(*solutions, sep=', ')


if __name__ == '__main__':
    main()