aboutsummaryrefslogtreecommitdiff
path: root/challenge-169/michael-dicicco/python/ch-2.py
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2022-06-16 18:07:18 -0400
committerDave Jacoby <jacoby.david@gmail.com>2022-06-16 18:07:18 -0400
commite28b4f047990a21b3e98b4e7b6b9e21f499d388a (patch)
tree39ed88bfdf9d2689042d534606601a19edc0bf9b /challenge-169/michael-dicicco/python/ch-2.py
parentb9ec9ea5dac6ae173154732b1c3f997676f798b9 (diff)
parent5c4b8df3cd8a191dd7c42fc49e12b5dfc3a02cc2 (diff)
downloadperlweeklychallenge-club-e28b4f047990a21b3e98b4e7b6b9e21f499d388a.tar.gz
perlweeklychallenge-club-e28b4f047990a21b3e98b4e7b6b9e21f499d388a.tar.bz2
perlweeklychallenge-club-e28b4f047990a21b3e98b4e7b6b9e21f499d388a.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-169/michael-dicicco/python/ch-2.py')
-rw-r--r--challenge-169/michael-dicicco/python/ch-2.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/challenge-169/michael-dicicco/python/ch-2.py b/challenge-169/michael-dicicco/python/ch-2.py
new file mode 100644
index 0000000000..8052d94e24
--- /dev/null
+++ b/challenge-169/michael-dicicco/python/ch-2.py
@@ -0,0 +1,38 @@
+def is_achilles(factors, given):
+ """Achilles was powerful but imperfect"""
+ for factor in factors:
+ """a powerful number is divisible by the squares of its prime factors"""
+ if not given % factor ** 2 == 0:
+ return False
+ for i in range(2, 10):
+ """a number is a perfect power if it has any integer roots"""
+ if nth_root(given, i) % 1 == 0:
+ return False
+ """the number is powerful and imperfect, like Achilles"""
+ return True
+
+
+def nth_root(some_number, n):
+ """raising a number to a fractional power of 1/n is the same as taking the nth root"""
+ return some_number ** (1 / n)
+
+
+def prime_factors(n):
+ output = []
+
+ while n % 2 == 0:
+ output.append(2)
+ n /= 2
+
+ for i in range(3, int(n ** 1 / 2 + 1), 2):
+ while n % i == 0:
+ output.append(i)
+ n /= i
+
+ if n > 2:
+ output.append(int(n))
+ return output
+
+
+if __name__ == '__main__':
+ print(", ".join([str(i) for i in range(72, 1801) if is_achilles(prime_factors(i), i)]))