Skip to content

Commit 5d2815c

Browse files
Iqrar99github-actionsdhruvmanila
authored andcommitted
Add a solution for Project Euler 49 (TheAlgorithms#2702)
* added doctests in modular_exponential.py * added doctests in modular_exponential.py * added URL link * updating DIRECTORY.md * Add problem 49 solution * updating DIRECTORY.md * Fix several mistakes These fixes are intended to follow the CONTRIBUTING.md * Move the import statements lower * Update project_euler/problem_49/sol1.py Co-authored-by: Dhruv <[email protected]> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Dhruv <[email protected]>
1 parent 61fca41 commit 5d2815c

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

Diff for: DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,8 @@
633633
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_47/sol1.py)
634634
* Problem 48
635635
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_48/sol1.py)
636+
* Problem 49
637+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_49/sol1.py)
636638
* Problem 52
637639
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_52/sol1.py)
638640
* Problem 53

Diff for: project_euler/problem_49/__init__.py

Whitespace-only changes.

Diff for: project_euler/problem_49/sol1.py

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
"""
2+
Prime permutations
3+
4+
Problem 49
5+
6+
The arithmetic sequence, 1487, 4817, 8147, in which each of
7+
the terms increases by 3330, is unusual in two ways:
8+
(i) each of the three terms are prime,
9+
(ii) each of the 4-digit numbers are permutations of one another.
10+
11+
There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes,
12+
exhibiting this property, but there is one other 4-digit increasing sequence.
13+
14+
What 12-digit number do you form by concatenating the three terms in this sequence?
15+
16+
Solution:
17+
18+
First, we need to generate all 4 digits prime numbers. Then greedy
19+
all of them and use permutation to form new numbers. Use binary search
20+
to check if the permutated numbers is in our prime list and include
21+
them in a candidate list.
22+
23+
After that, bruteforce all passed candidates sequences using
24+
3 nested loops since we know the answer will be 12 digits.
25+
The bruteforce of this solution will be about 1 sec.
26+
"""
27+
28+
from itertools import permutations
29+
from math import floor, sqrt
30+
31+
32+
def is_prime(number: int) -> bool:
33+
"""
34+
function to check whether the number is prime or not.
35+
>>> is_prime(2)
36+
True
37+
>>> is_prime(6)
38+
False
39+
>>> is_prime(1)
40+
False
41+
>>> is_prime(-800)
42+
False
43+
>>> is_prime(104729)
44+
True
45+
"""
46+
47+
if number < 2:
48+
return False
49+
50+
for i in range(2, floor(sqrt(number)) + 1):
51+
if number % i == 0:
52+
return False
53+
54+
return True
55+
56+
57+
def search(target: int, prime_list: list) -> bool:
58+
"""
59+
function to search a number in a list using Binary Search.
60+
>>> search(3, [1, 2, 3])
61+
True
62+
>>> search(4, [1, 2, 3])
63+
False
64+
>>> search(101, list(range(-100, 100)))
65+
False
66+
"""
67+
68+
left, right = 0, len(prime_list) - 1
69+
while left <= right:
70+
middle = (left + right) // 2
71+
if prime_list[middle] == target:
72+
return True
73+
elif prime_list[middle] < target:
74+
left = middle + 1
75+
else:
76+
right = middle - 1
77+
78+
return False
79+
80+
81+
def solution():
82+
"""
83+
Return the solution of the problem.
84+
>>> solution()
85+
296962999629
86+
"""
87+
prime_list = [n for n in range(1001, 10000, 2) if is_prime(n)]
88+
candidates = []
89+
90+
for number in prime_list:
91+
tmp_numbers = []
92+
93+
for prime_member in permutations(list(str(number))):
94+
prime = int("".join(prime_member))
95+
96+
if prime % 2 == 0:
97+
continue
98+
99+
if search(prime, prime_list):
100+
tmp_numbers.append(prime)
101+
102+
tmp_numbers.sort()
103+
if len(tmp_numbers) >= 3:
104+
candidates.append(tmp_numbers)
105+
106+
passed = []
107+
for candidate in candidates:
108+
length = len(candidate)
109+
found = False
110+
111+
for i in range(length):
112+
for j in range(i + 1, length):
113+
for k in range(j + 1, length):
114+
if (
115+
abs(candidate[i] - candidate[j])
116+
== abs(candidate[j] - candidate[k])
117+
and len(set([candidate[i], candidate[j], candidate[k]])) == 3
118+
):
119+
passed.append(
120+
sorted([candidate[i], candidate[j], candidate[k]])
121+
)
122+
found = True
123+
124+
if found:
125+
break
126+
if found:
127+
break
128+
if found:
129+
break
130+
131+
answer = set()
132+
for seq in passed:
133+
answer.add("".join([str(i) for i in seq]))
134+
135+
return max([int(x) for x in answer])
136+
137+
138+
if __name__ == "__main__":
139+
print(solution())

0 commit comments

Comments
 (0)