diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-01-17 09:00:14 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-01-17 09:00:14 +0000 |
| commit | 990d5177887cda20c8a4804b91afe5e9bcaaf201 (patch) | |
| tree | 8c2ae1ac10ff43e872f3cab2611255c76841f4c3 /challenge-147/sgreen/python | |
| parent | dd99d53e09a332c4ff5153b8bb54c9506a03860d (diff) | |
| parent | ee9f8c8288170dc442f8199fc1d2f7e45ac4377c (diff) | |
| download | perlweeklychallenge-club-990d5177887cda20c8a4804b91afe5e9bcaaf201.tar.gz perlweeklychallenge-club-990d5177887cda20c8a4804b91afe5e9bcaaf201.tar.bz2 perlweeklychallenge-club-990d5177887cda20c8a4804b91afe5e9bcaaf201.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-147/sgreen/python')
| -rwxr-xr-x | challenge-147/sgreen/python/ch-1.py | 50 | ||||
| -rwxr-xr-x | challenge-147/sgreen/python/ch-2.py | 32 |
2 files changed, 82 insertions, 0 deletions
diff --git a/challenge-147/sgreen/python/ch-1.py b/challenge-147/sgreen/python/ch-1.py new file mode 100755 index 0000000000..1b77f58482 --- /dev/null +++ b/challenge-147/sgreen/python/ch-1.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python + +import math + + +def is_prime(number): + # Return true or false if the number is a prime + if number < 2: + return False + + for i in range(2, int(math.sqrt(number)) + 1): + if number % i == 0: + return False + + # It's a prime + return True + + +def is_trunc_prime(number): + # Return true or false if the number and all it's left truncated + # parts is a prime. Start with the smallest figure first + n = str(number) + + if '0' in n: + # A left-truncatable prime cannot contain a zero + return False + + for i in range(len(n), 0, -1): + if not is_prime(int(n[i - 1:])): + return False + + return True + + +def main(): + solutions = [] + number = 1 + + # Keep increasing number by one until we have twenty truncated prime + # numbers + while len(solutions) < 20: + if is_trunc_prime(number): + solutions.append(number) + number += 1 + + print(*solutions, sep=', ') + + +if __name__ == '__main__': + main() diff --git a/challenge-147/sgreen/python/ch-2.py b/challenge-147/sgreen/python/ch-2.py new file mode 100755 index 0000000000..c6424b9617 --- /dev/null +++ b/challenge-147/sgreen/python/ch-2.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + +import math + + +def pentagon_number(n): + return int(n * (3 * n - 1) / 2) + + +def is_pentagon_number(x): + n = math.sqrt(24 * x + 1) + 1 + return int(n / 6) if n % 6 == 0 else None + + +def main(): + n1 = 2 + while True: + p1 = pentagon_number(n1) + for n2 in range(1, n1): + p2 = pentagon_number(n2) + + s1 = is_pentagon_number(p1 + p2) + s2 = is_pentagon_number(p1 - p2) + + if s1 is not None and s2 is not None: + print(f'P({n1}) + P({n2}) = {p1} + {p2} = { p1 + p2 } = P({s1})') + print(f'P({n1}) - P({n2}) = {p1} - {p2} = { p1 - p2 } = P({s2})') + return + n1 += 1 + + +main() |
