diff options
| author | Simon Green <mail@simon.green> | 2022-06-13 23:23:30 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2022-06-13 23:23:30 +1000 |
| commit | 78d5dd9442299652bc56f892d2736256453c08cd (patch) | |
| tree | 781a2902a0c6c1e104111e42755b0f821af6320a /challenge-169/sgreen/python/ch-2.py | |
| parent | cd7f24db7bbf5f10450ba2fa1450ad42dbf17262 (diff) | |
| download | perlweeklychallenge-club-78d5dd9442299652bc56f892d2736256453c08cd.tar.gz perlweeklychallenge-club-78d5dd9442299652bc56f892d2736256453c08cd.tar.bz2 perlweeklychallenge-club-78d5dd9442299652bc56f892d2736256453c08cd.zip | |
sgreen solutions to challenge 169
Diffstat (limited to 'challenge-169/sgreen/python/ch-2.py')
| -rwxr-xr-x | challenge-169/sgreen/python/ch-2.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/challenge-169/sgreen/python/ch-2.py b/challenge-169/sgreen/python/ch-2.py new file mode 100755 index 0000000000..3cd0323f3a --- /dev/null +++ b/challenge-169/sgreen/python/ch-2.py @@ -0,0 +1,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() |
