如果开平方之后是整数,那么开平方。否则进行减1操作。一直运算到1需要的最小的运算次数
import sys
import math
if __name__=="__main__":
N = int(sys.stdin.readline().strip().split()[0])
count = 0
n =int(math.sqrt(N))
h = [n]
while n>=4:
n = int(math.sqrt(n))
h.append(n)
for k in h:
c = N - k*k
N = k
count+=c+1
count+=N-1
print(count)
如果一直一步一步的for,时间复杂度比较高,所以这样求解