Problem:


Score : 300 points

Problem Statement

Takahashi is standing on a multiplication table with infinitely many rows and columns.

The square (i,j) contains the integer i \times j. Initially, Takahashi is standing at (1,1).

In one move, he can move from (i,j) to either (i+1,j) or (i,j+1).

Given an integer N, find the minimum number of moves needed to reach a square that contains N.

Constraints

2 \leq N \leq 10^{12}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum number of moves needed to reach a square that contains the integer N.

Sample Input 1

10

Sample Output 1

5

(2,5) can be reached in five moves. We cannot reach a square that contains 10 in less than five moves.

Sample Input 2

50

Sample Output 2

13

(5, 10) can be reached in 13 moves.

Sample Input 3

10000000019

Sample Output 3

10000000018

Both input and output may be enormous.



Sample input:
10


Sample output:
5

Submission status: Accepted

Solution code:
n = int(input())
# 約数の合計が最低の約数のペアを求める
def make_div(n):
    divi = []
    for i in reversed(range(1,int(n**0.5)+1)):
        if n%i == 0:
            return [i, n//i] 

print(sum(make_div(n))-2)

Submission status: Accepted

Solution code:
import math

n = int(input())
num_i = 0

for i in range(1,int(math.sqrt(n))+1):
    if n%i == 0:
        num_i = i

num_j = n//num_i
ans = (num_i-1)+(num_j-1)

print(ans)

Submission status: Accepted

Solution code:
def factorize(n):
    a = []
    f = 2
    while f * f <= n:
        if n % f == 0:
            a.append((f, n//f))
        f += 1
    if not len(a):
        a.append((1, n))
    return a

n = int(input())
factor = factorize(n)

ans = int(1e13)
for fa in factor:
    a, b = fa
    ans = min(ans, a + b - 2)
print(ans)

Submission status: Accepted

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    mt = 10 ** 20
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            t = n // i
            mt = min(mt, t + i - 2)
    print(mt if mt != 10 ** 20 else "error")


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import math
def ii():
  return int(input())

N = ii()

def facts(n):
  facts = []
  for i in range(1, n):
    if i*i>n:
      break
    if n%i==0:
      facts.append(i)
  return facts

fs = facts(N)
a = fs[-1]
b = N//a

print(a+b-2)

Submission status: Accepted

Solution code:
import math
N = int(input())

answer = N+1
i = int(math.sqrt(N))
while i > 0:
    if N % i == 0:
        answer = i + N // i
        break
    i -= 1
print(answer-2)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    i = 2
    ans = N + 1
    while i ** 2 <= N:
        if N % i == 0:
            ans = min(i + N / i, ans)
        i += 1
    print(int(ans - 2))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
mini=float('inf')
for i in range(1,int(n**0.5)+1):
    if n%i==0:
        mini=min(i+(n/i)-2,mini)
print(int(mini))

Submission status: Accepted

Solution code:
import math
N = int(input())
sq = math.floor(math.sqrt(N))
ans = N
for i in range(1, sq + 1):
	if(N % i == 0):
		ans = i + N // i - 2
print(ans)

Submission status: Accepted

Solution code:
from math import sqrt
N=int(input())
ans1=1
ans2=1

i=1
while i <= sqrt(N):
    if N%i==0:
        ans1=i
    i+=1
ans2=N//ans1

ans=(ans1-1)+(ans2-1)
print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())

root_n = int(math.sqrt(n))

ans = 10**12
for a in range(1, root_n + 1):
    if n % a != 0:
        continue

    b = n // a
    ans = min(ans, a + b - 2)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

mylist = []
x, y = 0, 0

for i in range(1, int(n ** 0.5) + 1):
    if n % i == 0:
        mylist.append(i)
        x = i
    else:
        continue

y = n // x

print(x + y - 2)

Submission status: Accepted

Solution code:
from math import sqrt
n=int(input())
a=n
flag=False
for i in range(2,int(sqrt(n))+2):
    if n%i==0:
        b=(i-1)+(n//i-1)
        if a>b:
            a=b
            flag=True
if flag:
    print(a)
else:
    print(n-1)

Submission status: Accepted

Solution code:
def main():

	
	n = int(input())
	a = 0
	b = 0
	ans_list = []
	for i in range(1,int(n**0.5)+1):
		if n % i ==0:
			a = i
			b = n//a
			ans_list.append(a+b-2)
	print(min(ans_list))


	



 
if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
n=int(input())
divisors = []
def a(n):
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            divisors.append(n//i)
    divisors.sort()
    return divisors
a(n)
x=len(divisors)//2
print(divisors[x]+divisors[x-1]-2)

Submission status: Accepted

Solution code:
import math

N = int(input())

goal_x = 1
goal_y = N

for i in range(2, int(math.sqrt(N) + 1)):
    if N % i == 0:
        if goal_x + goal_y > i + int(N / i):
            goal_x = i
            goal_y = int(N / i)

print(goal_x + goal_y - 2)

Submission status: Accepted

Solution code:
import math
n = int(input())

ans = 10**12
mid = math.ceil(math.sqrt(n)) + 1
for i in range(1, mid):
    if n % i == 0:
        ans = min(i+(n//i)-2, ans)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
i = 1
M = 1
while i ** 2 <= N:
    if N % i == 0:
        M = i
    i += 1
print(M + N // M - 2)

Submission status: Accepted

Solution code:
import math

N = int(input())
rN = int(math.ceil(math.sqrt(N)))

for i in (range(rN,0,-1)):
    a=N//i
    if a*i==N:
        print(a+i-2)
        break

Submission status: Accepted

Solution code:
N = int(input())

ans = N-1
M = int(N**0.5) + 1
for k in range(2, M):
    if N % k == 0:
        ans = min(ans, (k - 1) + (N // k - 1))
print(ans)

Submission status: Accepted

Solution code:
import itertools


def read():
    N = int(input())

    return N,


def solve(N, ):
    move = 1000000000000
    a = 1
    b = N
    while a < b:
        b = N // a
        if N % a == 0:
            move = min(move, a + b - 2)
        a += 1
    return move


if __name__ == '__main__':
    input = read()
    print(solve(*input))

Submission status: Accepted

Solution code:
import math

N = int(input())
sqrt_n = int(math.sqrt(N))
d0 = 1
d1 = 1
for i in range(1, sqrt_n + 2):
    if N % i == 0:
        d0 = i
        d1 = N // i
print( d0 - 1 + d1 - 1)

Submission status: Accepted

Solution code:
from math import sqrt
n = int(input())
m = 2*n

for i in range(1, int(sqrt(n))+1):
    if n % i == 0:
        a = n / i
        b = n / a
        if a+b <= m:
            m = a+b

print(int(m-2))

Submission status: Accepted

Solution code:
n = int(input())
ans = 10**12
for i in range(1,int(n**0.5)+1):
    if n % i == 0:
        j = n//i
        
        ans = min(ans,i+j)
print(ans-2)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()

  ans=inf
  for x in range(1,int(math.sqrt(n))+1):
    if n%x==0:
      ans=min(ans,x+n//x-2)
  return ans

# main()
print(main())

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
sys.setrecursionlimit(200000)
input = sys.stdin.readline

N, = map(int, input().split())
# gx, gy, = map(int, input().split())
# N = int(input())
# L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

l = []
for i in range(1, int(N ** 0.5) + 1):
    if N % i == 0:
        l.append(i)
    
ans = 10 ** 18
for v in l:
    x, y = v, N // v
    ans =  min(ans, x - 1 + y - 1)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

def solve():
    N = int(input())
    
    r = 1e12
    for i in range(1, int(np.sqrt(N)) + 1):
        if N % i == 0:
            r = min(r, (N / i - 1) + (i - 1))
            
    
    print(int(r))

solve()

Submission status: Accepted

Solution code:
import math
N = int(input())
n = int(math.sqrt(N))
divisor = [i for i in range(1, n + 1) if N % i == 0]
XY = [div + N // div - 2 for div in divisor]
print(min(XY))

Submission status: Accepted

Solution code:
N = int(input())

min = float('inf')

times = int(N**0.5)

for i in range(1, times+1, 1):

    if N%i==0:
        j = N//i
    else:
        continue
    temp = i+j-2
    if min > temp:
        min = temp

print(min)

Submission status: Accepted

Solution code:
import math

N = int(input())

hoji = float("inf")
for i in range(1,int(math.sqrt(N))+1):
    if N%i == 0 and (i-1)+((N//i)-1) < hoji:
        hoji = (i-1)+((N//i)-1)

print(hoji)

Submission status: Accepted

Solution code:
import math
n = int(input())
r = list()
for a in range(1,int(math.sqrt(n))+1):
    if n % a == 0:
        b = n//a
        r.append(a+b-2)
print(min(r))

Submission status: Accepted

Solution code:
import math
n = int(input())
r = list()
for a in range(1,int(math.sqrt(10**12)+1)):
    if n % a == 0:
        b = n//a
        r.append(a+b-2)
print(min(r))

Submission status: Accepted

Solution code:
N=int(input())
t=0
i=int(N**0.5)+1
while i>=2:
    if N%i==0:
        t=i
        break
    else:
        i-=1
        t=N
if t==N:
    print(t-1)
else:
    print(t-1+N//t-1)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())

x,y=0,0
for i in range(1, int(np.sqrt(n)) + 1):
    if n % i == 0:
        x = i
        y = n // x

print(x+y-2)

Submission status: Accepted

Solution code:
n = int(input())
 
l1 = []
for i in range(1, int(n ** 0.5) + 1):
  q, mod = divmod(n , i)
  if mod == 0:
    l1.append(q + i -2)
print(min(l1))

Submission status: Accepted

Solution code:
import sys
import math

def main():
    N = int(input())

    for a in reversed(range(1, int(math.sqrt(N))+1)):
        if N % a == 0:
            print(int(a + N/a - 2))
            sys.exit()

main()

Submission status: Accepted

Solution code:
N=int(input())

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

divisors=make_divisors(N)
l=[]
for i in divisors:
    tmp=N//i
    l.append(i-1+tmp-1)

print(min(l))

Submission status: Accepted

Solution code:
import math
N = int(input())
ans = float("inf")

for i in range(1,int(math.sqrt(N))+1):
    if N%i == 0:
        ans = min(ans,i+N//i-2)
print(int(ans))

Submission status: Accepted

Solution code:
N = int(input())
i = 1
ans = float("inf")
while i * i <= N:
    if N % i == 0:
        ans = min(ans, i + N // i - 2)
    i += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a=[]
for i in range(1,int(n**0.5)+1):
    if  n%i == 0:
        a.append([i,n//i])
b = 10**14
for i in range(len(a)):
    b = min(b,(a[i][0]-1)+(a[i][1]-1))
print(b)

Submission status: Accepted

Solution code:
def md(n):
    d=[]
    da=d.append
    for i in range(1,int(n**0.5)+1):
        if n%i==0:
            da(i)
            if i!=n//i:da(n//i)
    d.sort()
    return d
n = int(input())
t=md(n)
u=len(t)
midopen = 0
mini = 999999999999999
#print(t)
if u%2==1:midopen=u//2
else:midopen=u//2+1
for i in range(min(u-1,midopen+1)):
    x = t[i]
    y = n//x
    #print(x,y)
    mini = min(mini,x+y-2)
print(mini)

Submission status: Accepted

Solution code:
N=int(input())
a=int(N**0.5)+1#約数の最大限
answer = float('inf')
for i in range(1,a):
  if N%i==0:
    answer = min(answer,i+(N//i-2))
print(answer)

Submission status: Accepted

Solution code:
N = int(input())

ret = float("inf")
for i in range(1, int(N**0.5) + 1):
  if N % i == 0:
    ret = min(N // i + i - 2, ret)
    
print(ret)

Submission status: Accepted

Solution code:
import math
a = int(input())
b = int(math.sqrt(a))
for i in range(b):
    x = a/b
    if x.is_integer():
        break
    else:
        b -= 1
print(int(x + b -2))

Submission status: Accepted

Solution code:
from math import sqrt
from math import floor
n=int(input())
c=floor(sqrt(n))
t=[]
for i in range(1,c+1):
  if(n%i==0):
    d=n//i
    t.append(i)
    t.append(d)
print(t[-1]+t[-2]-2)

Submission status: Accepted

Solution code:
import math
a = int(input())
root = int(math.sqrt(a))+1
left = 1
for i in range(2, root):
    if(a%i==0):
        left = i
total = left+(a//left)
print(total-2)

Submission status: Accepted

Solution code:
import math

n = int(input().strip())
rep = []
for a in range(1, math.floor(n ** 0.5) + 1, 1):
    m = n % a
    if m == 0:
        rep.append(a + n // a)
print(min(rep) - 2)

Submission status: Accepted

Solution code:
n = int(input())

ans = 1000000000000

a = 1
while a * a <= n:
  if n % a != 0:
    a += 1
    continue
  b = n // a
  ans = min(ans, a + b - 2)
  a += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
min_result = 10**12
for n in range(1,int(N**0.5)+1):
    if N%n == 0:
        result = n + int(N/n) - 2
        if min_result > result:
            min_result = result
print(min_result)

Submission status: Accepted

Solution code:
import math

n = int(input())

ans = 10**12
for i in range(1, int(n ** 0.5) + 1):
    if n % i != 0:
        continue
    move = i + int(n / i) - 2
    if move < ans:
        ans = move
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

minimum = 10**12

a = 1

while a <= n**(1/2):
    b = n / a
    if b.is_integer():
        if a + b - 2 < minimum:
            minimum = a + b - 2
    
    else:
        a += 1
        continue
    
    a += 1

print(int(minimum))

Submission status: Accepted

Solution code:
n = int(input())
a = int(n**0.5)

while n % a != 0:
  a = a-1
else:
  print(a+n//a-2)

Submission status: Accepted

Solution code:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

N=int(input())
divisors = make_divisors(N)

distance = [ i - 1 + int(N / i) - 1 for i in divisors]
print(min(distance))

Submission status: Accepted

Solution code:
import math
n=int(input())
a=math.sqrt(n)
b=round(a)
for i in range(b,0,-1):
    if n%i==0:
        print(n//i+i-2)
        break

Submission status: Accepted

Solution code:
N = int(input())

divisors = []
for x in range(1, int(N**0.5) + 1):
    if N % x == 0:
        divisors.append(x)

sums = []
for x in divisors:
    i, j = x, N // x
    sums.append(i + j)

print(min(sums) - 2)

Submission status: Accepted

Solution code:
import math
N = int(input())

ans = 1e15
i = 0
while i < math.sqrt(N): 
  i += 1
  if N%i > 0:
    continue
  a = i
  b = N // i
  ans = min(ans, a+b-2)

  
print(ans)

Submission status: Accepted

Solution code:
from math import sqrt

N = int(input())

for i in range(int(sqrt(N)) + 1, -1, -1):
    if N % i == 0:
        print((i - 1) + (N // i - 1))
        break

Submission status: Accepted

Solution code:
n=int(input())
ab = []
for i in range(1,int(n**(1/2))+1):
    if n%i == 0:
        ab.append((n//i,i))
ans=10**12
for i in range(len(ab)):
    a,b = ab[i]
    ans=min(a+b-2,ans)
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
n=int(input())

b=n
a=[]
N=int(math.sqrt(n))

for i in range(1,N+1):
 q, mod=divmod(n,i)
 a=q+i-2

 if(mod==0 and a<b):
     b=a

print(b)

Submission status: Accepted

Solution code:
N = int(input())
li = list()
n = N + 1
for i in range(1, int(N**0.5+1)):
	if N % i == 0:
		li.append([i, int(N/i)])
for x in li:
	tmp = x[0] + x[1]
	if tmp <= n:
		n = tmp
ans = n - 2
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
sqrter=int(n**0.5)
i=sqrter
while(i>=1):
    if(n%i==0):
        print(i+n//i-2)
        break
    i-=1

Submission status: Accepted

Solution code:
n = int(input())

def min_walk(n):
    out = []
    for a in range(1, int(n**0.5)+1):
        if n % a == 0:
            out.append(a + (n//a) - 2)

    return min(out)


print(min_walk(n))

Submission status: Accepted

Solution code:
from math import sqrt
from sys import exit

N = int(input())

for i in range(int(sqrt(N)) + 1, -1, -1):
    if N % i == 0:
        print((i - 1) + (N // i - 1))
        exit()

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
ans=n
for i in range(1,int(np.sqrt(n))+1):
    if n%i==0:
        ans=np.min((ans,i+int(n/i)-2))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

a = make_divisors(N)
i = len(a)-1
if len(a)%2==0:
  print(a[i-1]+a[i]-2)
else:
  print(a[i]*2-2)

Submission status: Accepted

Solution code:
N=int(input())
a=1
b=N
mx=int(1+N**0.5)
for i in range(1,mx):
    if N%i==0:
        if (a+b)>(i+N//i):
            a=i
            b=N//i
print(a+b-2)

Submission status: Accepted

Solution code:
n = int(input())
nr = int(n ** 0.5) + 1
for i in range(nr,0,-1):
    if n % i == 0:
        j = n // i
        break
print(i + j -2)

Submission status: Accepted

Solution code:
import math
n = int(input())

ans = 0
for i in range(math.ceil(math.sqrt(n) + 1))[:0:-1]:
    if 0 == n % i:
        ans = i
        break
ans_partner = n // ans
print(ans + ans_partner - 2)

Submission status: Accepted

Solution code:
N = int(input())
minimal = N - 1
for i in range(1,int(N**0.5)+1):
    if N%i == 0:
        minimal = min(minimal, i+N/i-2)
print(int(minimal))

Submission status: Accepted

Solution code:
n = int(input())
th = int(n ** 0.5) + 1

for i in range(th, 0, -1):
  if n % i == 0:
    j = n // i
    break

print(i + j - 2)

Submission status: Accepted

Solution code:
N = int(input())
#掛け算表は対称行列
#対角成分に移動するときが最短移動
def make_divisors(n):
    divisors = []
    for i in range(1,int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort(reverse=True)
    return divisors
div = make_divisors(N)
l = len(div) // 2
a = div[l]
b = N // a
ans = (a - 1) + (b - 1)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
rslt = 1+N
rt_N = int(N**0.5)
for i in range(rt_N, 0, -1):
    if N%i == 0:
        rslt = i + int(N/i) -2
        break;
print(rslt)

Submission status: Accepted

Solution code:
import math
if __name__=='__main__':
    n=int(input())
    oldn=10**20
    for i in range(int(math.sqrt(n))+1):
        if n%(i+1)==0 and oldn>=(((i+1)+n//(i+1))-2):
            oldn=(((i+1)+n//(i+1))-2)
        elif n%(i+1)==0 and oldn<(((i+1)+n//(i+1))-2):
            break
    print(oldn)

Submission status: Accepted

Solution code:
N = int(input())

for i in reversed(range(1, int(N**0.5)+1)):
    j = int(N/i)
    if i*j == N:
        print(i-1+j-1)
        break

Submission status: Accepted

Solution code:
import math

n = int(input())
p = math.floor(math.sqrt(n))
while not (n/p).is_integer():
	p -= 1
q = int(n/p)
print(p-1 +  q-1)

Submission status: Accepted

Solution code:
N = int(input())
x = int( N**0.5 )

for i in range(x, 0, -1):
    if N % i == 0:
        break

print(i + (N // i) - 2)

Submission status: Accepted

Solution code:
import math

n = int(input())

ans = float('inf')
for i in range(1, math.ceil(n**(1/2))+1):
    if n%i == 0:
        a = i
        b = n // i
        d = a-1 + b-1
        ans = min(ans, d)

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
from itertools import combinations

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors

from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

import math 
n=int(math.sqrt(N))
k=n
divisors = []
if (n ** 2) == N:
    k = 2 * (n-1)
else:
    divisors = make_divisors(N)
    center = (len(divisors) // 2) - 1
    k = divisors[center] - 1 + divisors[center+1] - 1

print(k)

Submission status: Accepted

Solution code:
import math
N=int(input())
p=math.floor(math.sqrt(N))
while N % p > 0:
    p-=1
q=N//p
print(p+q-2)

Submission status: Accepted

Solution code:
N = int(input())
#2<=N<10**12

num=int(N**(1/2)+1)
count=N+1-2
##N=a*b
#output=a+b-2
#積なので対称性よりa<=bとしてオッケー
for i in range(1,num):
    if N%i==0:
       count=min(count,i+N//i -2)
print(count)

Submission status: Accepted

Solution code:
n = int(input())

res = n-1
for i in range(1, int(n**(1/2))+1):
    if n % i == 0:
        res = n//i - 1 + n//(n//i) - 1
print(res)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from math import sqrt,floor
n = int(input())

x = floor(sqrt(n))
for i in range(x,-1,-1):
    if n%i==0:
        print(i+n//i-2)
        exit()

Submission status: Accepted

Solution code:
N = int(input())

root = int(N ** (1/2))

ans = N + 1 - 2

for i in range(1, root+1):
    if (N % i == 0):
        ans = min(ans, i + N // i - 2)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
D=[]
i=1
while(i*i<=N):
    if N%i==0:
        D.append(i)
        D.append(N//i)
    i+=1
ans=N-1
for i in D:
    j=N//i
    ans=min(ans,i+j-2)
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    for i in range(int(n**.5),0,-1):
        q,r=divmod(n,i)
        if(r==0):
            print(i+q-2)
            return
resolve()

Submission status: Accepted

Solution code:
n=int(input())
for i in range(int(n**0.5),0,-1):
    if n%i==0:
        print(i+n//i-2)
        exit()

Submission status: Accepted

Solution code:
from math import sqrt, ceil

n = int(input())

for i in range(ceil(sqrt(n)), 0, -1):
    if n % i == 0:
        print(i + n // i - 2)
        break

Submission status: Accepted

Solution code:
N = int(input())
result = N+1;
for i in range(1,int(N**0.5)+1):
    if N%i == 0:
        a = N//i
        b = a+i
        if b < result:
            result = b
print(result-2)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

n = int(input());
#n = [int(i) for i in input().split()];
#S = [str(s) for s in in_S]

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            divisors.append(n//i)

    # divisors.sort()
    return divisors
    
m = make_divisors(n)
#print(m);

count = 0;
for i in range(0,len(m),2):
    #print(i);
    #if i==len(m)-1:
        #break;
    tmp =  (m[i]-1) + (m[i+1]-1);
    #print(tmp);
    if tmp < count or count == 0:
        count = tmp;
    #if n%i == 0 and n/i <= 9:
        #jud ='Yes';
print(count);

Submission status: Accepted

Solution code:
n=int(input())
mx=10**15
for i in range(1,int(n**.5+1)):
    if n%i==0 and n//i+i-2<mx:
        mx=n//i+i-2
print(mx)

Submission status: Accepted

Solution code:
import math
N = int(input())
root_N = int(math.sqrt(N))
ans = 10 ** 12

for i in range(1, root_N + 1):
    if N % i == 0:
        mov = (N // i) + i - 2
        ans = min(ans, mov)

print(ans)

Submission status: Accepted

Solution code:
N = int(input()) 

tmp = int(N**0.5)+1
ans = N
for i in range(1, tmp):
    if N%i==0:
        ans = min(ans, i+N//i-2)
    
print(ans)

Submission status: Accepted

Solution code:
import math

def main():
    N = int(input())
    min_steps = N
    for i in range(1, math.floor(math.sqrt(N))+1):
        if N % i == 0:
            a, b = i, N//i
            min_steps = min(min_steps, a-1+b-1)
    print(min_steps)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
i = 1
res = n
while i * i <= n:
  if n % i == 0:
    res = min(res, i + n//i - 2)
  i += 1
print(res)

Submission status: Accepted

Solution code:
N = int(input())
N_sqrt = int(N**(1/2))
for i in range(1,N_sqrt+1):
    if N%i == 0:
        num = i
ans = num+N//num-2
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

for i in range(1, int(np.sqrt(N)) + 1):
    if N % i == 0:
        ans = (i - 1) + ((N / i) - 1)

print(int(ans))

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())

for i in range(int(np.sqrt(N)), 0, -1):
    if N % i == 0:
        break

a1, a2 = i, N//i

print(a1 + a2 - 2)

Submission status: Accepted

Solution code:
N = int(input())
num_list = [(i-1) + (N//i-1) for i in range(1, int(N**0.5+1)) if N%i == 0]
print(min(num_list))

Submission status: Accepted

Solution code:
N = int(input())
print(min([
    (i-1) + (N//i-1)
    for i in range(1, int(N**0.5+1))
    if N%i == 0
]))

Submission status: Accepted

Solution code:
import math

n = int(input())
upper = int(math.sqrt(n))

mx = 1
for i in range(1, upper + 1):
    if n % i == 0:
        mx = i

h = mx
v = n // mx

move = h - 1 + v - 1

print(move)

Submission status: Accepted

Solution code:
def run():
  N = int(input())
  for i in range(int(N**(1/2)), 0, -1):
    if N % i == 0:
      j = N//i
      print(i+j-2)
      break
  
if __name__ == '__main__':
  run()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import math

def solve(N: int):
    INF = 10000000000000
    result = INF
    for a in range(1, int( math.sqrt(N) ) + 1):
        if N % a:
            continue
        result = min(result, a + int( N / a ) - 2)
    print(result)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    solve(N)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
r=10**12
for i in range(1,int(n**0.5)+1):
	if n%i==0:
		r=min(r,i+n//i-2)
print(r)

Submission status: Accepted

Solution code:
import math

N = int(input())

sqN = math.floor(math.sqrt(N))

s = N - 1
for i in range(1,sqN+1):
    if N % i == 0:
        s = min(s,(N // i) + i - 2)

print(s)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
    N = I()
    ans = N - 1
    max_sq = int(math.sqrt(N)) + 1
    for i in range(1, max_sq):
        if N % i != 0:
            continue
        j = N // i
        ans = min(ans, i+j-2)
    print(ans)
main()

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
def main():
    N = int(input())
    ans = N-1
    max_sq = int(math.sqrt(N)+1)
    for i in range(1, max_sq):
        if N % i != 0:
            continue
        j = N // i
        ans = min(ans, i+j-2)
    print(ans)
main()

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
def main():
    N = int(input())
    ans = 10e+12 
    max_sq = int(math.sqrt(N)+1)
    for i in range(1, max_sq):
        if N % i == 0:
            j = N // i
            ans = min(ans, i+j-2)
    print(ans)
main()

Submission status: Accepted

Solution code:
from __future__ import print_function

import sys
input = sys.stdin.readline

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

# import math
# import string
# import fractions
# from fractions import Fraction
# from fractions import gcd

# def lcm(n,m):
#     return int(n*m/gcd(n,m))

# import re
# import array
# import copy
# import functools
# import operator

# import collections
# import itertools
# import bisect
# import heapq


# from heapq import heappush
# from heapq import heappop
# from heapq import heappushpop
# from heapq import heapify
# from heapq import heapreplace

# from queue import PriorityQueue as pq

# def reduce(p, q):
#     common = fractions.gcd(p, q)
#     return (p//common , q//common )
# # from itertools import accumulate
# # from collections import deque

# from operator import mul
# from functools import reduce

# def combinations_count(n, r):
#     r = min(r, n - r)
#     numer = reduce(mul, range(n, n - r, -1), 1)
#     denom = reduce(mul, range(1, r + 1), 1)
#     return numer // denom

# import random

def main():
    n = int(input().strip())

    a=1;b=n
    for i in range(1, int(n**.5)+1):
        if n%i==0:
            a=i
            b=n//a
    # eprint("a,b ",end=": ")
    # eprint(a,b)
    print(a+b-2)
        
    return

if __name__ == '__main__':
    main()



    '''
    for given N (2<=N<=10**12)
    find a_cnt such that           //  := (i_ans - i) + (j_ans - j)

    
    let T such that { (i,j) | (i,j \in [1...10**12] ) and (i*j == N) }
    

    let (i_ans, j_ans) \in T such that

    ( (i_ans, j_ans) \in T )
    and
    ( for all (i,j) \in T . (i_ans - 1) + (j_ans - 1) <= (i-1)+(j-1) )

    find a_cnt := (i_ans - 1) + (j_ans - 1)


    //
    [1, 1] -> [i_ans, j_ans] such that ( i_ans * j_ans = N )
    
    
    (1 <= i_ans <= , 1 <= j_ans <= )
    
    
    
    
    
    
    '''

Submission status: Accepted

Solution code:
import math
x = int(input())
s = int(math.sqrt(x))
c = 0
while True:
    if x % s == 0:
        c = x // s
        break
    s = s - 1
print(s + c - 2)

Submission status: Accepted

Solution code:
from math import sqrt


N = int(input())
res = N
for x in range(1, int(sqrt(N)) + 1):
    if N % x == 0:
        y = N // x
        res = min(res, x + y - 2)

print(res)

Submission status: Accepted

Solution code:
N = int(input())

max_i = int(N ** 0.5)

for i in range(max_i, 0, -1):
    if N % i == 0:
        x = i
        y = N // i
        break

ans = x + y - 2

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

x = int(N ** 0.5)

for i in range(x, 0, -1):
    if N % i == 0:
        y = N // i
        break

ans = N // y + y - 2

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

x = int(N ** 0.5)

for i in range(x):
    if N % x == 0:
        y = N // x
        break
    x -= 1

ans = x + y - 2

print(ans)