-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdown_to_zero.py
52 lines (40 loc) · 1003 Bytes
/
down_to_zero.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
#!/bin/python3
import os
import sys
import math
#
# Complete the downToZero function below.
#
def downToZero(n):
m=set()
count=0
q=[]
q.append((n,count))
while len(q):
data,count=q.pop(0)
if data<=1:
if data==1:
count+=1
break
if data-1 not in m:
q.append((data-1,count+1))
m.add(data-1)
sqr=int(math.sqrt(data))
for i in range(sqr,1,-1):
if data%i==0:
div=max(int(data/i),i)
if div not in m:
m.add(div)
q.append((div,count+1))
return count
#
# Write your code here.
#
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
q = int(input())
for q_itr in range(q):
n = int(input())
result = downToZero(n)
fptr.write(str(result) + '\n')
fptr.close()