diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-06-16 14:26:20 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-16 14:26:20 +0100 |
| commit | 07094b2c5ff58b46ab54497c2a80c5451705fac1 (patch) | |
| tree | 7bec84698852cb0784ea9f67abbb3f95d8f73f44 /challenge-169/michael-dicicco/python | |
| parent | aa64948dbbcb3a643b4f23c939f0b8f7167558c3 (diff) | |
| parent | c99d4e2e180ac519c405e7bb7f799eb479aa0cea (diff) | |
| download | perlweeklychallenge-club-07094b2c5ff58b46ab54497c2a80c5451705fac1.tar.gz perlweeklychallenge-club-07094b2c5ff58b46ab54497c2a80c5451705fac1.tar.bz2 perlweeklychallenge-club-07094b2c5ff58b46ab54497c2a80c5451705fac1.zip | |
Merge pull request #6272 from mikedici/mdicicco-169
michael-dicicco java and python solutions
Diffstat (limited to 'challenge-169/michael-dicicco/python')
| -rw-r--r-- | challenge-169/michael-dicicco/python/ch-1.py | 24 | ||||
| -rw-r--r-- | challenge-169/michael-dicicco/python/ch-2.py | 38 |
2 files changed, 62 insertions, 0 deletions
diff --git a/challenge-169/michael-dicicco/python/ch-1.py b/challenge-169/michael-dicicco/python/ch-1.py new file mode 100644 index 0000000000..102d3eaeb5 --- /dev/null +++ b/challenge-169/michael-dicicco/python/ch-1.py @@ -0,0 +1,24 @@ +def is_brilliant(factors): + """a brilliant number has only two prime factors of the same length""" + return len(factors) == 2 and len(str(int(factors[0]))) == len(str(int(factors[1]))) + + +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(4, 300) if is_brilliant(prime_factors(i))])) 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)])) |
