forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsol1.py
153 lines (121 loc) · 3.92 KB
/
sol1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""
Prime permutations
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,
(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?
Solution:
First, we need to generate all 4 digits prime numbers. Then greedy
all of them and use permutation to form new numbers. Use binary search
to check if the permutated numbers is in our prime list and include
them in a candidate list.
After that, bruteforce all passed candidates sequences using
3 nested loops since we know the answer will be 12 digits.
The bruteforce of this solution will be about 1 sec.
"""
importmath
fromitertoolsimportpermutations
defis_prime(number: int) ->bool:
"""Checks to see if a number is a prime in O(sqrt(n)).
A number is prime if it has exactly two factors: 1 and itself.
>>> is_prime(0)
False
>>> is_prime(1)
False
>>> is_prime(2)
True
>>> is_prime(3)
True
>>> is_prime(27)
False
>>> is_prime(87)
False
>>> is_prime(563)
True
>>> is_prime(2999)
True
>>> is_prime(67483)
False
"""
if1<number<4:
# 2 and 3 are primes
returnTrue
elifnumber<2ornumber%2==0ornumber%3==0:
# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
returnFalse
# All primes number are in format of 6k +/- 1
foriinrange(5, int(math.sqrt(number) +1), 6):
ifnumber%i==0ornumber% (i+2) ==0:
returnFalse
returnTrue
defsearch(target: int, prime_list: list) ->bool:
"""
function to search a number in a list using Binary Search.
>>> search(3, [1, 2, 3])
True
>>> search(4, [1, 2, 3])
False
>>> search(101, list(range(-100, 100)))
False
"""
left, right=0, len(prime_list) -1
whileleft<=right:
middle= (left+right) //2
ifprime_list[middle] ==target:
returnTrue
elifprime_list[middle] <target:
left=middle+1
else:
right=middle-1
returnFalse
defsolution():
"""
Return the solution of the problem.
>>> solution()
296962999629
"""
prime_list= [nforninrange(1001, 10000, 2) ifis_prime(n)]
candidates= []
fornumberinprime_list:
tmp_numbers= []
forprime_memberinpermutations(list(str(number))):
prime=int("".join(prime_member))
ifprime%2==0:
continue
ifsearch(prime, prime_list):
tmp_numbers.append(prime)
tmp_numbers.sort()
iflen(tmp_numbers) >=3:
candidates.append(tmp_numbers)
passed= []
forcandidateincandidates:
length=len(candidate)
found=False
foriinrange(length):
forjinrange(i+1, length):
forkinrange(j+1, length):
if (
abs(candidate[i] -candidate[j])
==abs(candidate[j] -candidate[k])
andlen({candidate[i], candidate[j], candidate[k]}) ==3
):
passed.append(
sorted([candidate[i], candidate[j], candidate[k]])
)
found=True
iffound:
break
iffound:
break
iffound:
break
answer=set()
forseqinpassed:
answer.add("".join([str(i) foriinseq]))
returnmax(int(x) forxinanswer)
if__name__=="__main__":
print(solution())