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:
def d(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

x = d(int(input()))

if len(x) % 2 == 1:
    print(x[len(x)//2] * 2 - 2)
else:
    print(x[len(x)//2-1] + x[len(x)//2] -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()
    return divisors

d = make_divisors(n)
for i in d:
    if i >= n ** 0.5:
        break
print((i + n // i) - 2)

Submission status: Accepted

Solution code:
import math
N = int(input())
sq = int(math.sqrt(N))
i_max = 0
j_max = 0
for i in range(sq,0,-1):
    if N % i == 0:
        i_max = i
        j_max = N // i
        break

print(i_max + j_max - 2)

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

n_divisors = []

for i in range(1, int(n ** 0.5) + 1) :
    if n % i == 0 :
        n_divisors.append(i)
        if n // i != i :
            n_divisors.append(n // i)
        else :
            n_divisors.append(i)

len_nd = len(n_divisors)

print(n_divisors[len_nd - 1] + n_divisors[len_nd - 2] - 2)

Submission status: Accepted

Solution code:
N=int(input())

li=[]
for i in range(1,int(N**0.5)+1):
  if N%i==0:
    li.append(i+N//i-2)
print(min(li))

Submission status: Accepted

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

yakusuu = []

for i in range(1,int(n**0.5)+1):
    if n%i == 0:
        yakusuu.append((i, n/i))

ans = yakusuu[0][0]+yakusuu[0][1]
for i in range(len(yakusuu)):
    wa = yakusuu[i][0]+yakusuu[i][1]
    ans = min(wa, ans)
print(int(ans)-2)

Submission status: Accepted

Solution code:
import math
N=int(input())
A=N
XX=math.sqrt(N)
XX=round(XX)
for x in range(1,XX+1):
    if N%x==0:
        B=N//x
        if x+B-2<A:
            A=x+B-2
print(A)

Submission status: Accepted

Solution code:
import math

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

r = 1
for n in range(2, N_sqrt+1):
  r = n if N%n == 0 else r

print(r + N//r - 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()
    return divisors

ans=make_divisors(N) #listで返る
minima=10**12+1

for i in ans:
  pair=N/i
  minima=min(i+pair,minima)

print(int(minima-2))

Submission status: Accepted

Solution code:
import math

n = int(input())

sqrt_n = int(math.sqrt(n))

a = 0
b = 0

for i in range(sqrt_n):
    if n % (sqrt_n - i) == 0:
        a = int(sqrt_n - i)
        break

b = n // a

print(a + b - 2)

Submission status: Accepted

Solution code:
import math
def get_yakusuu(n):
  a = 1
  b = n
  f_max = int(math.sqrt(n)) + 1
  for i in reversed(range(1, f_max)):
    if n % i == 0:
      a = i
      b = n // i
      break
  return a, b
    
N = int(input())

nearest = get_yakusuu(N)
print(nearest[0] + nearest[1] - 2)

Submission status: Accepted

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

print(min(arr)-2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = 10**15

def 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())
fact = divisors(n)

ans = INF 
for item in fact:
    ans = min(ans, item + n // item - 2)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
min = 10000000000001
for i in range(1, 1000001):
    if N % i == 0:
        val = i + int(N / i) - 2
    if min > val:
        min = val

print(min)

Submission status: Accepted

Solution code:
import math
n = int(input())
ans = float('inf')
sqrt_val = math.ceil(math.sqrt(n))
for i in range(sqrt_val):
    if n % (i+1) == 0:
        ans = min(ans, (i + (n//(i+1)) - 1))

print(ans)

Submission status: Accepted

Solution code:
def divs(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

N = int(input())
x = divs(N)
a1 = x[-1]-1
a2 = x[-2]-1
print(a1+a2)

Submission status: Accepted

Solution code:
n = int(input())
i = 1
ans = 10**12
while (i * i < n):
    i += 1
    if n % i == 0:
        ans = min(ans, (i-1) + (n//i - 1))
ans = min(ans, n-1)
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



n = int(input())
x = make_divisors(n)

if len(x) % 2 == 0:
    a = x[int((len(x)-1)/2)]
    b = x[int((len(x)+1)/2)]
else:
    a = x[int(len(x)/2)]
    b = x[int(len(x)/2)]

print(a+b-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()
    return divisors

l = make_divisors(n)

if len(l)%2==0:
    p = len(l)//2 - 1
    print(l[p] + l[p+1] - 2)
else:
    p = len(l)//2
    print(l[p] + l[p] -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()
    return divisors

t = make_divisors(N)
if len(t) % 2 != 0:
    i = len(t) // 2
    print(t[i] + t[i] - 2)
else: 
    i = len(t) // 2 - 1
    print(t[i] + t[i+1] - 2)

Submission status: Accepted

Solution code:
N=int(input())

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

    return divsets

divs = make_divsets(N)
#print(divs)

min = 1000000000000019

for (a, b) in divs:
    steps = a-1 + b-1
    if steps < min:
        min = steps
print(min)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

i = 1
u = [[1, N]]
while i ** 2 <= N:
  if N / i == N // i:
    u[0] = [i, N // i]
  i += 1
    
print(u[0][0] + u[0][1] - 2)

Submission status: Accepted

Solution code:
import numpy as np
N=int(input())
root=int(np.sqrt(N)//1)
a=N
b=N
for i in range(root, 0, -1):
    if N%i==0:
        a=i
        b=N//i
        break
print(a-1+b-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
val = int(n**0.5)
if n % val == 0:
    print(val + n // val - 2)
else:
    while n % val != 0:
        val -= 1
    print(val + n // val - 2)

Submission status: Accepted

Solution code:
N = int(input())
lis = []
for i in range(1,10**6+1):
  a = N/i
  if a.is_integer():
    lis.append(N/i+i-2)
if len(lis) == 0:
  print(N-1)
else:
  print(int(min(lis)))

Submission status: Accepted

Solution code:
import math

A = int(input())

start = math.ceil(math.pow(A, 0.5))
while A % start != 0:
    start -= 1

print((start + (A // start) - 2))

Submission status: Accepted

Solution code:
import math

n = int(input())
counter = math.ceil(math.sqrt(n))
while True:
    if n % counter == 0:
        break
    counter -= 1
print(counter + (n // counter) - 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def run(N):
    lists = [N-1]
    for i in range(2, int(N**(1/2))+1):
        if N % i == 0:
            lists.append(i + (N // i) - 2)
    return min(lists)


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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
n=int(input())
b=int(math.sqrt(n))
for i in range(1,b+1)[::-1]:
    if n/i==n//i:
        c=i
        d=n//i
        break
print(c+d-2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

n = int(input())

l = n - 1

for i in range(1, int(math.sqrt(n)) + 1):
  if n % i == 0:
    m = n // i
    if l > (m + i - 2):
      l = m + i - 2
      
print (l)

Submission status: Accepted

Solution code:
import math

n = int(input())
ary = []
for i in range(1, int(math.sqrt(n) + 1)):
    if n % i == 0:
        j = n // i
        ary.append(i + j - 2)
print(min(ary))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
cost = []
i = 2
while i <= n**0.5:
  if n % i == 0:
    cost.append(i+n/i-2)
  i += 1
if len(cost) == 0:
  cost.append(n-1)
print(int(min(cost)))

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
  
A= input()
A= int(A)
X = 100000000000000000000000
for i in make_divisors(A):
  Y = A / i
  if Y + i < X:
    X = Y + i
print(int(X-2))

Submission status: Accepted

Solution code:
n = int(input())
ans = float('inf')
for i in range(1, n):
    if i**2 > n:
        break
    if n % i ==0:
        tmp = i + (n//i) -2
        if tmp<ans:
            ans = tmp
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

l = [i for i in range(1, int(N**0.5) + 1) if N % i == 0]
n = int(N / l[-1])
print(l[-1] + n - 2)

Submission status: Accepted

Solution code:

def nums():
    return list(map(int,input().split(" ")))
def num():
    return int(input())
def yakusu(n):
    res = set()
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            res.add((i,int(n/i)))
    return res
"""
a,b = nums()
if a <= 9 and b <= 9:
    print(a * b)
else:
    print(-1)
"""
"""
n = num()
qq = []
for i in range(10):
    for j in range(10):
        qq.append(i * j)
if n in qq:
    print("Yes")
else:
    print("No")
"""
N = num()
y = yakusu(N)

min = -1

for i in y:
    x,y = i
    if min == -1:
        min = x + y
    elif min > x + y:
        min = x+y

print(min-2)

Submission status: Accepted

Solution code:
from fractions import gcd

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

divs = make_divisors(N)
#print(divs)
min_cnt = 10 ** 13
for d in divs:
    #print(d)
    x = N // d
    if (d + x - 2) < min_cnt:
        min_cnt = d + x - 2
print(min_cnt)

Submission status: Accepted

Solution code:
from math import sqrt
import sys
def input(): return sys.stdin.readline().strip()

def main():
    n = int(input())
    i = int(sqrt(n))
    while i > 0:
        if n % i == 0:
            print(i - 1 + (n // i) - 1)
            return
        i -= 1
main()

Submission status: Accepted

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

N = int(input())

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

Submission status: Accepted

Solution code:
N = int(input())

def get_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

divs = get_divisors(N)
divs.sort()
if len(divs)%2==1:
    idx = (len(divs)-1) // 2
    print(2 * (divs[idx] - 1))

else:
    idx = len(divs)//2 - 1
    print(divs[idx] - 1 + divs[idx + 1] - 1)

Submission status: Accepted

Solution code:
import sys
import math
import os

input = sys.stdin.readline

n = int(input())
ans = n
for i in range(1,math.ceil(math.sqrt(n))+1):
    if n%i==0:
        ans = min(ans,i+n//i-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:
        a = i

b = n//a
ans = a + b -2
print(ans)

Submission status: Accepted

Solution code:
# At ABC 144 C (300)
import math

def f(n):
    min_ = float("inf")
    end = int(math.sqrt(n) + 1)
    for div in range(1, end + 1):
        if n % div == 0:
            r = n // div
            min_ = min(min_, (div - 1) + (r - 1))
    return min_

assert f(10) == 5
assert f(50) == 13
assert f(10000000019) == 10000000018

n = int(input())
ans = f(n)
print(ans)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

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

def main():
  n = ni()

  x = divi(n)
  
  ans = 1e18+7
  for i in range(0, len(x), 2):
    # print(x[i])
    try:
      ans = min(ans, x[i] - 1 + x[i+1] - 1)
    except:
      ans = min(ans, (x[i] - 1) * 2)  
  print(ans)
  
main()

Submission status: Accepted

Solution code:
import numpy as np
import sys

N = int(input())

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

Submission status: Accepted

Solution code:
n = int(input())

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

print(m - 2)

Submission status: Accepted

Solution code:
import math

N=int(input())
SQ=math.floor(math.sqrt(N))

for i in range(SQ):
    if N%(SQ-i)==0:
        p=(SQ-i)
        q=N//(SQ-i)
        break
print(p+q-2)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
#ABC144C
import sys
import math
import collections
#sys.setrecursionlimit(100000)


n=int(input())

#tmp = input().split()
#a,b = list(map(lambda a: int(a), tmp))

sta=math.floor(math.sqrt(n))

#print(sta)

for i in range(sta):
	if(n%(sta-i)==0):
		print(n//(sta-i)+sta-i-2)
		sys.exit()

Submission status: Accepted

Solution code:
N = int(input())

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

walk_count = x - 1 + y - 1
print(walk_count)

Submission status: Accepted

Solution code:
import math

N = int(input())

upper = int(math.sqrt(N)) + 1
cell = 1 + N
for i in range(2, upper):
    if N % i == 0:
        j = N // i
        cell = min(cell, i+j)

print(cell-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)
            divisors.append(n//i)
    return divisors
    
divisors = make_divisors(N)

x_ax = divisors[-2] - 1
y_ax = divisors[-1] - 1

print(x_ax + y_ax)

Submission status: Accepted

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

if __name__ == "__main__":
    f_c()

Submission status: Accepted

Solution code:
from math import sqrt

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

    ans = 10**12+1
    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:
import math
n=int(input())
s=[]
for i in range(1, int(math.sqrt(n)) + 1):
    if n%i==0:
        s.append(int(i)+int(n/i)-2)
print(min(s))

Submission status: Accepted

Solution code:
import sys
import math

n = int(input())

sqrtn = int(math.sqrt(n))

for i in range(sqrtn, 0, -1):
	if n % i == 0:
		res = int((n / i) - 1 + i - 1)
		print(res)
		sys.exit()

Submission status: Accepted

Solution code:
import math

def read():
    N = int(input().strip())
    return N, 

def solve(N):
    pairs = []
    n = math.ceil(math.sqrt(N)) + 1
    for i in range(1, n):
        j = N // i
        if i * j == N:
            pairs.append((i, j))
    min_moves = 1000000000000
    for pair in pairs:
        min_moves = min((pair[0] - 1) + (pair[1] - 1), min_moves)
    return min_moves

if __name__ == '__main__':
    inputs = read()
    print("%s" % solve(*inputs))

Submission status: Accepted

Solution code:
n = int(input())
import math
m = int(math.sqrt(n))+1
anss = []
for x in range(1, m+1):
  y = n//x
  z = n%x
  if z == 0:
    anss.append(abs(x-1)+abs(y-1))
print(min(anss))

Submission status: Accepted

Solution code:
n = int(input())

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

divs = divisors(n)

ans = n-1
for div in divs:
    div2 = n//div
    ans = min(ans, div + div2 - 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

N = int(input())
minimam = 10**12
for x in make_divisors(N)[::2]:
    minimam = min(minimam, int(x + N/x - 2))
print(minimam)

Submission status: Accepted

Solution code:
N = int(input())

ans = N-1

def get_steps(a, b):
    return (a-1) + (b-1)


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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

Solution code:
import sys

def get_array(): return list(map(int, sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()

def all_factors(n):
    """returns a sorted list of all distinct factors of n"""
    small, large = [], []
    for i in range(1, int(n**0.5) + 1, 2 if n & 1 else 1):
        if not n % i:
            small.append(i)
            large.append(n // i)
    if small[-1] == large[-1]:
        large.pop()
    large.reverse()
    small.extend(large)
    return small

def solve(a,b):
    return (a-1)+(b-1)


n = int(input())
facts = all_factors(n)
length = len(facts)
mini = 10**15 + 7
if length&1:
    for i in range(length//2+1):
        mini = min(mini, solve(facts[i], facts[~i]))
    print(mini)
else:
    for i in range(length//2):
        mini = min(mini, solve(facts[i], facts[~i]))
    print(mini)

Submission status: Accepted

Solution code:
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())

x = ii()

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
y = make_divisors(x)

m = 10000000000000

if(len(y)%2==1):
    for i in range(0,len(y),2):
        if(i==len(y)-1):
            if m > (y[i]-1)*2:
                m = (y[i]-1)*2
        else:
            if m > y[i]+y[i+1]-2:
                m = y[i]+y[i+1]-2
else:
    for i in range(0,len(y),2):
        if m > y[i]+y[i+1]-2:
                m = y[i]+y[i+1]-2

print(m)

Submission status: Accepted

Solution code:
import math

N = int(input())

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

ans = (a-1) + (int(N/a)-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-1 + j-1)
        break

Submission status: Accepted

Solution code:
# 全ての約数を列挙。o(√N)。
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())
ans = float('inf')
for i in make_divisors(n):
    j = n//i
    temp = i + j - 2
    ans = min(ans, temp)
print(ans)

Submission status: Accepted

Solution code:
# coding:utf8
from collections import  Counter

n=int(input())

ans=10**12+1

def divisor(n): #nの約数を全て求める
    i = 1
    table = []
    while i * i <= n:
        if n%i == 0:
            table.append(i)
            table.append(n//i)
        i += 1
    table = list(table)
    table.sort()
    return table

t=divisor(n)
for i in range(0,len(t)//2):
    tmp=t[i]+(n//t[i])-2
    ans=min(ans,tmp)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

a = int(n ** 0.5)

ans = n - 1

for i in range(2, a + 1):
    if n % i == 0:
        ans = i + n //i - 2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function

import os
import sys
import math
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


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

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


# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

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

q = int(math.floor(math.sqrt(N)))
ret = float("inf")
for i in range(1, q + 1):
	if N % i == 0:
		ret = min(i + int(N / i), ret)
		
		
print(ret - 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())
a = make_divisors(n)
b = [ int(i + n / i)  for i in a]
#print(a)
#print(b)
ans = min(b) - 2
print(ans)

Submission status: Accepted

Solution code:
from collections import *
from math import *
n = int(input())
x = int(sqrt(n))
for i in range(x,0,-1):
    if(n%i == 0):
        print(i-1+n//i-1)
        break

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
a = int(input())
b = make_divisors(a)
b_len = len(b)
if b_len % 2 == 0:
    print(b[b_len//2-1] + b[b_len//2] - 2)
else:
    print(2 * b[b_len//2 ] - 2)

Submission status: Accepted

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

n = int(input())

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

Submission status: Accepted

Solution code:
N = int(input())
def make_div(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 sorted(divisors)
div = make_div(N)
minn = 100000000000000000000
for i in range(len(div)):
    a,b = div[i],div[-i-1]
    if a > b:
        break
    if (a + b) < minn:
        minn = a + b

print(minn-2)

Submission status: Accepted

Solution code:
N=input()

N_sqrt=int(N**0.5)+1

S=set()
S.add(1)
S.add(N)

for i in range(1,N_sqrt+1):
    if N%i==0:
        S.add(i)
        S.add(N/i)


ans=float("inf")
for x in S:
    y=N/x
    ans=min(ans,x+y-2)

print ans

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

print(a + b - 2)

Submission status: Accepted

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