-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkth_multiple.py
28 lines (23 loc) · 894 Bytes
/
kth_multiple.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
from collections import deque
'''Design an algorithm to find the kth number such that the only prime factors are 3, 5, and 7.
Note that 3,5 and 7 do not have to be factors but is should not have any other prime factors
For example the first several multiples would be in orders 1, 3, 5, 7, 9, 15, 21'''
def clear(deq, num):
while deq[0] <= num:
deq.popleft()
return deq
def solution(n):
deques = [deque([3]), deque([5]), deque([7])]
num = 1
for k in range(n-1):
deques = list(map(lambda deq: clear(deq, num), deques))
candidates = list(map(lambda deq: deq[0], deques))
ind = candidates.index(min(candidates))
num = deques[ind].popleft()
deques[0].append(num * 3)
deques[1].append(num * 5)
deques[2].append(num * 7)
return num
# for i in range(1, 15):
# print(solution(i))
print(solution(1000000))