diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-24 22:23:16 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-24 22:23:16 +0100 |
| commit | 206c2fce8db1de9b7f82f04a3276005a284b3c40 (patch) | |
| tree | dc610bfa166543180dcaab4d843a3a154cbb19c9 /challenge-147 | |
| parent | ac43b36f9732005921ef7f303ffa669fe2c6ae03 (diff) | |
| download | perlweeklychallenge-club-206c2fce8db1de9b7f82f04a3276005a284b3c40.tar.gz perlweeklychallenge-club-206c2fce8db1de9b7f82f04a3276005a284b3c40.tar.bz2 perlweeklychallenge-club-206c2fce8db1de9b7f82f04a3276005a284b3c40.zip | |
Add Python solution to challenge 147
Diffstat (limited to 'challenge-147')
| -rw-r--r-- | challenge-147/paulo-custodio/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-147/paulo-custodio/perl/ch-2.pl | 2 | ||||
| -rw-r--r-- | challenge-147/paulo-custodio/python/ch-1.py | 36 | ||||
| -rw-r--r-- | challenge-147/paulo-custodio/python/ch-2.py | 43 |
4 files changed, 81 insertions, 2 deletions
diff --git a/challenge-147/paulo-custodio/perl/ch-1.pl b/challenge-147/paulo-custodio/perl/ch-1.pl index 85b81d6210..76794b7c9b 100644 --- a/challenge-147/paulo-custodio/perl/ch-1.pl +++ b/challenge-147/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 147 # -# TASK #1 › Truncatable Prime +# TASK #1 > Truncatable Prime # Submitted by: Mohammad S Anwar # Write a script to generate first 20 left-truncatable prime numbers in base 10. # diff --git a/challenge-147/paulo-custodio/perl/ch-2.pl b/challenge-147/paulo-custodio/perl/ch-2.pl index f81f51987d..3f9ad91939 100644 --- a/challenge-147/paulo-custodio/perl/ch-2.pl +++ b/challenge-147/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 147 # -# TASK #2 › Pentagon Numbers +# TASK #2 > Pentagon Numbers # Submitted by: Mohammad S Anwar # Write a script to find the first pair of Pentagon Numbers whose sum and # difference are also a Pentagon Number. diff --git a/challenge-147/paulo-custodio/python/ch-1.py b/challenge-147/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..f3982a0174 --- /dev/null +++ b/challenge-147/paulo-custodio/python/ch-1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 + +# Challenge 147 +# +# TASK #1 > Truncatable Prime +# Submitted by: Mohammad S Anwar +# Write a script to generate first 20 left-truncatable prime numbers in base 10. +# +# In number theory, a left-truncatable prime is a prime number which, in a given +# base, contains no 0, and if the leading left digit is successively removed, +# then all resulting numbers are primes. +# +# Example +# 9137 is one such left-truncatable prime since 9137, 137, 37 and 7 are all +# prime numbers. + +from sympy import isprime, nextprime + +def left_truncatable_prime_it(): + prime = None + while True: + prime = nextprime(prime) if prime is not None else 2 + if is_left_truncatable_prime(prime): + yield prime + +def is_left_truncatable_prime(p): + while True: + if not isprime(p): + return False + p = int(str(p)[1:]) if len(str(p)) > 1 else '' + if p == '': + return True + +it = left_truncatable_prime_it() +out = [next(it) for _ in range(20)] +print(", ".join(map(str, out))) diff --git a/challenge-147/paulo-custodio/python/ch-2.py b/challenge-147/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..87ee1d7626 --- /dev/null +++ b/challenge-147/paulo-custodio/python/ch-2.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +# Challenge 147 +# +# TASK #2 > Pentagon Numbers +# Submitted by: Mohammad S Anwar +# Write a script to find the first pair of Pentagon Numbers whose sum and +# difference are also a Pentagon Number. +# +# Pentagon numbers can be defined as P(n) = n(3n - 1)/2. +# +# Example +# The first 10 Pentagon Numbers are: +# 1, 5, 12, 22, 35, 51, 70, 92, 117 and 145. +# +# P(4) + P(7) = 22 + 70 = 92 = P(8) +# but +# P(4) - P(7) = |22 - 70| = 48 is not a Pentagon Number. + +limit = 100_000_000 + +pentagon = [1] +is_pentagon = {} + +def find_pair(): + is_pentagon_func(limit) # build pentagon up to N + try_ = pentagon.copy() + for a in try_: + for b in try_: + if is_pentagon_func(a + b) and is_pentagon_func(abs(a - b)): + return a, b + raise Exception("No pair found") + +def is_pentagon_func(num): + while pentagon[-1] < num: + n = len(pentagon) + 1 + p = n * (3 * n - 1) // 2 + pentagon.append(p) + is_pentagon[p] = True + return is_pentagon.get(num, False) + +a, b = find_pair() +print(f"({a},{b})") |
