aboutsummaryrefslogtreecommitdiff
path: root/challenge-169/sgreen/python/ch-2.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-169/sgreen/python/ch-2.py')
-rwxr-xr-xchallenge-169/sgreen/python/ch-2.py56
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()