-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
959c19b
commit 8935852
Showing
5 changed files
with
174 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
""" | ||
Problem 49: Prime permutations | ||
https://projecteuler.net/problem=49 | ||
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, | ||
is unusual in two ways: (i) each of the three terms are prime, and, | ||
(ii) each of the 4-digit numbers are permutations of one another. | ||
There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, | ||
exhibiting this property, but there is one other 4-digit increasing sequence. | ||
What 12-digit number do you form by concatenating the three terms in this sequence? | ||
""" | ||
# pylint: disable=invalid-name | ||
|
||
from typing import List, Iterable, Tuple | ||
|
||
from src.common.primes import is_prime | ||
from src.common.permutations import is_permutation | ||
|
||
|
||
def get_sorted_n_digit_primes(n: int) -> List[int]: | ||
"""Get all primes with `n` digits as a sorted list.""" | ||
n_digit_primes = [] | ||
for i in range(pow(10, n - 1), pow(10, n)): | ||
if is_prime(i): | ||
n_digit_primes.append(i) | ||
return n_digit_primes | ||
|
||
|
||
def get_n_digit_prime_arithmetic_sequences(n: int) -> Iterable[Tuple[int]]: | ||
""" | ||
Get arithmetic sequences made of three increasing numbers which have the following properties: | ||
(i) each of the three terms are prime, and, | ||
(ii) each of the 4-digit numbers are permutations of one another, and, | ||
(iii) they are equidistant. | ||
""" | ||
n_digit_primes_list = get_sorted_n_digit_primes(n) | ||
n_digit_primes_set = set(n_digit_primes_list) | ||
num_n_digit_primes = len(n_digit_primes_list) | ||
for idx1 in range(num_n_digit_primes): | ||
prime_1 = n_digit_primes_list[idx1] | ||
for idx2 in range(idx1 + 1, num_n_digit_primes): | ||
prime_2 = n_digit_primes_list[idx2] | ||
prime_3 = prime_2 + (prime_2 - prime_1) | ||
if prime_3 in n_digit_primes_set and \ | ||
is_permutation(prime_1, prime_2) and \ | ||
is_permutation(prime_2, prime_3): | ||
yield (prime_1, prime_2, prime_3) | ||
|
||
|
||
def main() -> None: | ||
"""Main function.""" | ||
n = 4 | ||
arithmetic_sequences = get_n_digit_prime_arithmetic_sequences(n) | ||
next(arithmetic_sequences) | ||
relevant_arithmetic_sequence = next(arithmetic_sequences) | ||
concatenated_number = ''.join([str(number) for number in relevant_arithmetic_sequence]) | ||
print(f'The arithmetic sequence is {relevant_arithmetic_sequence} ' \ | ||
f'and the corresponding 12-digit number is {concatenated_number}.') | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
""" | ||
Problem 49: Prime permutations | ||
https://projecteuler.net/problem=49 | ||
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, | ||
is unusual in two ways: (i) each of the three terms are prime, and, | ||
(ii) each of the 4-digit numbers are permutations of one another. | ||
There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, | ||
exhibiting this property, but there is one other 4-digit increasing sequence. | ||
What 12-digit number do you form by concatenating the three terms in this sequence? | ||
""" | ||
|
||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize('test_input_n,expected_result', [ | ||
(1, [2, 3, 5, 7]), | ||
(2, [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]) | ||
]) | ||
def test_get_n_digit_primes(test_input_n, expected_result): | ||
# arrange | ||
from src.p049_prime_permutations import get_sorted_n_digit_primes | ||
|
||
# act | ||
actual_result = get_sorted_n_digit_primes(test_input_n) | ||
|
||
# assert | ||
assert actual_result == expected_result | ||
|
||
|
||
@pytest.mark.parametrize('test_input_n,expected_result', [ | ||
(1, []), | ||
(2, []), | ||
(3, []), | ||
]) | ||
def test_get_n_digit_prime_arithmetic_sequences_no_sequences(test_input_n, expected_result): | ||
# arrange | ||
from src.p049_prime_permutations import get_n_digit_prime_arithmetic_sequences | ||
|
||
# act | ||
actual_result_iter = get_n_digit_prime_arithmetic_sequences(test_input_n) | ||
|
||
# assert | ||
assert list(actual_result_iter) == expected_result | ||
|
||
|
||
def test_get_n_digit_prime_arithmetic_sequences_first_4_digit_sequence(): | ||
# arrange | ||
from src.p049_prime_permutations import get_n_digit_prime_arithmetic_sequences | ||
|
||
# act | ||
actual_result_iter = get_n_digit_prime_arithmetic_sequences(4) | ||
|
||
# assert | ||
expected_result = (1487, 4817, 8147) | ||
assert next(actual_result_iter) == expected_result |