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())
x = n + 1 - 2
for i in range (2, n):
  if n % i == 0:
    m = n // i
    x = i + m - 2
  if i * i > n:
    break
print(x)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

N = int(input())
 
mi = 10**100

for a in range(1, int(math.sqrt(N))+1, 1):
  if N % a == 0:  
    b = N//a
    if mi > a+b-2:
      mi = a+b -2
print(mi)

Submission status: Accepted

Solution code:
n=int(input())
l=list(range(2,int(n**0.5//1+1)))
ans=n-1
count=0
for i in l:
    if n%i==0:
        a=i+int(n/i)-2
        ans=min(ans,a)
        count+=1
print(ans if count>0 else n-1)

Submission status: Accepted

Solution code:
N = int(input())
ans = float('inf')

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
sqrt = math.sqrt
n = int(input())
m = int(sqrt(n)) +1

ans = float('inf')
for i in range(1,m):
    if n%i==0:
        ans = min(ans,i+n//i-2)

print(ans)

Submission status: Accepted

Solution code:
n = int(raw_input())
ans_xy =[]
tmp = []
for i in range(1,int(n**0.5)+1):
  if n%i==0:
    ans_xy.append(i)
    ans_xy.append(n/i)
    tmp.append(ans_xy)
    ans_xy = []
ans=tmp[0][0]+tmp[0][1]-2
for i in range(1,len(tmp)):
  ans = min(ans,tmp[i][0]+tmp[i][1]-2)
print ans

Submission status: Accepted

Solution code:

import sys
import math
from copy import deepcopy
from collections import deque, defaultdict
from operator import mul
from functools import reduce
sys.setrecursionlimit(20000000)

# input = sys.stdin.readline


def main():
    N = int(input())
    min_s = 10 ** 12

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

    print(int(min_s - 2))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = [i for i in range(1,int((n)**0.5 +1))]
a.reverse()
#print(a)
for j in range(len(a)):
    if n%int(a[j]) == 0:
        print(int(int(a[j]) + n//int(a[j]) -2))
        break

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N=int(input())
sq = int(math.sqrt(N))
if sq**2 == N:
    print(2*sq-2)
else:
    for i in range(sq):
        j=sq-i
        if not N%j:
            print(j+N//j-2)
            break

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:
        tmp = i + (n // i) - 2
        ans = min(ans, tmp)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math


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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

INF = 1e12
n = int(input())
cnt = INF
m = math.sqrt(n)

for i in range(1, math.ceil(m) + 1):
  if n % i == 0:
    j = n // i
    cnt = min(cnt, i + j - 2)
  else:
    continue

print(cnt)

Submission status: Accepted

Solution code:
import math

INF = 1e12
n = int(input())
ans = INF
m = math.sqrt(n)
for i in range(1,math.ceil(m)+1):
    if n%i==0:
        j = n//i
        ans = min(ans, i+j-2)
    else:
        continue
print(ans)

Submission status: Accepted

Solution code:
import math
n = int(input())
min = n - 1
#√n を算出
split = n / math.sqrt(n)


for i in range(int(split),0,-1):
    if n % i == 0:
        min = i + n/i -2
        break

print(int(min))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#start 1,1

n = int(input())

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

print(int(sum(mina)-2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict


def factorize(n):
    f = defaultdict(int)
    tmp = n
    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        while tmp % i == 0:
            f[i] += 1
            tmp //= i
    if tmp != 1:
        f[tmp] += 1
    if not f:
        f[n] += 1
    return f


def distance(s, d):
    a, b = 1, 1
    for i, si in enumerate(s):
        if si == "0":
            a *= factors[i]
            if a > d:
                return inf
        else:
            b *= factors[i]
            if b > d:
                return inf
    return a + b - 2


def answer(f, d):
    a = 1
    for i in f:
        a *= i
        if a > d:
            return inf
    return a + N // a - 2


def sa(f):
    d = inf
    for fi in f:
        d = min(d, answer(fi, d))
    return d


def comb(f):
    c = [[]]
    for k in f:
        ct = [[k] * i for i in range(1, f[k] + 1)]
        ci = c.copy()
        for cti in ct:
            for cii in ci:
                c.append(cii + cti)
    return [[1]] + c[1:]


# CONST = 1099511627776
N = int(input())
inf = 999999999999
factors = factorize(N)
print(sa(comb(factors)))

Submission status: Accepted

Solution code:
import math
n=int(input())
x=1
for i in range(2, math.ceil(math.sqrt(n))+1):
  if n%i==0 and x<i:
    x=i
print(x+n//x-2)

Submission status: Accepted

Solution code:
N = int(input())
ans = N - 1

i = 1
while i * i <= N:
    if N % i == 0:
        j = N // i
        ans = min(ans, (i - 1) + (j - 1))
    i += 1

print(ans)

Submission status: Accepted

Solution code:
import math
N = int(input())
L = []

def f(x):
  n = int(math.sqrt(x))
  for i in range(n,0,-1):
    if x % i == 0:
       return i
result = f(N) + N // f(N) - 2
print(result)

Submission status: Accepted

Solution code:
import math
n = int(input());
s = n - 1
for i in range(1,int(math.sqrt(n))+1):
    t = n / i
    if t.is_integer() == True and s > (t-1)+(i-1):
        s = (t-1)+(i-1)
print(int(s))

Submission status: Accepted

Solution code:
import math

n = int(input())


def make_divisors(n):
    min_sum = 10**12
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            a = i
            #if i != n // i:
            #    divisors.append(n//i)
            b = n//i
        if a+b < min_sum:
            min_sum = a+b
    # divisors.sort()
    # return divisors 
    return min_sum

print(make_divisors(n)-2)

Submission status: Accepted

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

sqrtN = int(math.sqrt(N)) + 1

for a in range(1, sqrtN)[::-1] :
    if N % a == 0:
        b = N / a
        if AB_MIN > a + b - 2:
            AB_MIN = a + b - 2

if AB_MIN == 1000000000000:
    AB_MIN = N - 1

print(int(AB_MIN))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 2019/10/29



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


n=int(input())
div=divisors(n)
res=float('inf')
for e in div:
    d=e+n//e-2
    res=min(res,d)
print(res)

Submission status: Accepted

Solution code:
N = int(input())
import math
n = math.floor(N**0.5)
s = N + 1
for i in range(1,n + 1):
    if N % i == 0:
        a = i + (N/i)
        s = a
print(int(s-2))

Submission status: Accepted

Solution code:
def dis(xa,ya):
    return abs(xa - 1) + abs(ya - 1)

n = int(input())
ans = 2*n
i = 1
while i*i <= n:
    if (n % i == 0):
        ans = min(ans, dis(i,n//i))
    i += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = 0

for i in range(1, int(N ** (1/2)+1)):
    if (N % i == 0):
        a = i

b = N // a

ans = (a - 1) + (b - 1)

print(ans)

Submission status: Accepted

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

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

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


#print(make_divisors(50))

i= 0

A = int(input())
A_d = make_divisors(A)
count = 0

while A_d[i]<= A**0.5:
    if A == 1:
        count = 0
        break
    count = i
    i += 1

result = []
result.append(A_d[count])
result.append(int(A / A_d[count]))

#print(result)

result_sum = result[0] + result[1] - 2

print(result_sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

N = int(input())
a = math.sqrt(N)
b = math.floor(a)
for i in range(b):
    c = N/(b-i)
    if c.is_integer():
        ans = c-2+b-i
        break;
print(int(ans))

Submission status: Accepted

Solution code:
import math

N = int(raw_input())

limit = int(math.sqrt(N)) + 1
n = float(N)
minimum = (1, 1, N)
for i in range(1, limit):
    j = n / i
    if not j.is_integer():
        continue
    j = int(j)
    distance = i + j - 2
    if minimum[2] > distance:
        minimum = i, j, distance
print(minimum[2])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
n = int(input())
num = math.sqrt(n)

l = []
for i in range(1,int(num+1)):
    if n % i == 0:
        l.append(i)
ans = int(n/max(l)) -1 + int(max(l)) - 1
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

#input
N = int(input())

#output
import math
M = int(math.sqrt(N))

ans = N
for i in reversed(range(1, M+1)):
    if float.is_integer(N/i):
        a = i
        b = int(N/i)
        c = a + b -2
        ans = min(ans, c)
        
print(ans)

Submission status: Accepted

Solution code:
N = int ( input() )
check_list = []
for i in range( 1, int( N**( 1/2 )+1 ) ):
    check = 0
    if N % i == 0:
        check = i + int( N / i ) - 2
        check_list.append(check)

print( min(check_list) )

Submission status: Accepted

Solution code:
import math

N = int(input())
ans = N-1
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:
# -*- coding: utf-8 -*-
n = int(input())
pair = (1,n)
#sqrt(n)(切り下げ)から1までを探索する。
from math import sqrt
mid = int(sqrt(n))
for i in range(mid,0,-1):
    if n % i==0:
        pair = (i,n//i)
        break
print(sum(pair)-2)

Submission status: Accepted

Solution code:
n = int(input())

ans = n+1
for x in range(int(n**0.5), 1, -1):
    if(n % x == 0):
        ans = x + n//x
        break
print(ans-2)

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
n = int(input())
i = int(n**(1/2))
while n%i:
	i-=1
j = n//i
ans = (i - 1) + (j - 1)
#print(i,j)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#144c
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_list = make_divisors(N)

if len(divisors_list)%2==0:
    print(divisors_list[int(len(divisors_list)/2)] + divisors_list[int(len(divisors_list)/2 -1)] -2)
else:
    print(divisors_list[int((len(divisors_list)-1)/2)] + divisors_list[int((len(divisors_list)-1)/2)] -2)

Submission status: Accepted

Solution code:
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]

def divisors(N: int) -> set:
    """ 約数の列挙・個数 """
    from math import sqrt

    # 1とその数はデフォで入れとく
    s = {1, N}
    # 終点はルート切り捨て
    for i in range(2, int(sqrt(N))+1):
        # 割り切れるなら、iとN/iを追加
        if N % i == 0:
            s.add(i)
            s.add(N // i)
    return s

N = INT()

ans = 10 ** 18
for div in divisors(N):
    ans = min(ans, div+N//div-2)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

# print(N)

ans = N
i = 1
while i * i <= N:
    # print(i)
    
    div, mod = divmod(N, i)
    
    if mod == 0:
        # print(i)
        # print(div)
        # print(mod)
        
        ans = min(ans, ((i - 1) + (div - 1)))
    
    i += 1
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
n=int(input())

import math
l=[]
v=n+1-2

for a in range(1,int(math.sqrt(n))+1):
  if n%a==0:
    b=int(n/a)
    temp=a+b-2
    if v>temp:
      v=temp
    
print(v)

Submission status: Accepted

Solution code:

a = int(input())

arr = []

for i in range(1,int(a**0.5)+1):
    b = a / i
    
    if a == int(b)*i:
        arr.append([int(i),int(b)])

arrb = [sum(arr[i]) for i in range(len(arr))]
print(min(arrb)-2)

Submission status: Accepted

Solution code:
n=int(input())

import math
l=[]
for a in range(1,int(math.sqrt(n))+1):
  if n%a==0:
    b=int(n/a)
    l.append(a+b-2)
    
print(min(l))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np
N = int(read())
U = 10**6 + 100
x = np.arange(1,U,dtype=np.int64)
div = x[N%x==0]
ans = (div + (N//div)).min() - 2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

for i in range(1, int(n**0.5)+1):
    if n%i == 0:
        x = i
        y = n/i

print(int(x+y-2))

Submission status: Accepted

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

ans = 10**18

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

print(ans)

Submission status: Accepted

Solution code:
import math

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

#約数の差が最小となるa,bを求める
min_diff = N
a = -1
b = -1

for i in range(1,n+1):
    if N % i == 0:
        j = N // i
        if min_diff > abs(i-j):
            a = i
            b = j
            min_diff = abs(i-j)

print(a+b-2)

Submission status: Accepted

Solution code:
from enum import Enum
from queue import Queue

import numpy as np

import collections
import bisect
import sys
import math

from scipy.sparse.csgraph import floyd_warshall

BIG_NUM = 200000000000
MOD = 1000000007
EPS = 0.000000001

N = int(input())
ans = N

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

print(ans)

Submission status: Accepted

Solution code:
Input = int(input())

aa = int(Input**0.5)

for i in range(1,aa+1):
  if Input%(aa+1-i) == 0:
    num1 = aa+1-i
    num2 = Input/(aa+1-i)
    break
print(int(num1 + num2 -2))

Submission status: Accepted

Solution code:
import math

def f(x):
    y = int(math.sqrt(x)) + 1
    for i in reversed(range(1,y)):
        if x % i == 0:
            a = x/i -1
            b = i -1
            sum = a+b
            return print(str(int(sum)))

f(int(input()))

Submission status: Accepted

Solution code:
N = int(input())

a = 1
ans = 2**64
while a*a<=N:
    b = N/a
    if b!=int(b):
        a += 1
        continue
    b = int(b)
    ans = min(ans, a+b-2)

    a += 1

print(ans)

Submission status: Accepted

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

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

L = make_divisors(n)
P = []
if math.sqrt(n) % 1 != 0:
    P.append(L[-1])
    P.append(L[-2])
else:
    P.append(L[-1])
    P.append(L[-1])

print(sum(P)-2)

Submission status: Accepted

Solution code:
import math
import copy

n=int(input())

i=int(n**0.5)
j=0
# print(i)

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

Submission status: Accepted

Solution code:
N = int(input())
lis = []
for a in range(1,int(N**0.5)+1):
    b = N // a
    if a * b == N:
       lis.append(a + b - 2)
print(min(lis))

Submission status: Accepted

Solution code:
N = int(input())

ans = N

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

Submission status: Accepted

Solution code:
# ABC144 C - Walk on Multiplication Table
import math
si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))
n = ni()
sqrtn = int(math.sqrt(n))
for i in range(sqrtn, 0, -1):
    if n%i == 0:
        j = n // i
        print(i+j-2)
        break

Submission status: Accepted

Solution code:
import math

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

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)
    return divisors

n=int(input())
a = make_divisors(n)
ans = 11451481019191919191919
for i in a:
	ans = min(ans, i+n/i)
	
print(int(ans-2))

Submission status: Accepted

Solution code:
n=int(input())

l=list()

m=n+1
for i in range(1,int(n**0.5)+1):
  if n%i==0:
    m = min(m, i+n//i)

print(m-2)

Submission status: Accepted

Solution code:
import math

n = int(input())
r_n = int(math.sqrt(n))
s = []

for i in range(1, r_n+1):
    if n%i == 0:
        s.append(n//i + i - 2)
print(min(s))

Submission status: Accepted

Solution code:
from math import sqrt

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

    ans = float('inf')
    for i in range(1, int(sqrt(N))+1):
        if N % i == 0:
            tmp = i + (N // i) - 2
            ans = min(ans, tmp)
        
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
qa=[n+1]
for i in range(1,int(n**(1/2))+1):
	if n%i==0:
		qa.append(i-1+(n//i)-1)
print(min(qa))

Submission status: Accepted

Solution code:
m = int(input())
a = {}
for i in range(1, int(m**0.5)+1):
    if m % i == 0:
        n = m // i
        a[i] = n
ans =[]
for i,j in a.items():
    b = i + j
    ans.append(b)

print(min(ans)-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)

    return divisors

N = int(input())
a = make_divisors(N)
b = []
c = []

for j in range(len(a)):
    for k in range(len(a)):
        if a[j] * a[k] == N:
            b.append([a[j],a[k]])

for l in range(len(b)):
    c.append(b[l][0] + b[l][1])

print(min(c) - 2)

Submission status: Accepted

Solution code:
#  --*-coding:utf-8-*--

import math

def main():
    N = int(input())
    
    N2 = int(math.sqrt(N))

    for k in range(N2, 0, -1):
        if N%k == 0:
            print(N//k + k - 2)
            return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

def rangesqrt(n):
    i = 1
    while i * i <= n:
        yield i
        i += 1

pos = ((d, N // d) for d in rangesqrt(N) if N % d == 0)
print(min(x - 1 + y - 1 for x, y in pos))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import sqrt


def Calc_Divisors(num):
    divisors = []
    for i in range(1, int(sqrt(num)) + 1):
        if num % i == 0:
            divisors.append(i)
            if i != num // i:
                divisors.append(num//i)
    divisors.sort()
    return divisors


if __name__ == "__main__":
    N = int(input())

    div = Calc_Divisors(N)

    ans = N
    for d in div:
        a = d
        b = N // a
        c = a + b - 2
        if c < ans:
            ans = c

    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

N = int(input())
s = N-1
a = N
b = 1
for i in range(int(N**0.5)+1, 1, -1):
        if N%i == 0:
            a = i
            b = N//i
            s = a + b -2
            break

print(s)

Submission status: Accepted

Solution code:
import math


N = int(input())
a = math.sqrt(N)

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

Submission status: Accepted

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

Submission status: Accepted

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

sqrtn = int(math.sqrt(n))
for i in range(sqrtn, 0, -1):
    if n % i == 0:
        j = n // i
        print(i+j-2)
        break

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

a = n
b = 1

for i in range(int(n**0.5)+1, 1 ,-1):
    if n%i == 0:
        a = i
        b = n//i
        break

print(a+b-2)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())

ar = np.arange(1, 10 ** 6 + 10)
div = ar[n % ar == 0]

ans = (div + n // div - 2).min()

print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import bisect
 
 
sys.setrecursionlimit(1000000000)
def input():
    return sys.stdin.readline().strip()
 
def iinput():
    return int(input())
 
def finput():
    return float(input())
 
def tinput():
    return input().split()
 
def rinput():
    return map(int, tinput())
 
def rlinput():
    return list(rinput())
 
def modst(a, s):
    res = 1
    while s:
        if s % 2:
            res *= a
        a *= a
        s //= 2
    return res

 
def main():
    n = iinput()
    for i in range(int(n ** 0.5) + 1, 0, -1):
        if n % i == 0:
            print(i - 2 + n // i)
            break
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math


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())
N_diviors = make_divisors(N)
N_diviors_len = len(N_diviors)
N_sqrt = math.sqrt(N)

for i in range(N_diviors_len):
    if N_sqrt <= N_diviors[i]:
        print(N_diviors[i] + int(N / N_diviors[i]) - 2)
        break

Submission status: Accepted

Solution code:
n = int(input())


def devide2(x):
    ar = []
    ar.append([1, x])
    for i in range(2, int(-(-x ** 0.5 // 1)) + 1):
        if x % i == 0:
            ar.append([i, x // i])
    return ar


kumi = devide2(n)
# print(kumi)

ans = 10 ** 12 + 1000
for i in range(len(kumi)):
    ans = min(ans, sum(kumi[i]))

print(ans - 2)

Submission status: Accepted

Solution code:
from operator import itemgetter
n = int(input())

list_p = []
for i in range(1, int(n**0.5)+1):

    j = n / i

    if j.is_integer():
        list_p.append([i, j, i+j])

list_sorted = sorted(list_p, key=itemgetter(2))
print(int(list_sorted[0][0] + list_sorted[0][1] - 2))

Submission status: Accepted

Solution code:
import numpy as np

def isPrime(n):
  s = int(np.sqrt(n))
  for i in range(2, s+1):
    if n % i == 0:
      return False
  return True

def sum_devisor(n):
  s = int(np.sqrt(n))
  while(n % s != 0):
    s -= 1
  return s + n/s

n = int(input())
if isPrime(n) == True:
  print(n-1)
else:
  print(int(sum_devisor(n)-2))

Submission status: Accepted

Solution code:
n = int(input())
import math
divisors = []
for i in range(1, math.ceil(math.sqrt(n))+1):
  if n%i==0:
    divisors.append([i, n//i])

ans = 2*10**12
for divisor in divisors:
  ans = min(ans, divisor[0]+divisor[1]-2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math


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


def nearN():
    if N_diviors_len == 2:
        print(N - 1)
        exit()

    for i in range(N_diviors_len):
        if N_sqrt <= N_diviors[i]:
            print(N_diviors[i] + int(N / N_diviors[i] - 2))
            break


N = int(input())
N_diviors = make_divisors(N)
N_diviors_len = len(N_diviors)
N_sqrt = math.sqrt(N)

nearN()

Submission status: Accepted

Solution code:
import math
N = int(input())
Min = float('inf')
Max = math.ceil(math.sqrt(N))
for i in range(1,Max+1):
    if N%i==0:
        b = N//i
        Min = min(Min,i+b-2)
print(Min)

Submission status: Accepted

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

i = 1
ans = 1e16

while i <= math.sqrt(n):
  b = divmod(n,i)
  if b[1] == 0:
    tmp = b[0] + n // b[0] - 2
    if tmp < ans:
      ans = tmp
  i+=1
   
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
i = 1
ans = N

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

print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())

for i in range(round(N**0.5)):
    if N%(i+1)==0:
        a,b=i+1,int(N/(i+1))

print(a+b-2)

Submission status: Accepted

Solution code:
N = int(input())
import math
sqrtN = math.floor(math.sqrt(N))
list = range(1, sqrtN+1)
a=[]
for i in list[::-1]:
    while N%i == 0:
        a.append(int(i))
        a.append(int(N/i))
        break
    if not a:
        pass
    else:
        break
f = a[0]+a[1]-2
print(f)