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

Submission status: Accepted

Solution code:
import math

N = int(input())
min_move_count = 10**13

for i in range(1, int(math.sqrt(N)) + 1):
    if N % i == 0:
        move_count = i + (N // i) - 2
        if move_count < min_move_count:
            min_move_count = move_count

ans = min_move_count
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
import math

N = int(input())

min_move_count = float("inf")
for i in range(1, int(math.sqrt(N)) + 1):
    if (N / i).is_integer():
        move_count = i + (N // i) - 2
        if move_count < min_move_count:
            min_move_count = move_count

print(min_move_count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(n):
  l = []
  for i in range(int(n**(1/2))):
    if n % (i+1) == 0:
      a = i + 1
      b = n // a
      l.append(a+b-2)
  return min(l)

print(solve(int(input())))

Submission status: Accepted

Solution code:
N = int(input())
root = int(N**0.5)
for i in range(root,-1,-1):
  if N % i ==0:
    break

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

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7


#############
# Main Code #
#############

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

make_divisors(N)

ans = float('inf')
for i in divisors:
    a, b = i
    ans = min(ans, a + b - 2)
print(ans)

Submission status: Accepted

Solution code:
from math import sqrt
N = int(input()) # 2 <= N <= 10^12
# (1,1) (x,y) = x*y
a = N-1
for x in range(2, int(sqrt(N))+1):
  q, r = divmod(N, x)
  if r == 0:
    a = min(x-1 + q - 1, a)
print(a)

Submission status: Accepted

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

n = int(read())
lim = int(n ** 0.5)
fact = [1]
for i in range(2, lim + 1):
    if (n % i == 0):
        fact.append(i)
    else:
        pass

if (len(fact) == 1):
    ans = n - 1

else:
    tar = fact[-1]
    ans = tar + int(n // tar) - 2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

list_ans=[]
for i in range(2,n_+1):
  if n%i==0:
    ans=i+(n//i)-2
    list_ans.append(ans)
    
  
if not list_ans:
  print(n-1)
  exit()
print(min(list_ans))

Submission status: Accepted

Solution code:
import math
N=int(input())
M =int(math.sqrt(N))
for i in range(M):
    if N%(M-i)==0:
        K=N//(M-i)
        L=M-i
        break
print(L+K-2)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def isqrt(n):
    if n > 0:
        x = 1 << (n.bit_length() + 1) // 2
        y = (x + n // x) // 2
        while y < x:
            x = y
            y = (x + n // x) // 2
        return x
    elif n == 0:
        return 0
    else:
        raise ValueError("isqrt() argument must be nonnegative")


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

    n = isqrt(N)
    ans = INF
    for i in range(1, n + 1):
        if N % i == 0 and ans > N // i + i - 2:
            ans = N // i + i - 2

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
def main():
    N = int(input())
    min_sum_xy = N + 1
    for x in range(2, math.floor(N**(1/2))+1):
        if N % x == 0:
            y = N // x
            min_sum_xy = min(x + y, min_sum_xy)
    print(min_sum_xy - 2)
main()

Submission status: Accepted

Solution code:
N=int(input())

a=1
b=N//a
dis=abs(b-a)

for i in range(1,int(N**0.5)+1):
    if N%i==0:
        _a=i
        _b=N//i
        _dis=abs(_b-_a)
        if _dis<dis:
            a=_a
            b=_b
            dis=_dis

print(a-1+b-1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

m= int(n**0.5)

while m >= 1:
    if n%m ==0:
        print(int(n/m)+m-2)
        exit(0)

    m -=1

Submission status: Accepted

Solution code:
n = int(input())
def divisor(n): 
    i = 1
    out = float('inf')
    while i * i <= n:
        if n%i == 0:
          out = min((i+n//i),out)
        i += 1
    return out
print(divisor(n)-2)

Submission status: Accepted

Solution code:
n = int(input())

m= int(n**0.5)

while m >= 1:
    if n%m ==0:
        print(int(n/m)+m-2)
        exit(0)
    else:
        m -=1

Submission status: Accepted

Solution code:
n = int(input())
def divisor(n): 
    i = 1
    a = 0
    b = 0
    out = float('inf')
    while i * i <= n:
        if n%i == 0:
            a = i
            b = n//i
        if a+b < out:
          out = a+b
        i += 1
    return out
print(divisor(n)-2)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    i = 2
    minv = int(10**12)
    while i*i <= n:
        if n % i == 0:
            a = i
            b = int(n / i)
            minv = min(minv, a+b-2)
        i += 1
    if minv < int(10**12):
        print(minv)
    else:
        print(n-1)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())

def is_prime(n):
    if n == 1:
        return False
    for i in range(2,int(n**0.5)+1):
        if n % i == 0:
            return False
    return True
  
def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]
if is_prime(n):
  print(n-1)
  exit()
  
#print(make_divisors(n))
div=make_divisors(n)
l=[]
for i in div:
  ii=n//i
  l.append(ii+i)

print(min(l)-2)

Submission status: Accepted

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

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

Submission status: Accepted

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

a = math.ceil(math.sqrt(N))

yaku = {1,N}

for i in range(2,a+1):
  if N % i == 0:
    yaku.add(i)
    yaku.add(N // i)

sl = len(yaku)
lyaku =list(yaku)
lyaku.sort()
if sl % 2 == 1:
  print((lyaku[sl//2]-1)*2)
else:
  print(lyaku[sl//2]+lyaku[sl//2-1] -2)

Submission status: Accepted

Solution code:
import math
N = int(input())
MIN = math.inf
Limit = math.ceil(math.sqrt(N))
for T in range(1,Limit+1):
    if N%T==0 and T+N//T<=MIN:
        MIN = T+N//T
print(MIN-2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

resolve()

Submission status: Accepted

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

minimum = n
for i in range(1, n2 + 1): #ｎまで調べると無駄な調べが入って間に合わんから
    if (n % i == 0):    #√ｎまででおｋ
        minimum = min(minimum, (n // i -1) + (i - 1))


print(minimum)

Submission status: Accepted

Solution code:
N = int(input())
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()

n = len(divisors)
i = len(divisors) // 2

A, B = 0, 0
if n % 2 == 0:
    A, B = divisors[i - 1], divisors[i]
else:
    A, B = divisors[i], divisors[i]

print(A - 1 + B - 1)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import math
import sys
N = int(input())

def divisor(N):
    lst = []
    for i in range(2,math.ceil(math.sqrt(N))+1):
        if N%i == 0:
            lst.append(i)
            #if i != int(N/i):
                #lst.append(int(N/i))
    #lst.sort()
    return lst
    
ans = divisor(N)
#print(ans,len(ans))
if N == 2:
    print(1)
    sys.exit()
    
if len(ans) > 0:
    A = ans[-1]
    if A*A < N:
        B = int(N/A)
        print(A+B-2)
    else:
        print(2*A-2)
else:
    print(N-1)

'''
if len(ans)%2 == 0 and len(ans)>1:
    A = ans[len(ans)//2-1]
    B = ans[len(ans)//2]
    #print(A,B)
    print(A+B-2)
elif len(ans)%2 == 1 and len(ans)>1:
    A = ans[len(ans)//2]
    print(2*A-2)
else:
    print(N-1)
'''

Submission status: Accepted

Solution code:
import math

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

minimum=n
for i in range(1,n2+1):
    if(n%i==0):
        minimum=min(minimum,(n//i-1)+(i-1))

print(minimum)

Submission status: Accepted

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

N_sq=N**(1/2)

for i in range(1,int(N_sq)+1):
    if N%i ==0:
        div = [i,int(N/i)]
        
print(sum(div)-2)

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
n = int(input())
x = 10**13
for i in range(1,int(n**.5)+1):
    if n%i == 0:
        x = min(x,n//i+i-2)
print(x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,a=int(input()),float("inf")
for i in range(1,5*10**6+1):
	if n%i==0:j=n//i;a=min(a,i+j-2)
print(a)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N = int(input())
Min = 1e12
for a in range(1, int(math.sqrt(N)) + 1):
    if (N % a) == 0:
        Min = min(Min, a + N//a - 2)
print(Min)

Submission status: Accepted

Solution code:
def divisor(n): 
    i = 1
    table = []
    while i * i <= n:
        if n%i == 0:
            table.append(i)
            table.append(n//i)
        i += 1
    table = list(set(table))
    return table

N = int(input())
L = divisor(N)
L = sorted(L)
ans = float("inf")

for i in L:
    p = i
    q = N // i
    temp = (p-1)+(q-1)
    ans = min(ans, temp)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
r=100000000000000000000000000000
for i in range(1,int(pow(n,0.5))+1):
  if(n%i==0):
    r=min(r,abs(i-1)+abs(n//i-1))
print(r)

Submission status: Accepted

Solution code:
import math

N = int(input())

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

lst = yakusuu(N)

ans = 10**18
for i in range(len(lst)):
    a = lst[i]
    b = N//lst[i]
    
    ans = min(ans, (a-1)+(b-1))
print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())
a = 1
b = 1
tmp_ans = 0
ans = 10**12

for i in range(1, int(math.sqrt(n))+1):
    j = n // i
    if n == i*j:
        tmp_ans = (i-a) + (j-b)
        ans = min(ans, tmp_ans)
print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())
a = 1
b = 1
tmp_ans = 0
ans = 10**12

for i in range(1, int(math.sqrt(n))+1):
    j = n // i
    if n == i*j:
        tmp_ans = (i-a) + (j-b)
        if tmp_ans < ans:
            ans = tmp_ans
print(ans)

Submission status: Accepted

Solution code:
import math
def is_prime(n):
    if n == 1: return False

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False
    return True

n = int(input())
a = 1
b = 1
tmp_ans = 0
ans = 10**12

if is_prime(n) == True:
    print(n - 1)
elif is_prime(n) == False:
    for i in range(2, int(math.sqrt(n))+1):
        j = n // i
        if n == i*j:
            tmp_ans = (i-a) + (j-b)
            if tmp_ans < ans:
                ans = tmp_ans
    print(ans)

Submission status: Accepted

Solution code:
import sys

def I(): return int(sys.stdin.readline().rstrip())

N = I()

d = []  # Nの約数(Nが平方数のとき、N**.5を敢えて2回dに入れている)
for i in range(1,int(N**.5)+1):
    if N % i == 0:
        d.append(i)
        d.append(N//i)

# 目的地の(i,j)は、i*j==Nを満たす(i,j)の中でiとjが最も近いもの

d.sort()
a = len(d)

print(d[a//2] + d[a//2-1] - 2)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし

N = I()

d = []
for i in range(1,int(N**.5)+1):
    if N % i == 0:
        d.append(i)
        if i != N//i:
            d.append(N//i)

d.sort()
a = len(d)

if a % 2 == 0:
    print(d[(a-1)//2] + d[(a+1)//2] - 2)
else:
    print(d[(a-1)//2]*2 - 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[2]:


import math


# In[14]:


N = int(input())


# In[15]:


for x in range(1,int(math.sqrt(N)+1))[::-1]:
    if N%x == 0:
        print((x-1)+(N//x-1))
        break


# In[ ]:

Submission status: Accepted

Solution code:
n = int(input())
maxn = int(n ** 0.5)
for i in range(maxn):
    p = maxn - i
    if n % p == 0:
        q = n // p
        break
print(p + q - 2)

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

Solution code:
N=int(input())
list=[]
import math
for i in range(1,int(math.sqrt(N))+1):
  if N%i==0:
    list.append(i)
k=list[-1]
print(k+int(N/k)-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)%1==0:
        ans=min(ans,((n/i)-1)+(i-1))
print(int(ans))

Submission status: Accepted

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

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

print(i + j -2)

Submission status: Accepted

Solution code:
import math

n = int(input())

t = int(math.sqrt(n))

ans = 0

for i in reversed(range(t+1)):
    if n % i == 0:
        ans = n//i+i
        break
        
print(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)

    divisors.sort()
    return divisors

n = int(input())
a = (max(make_divisors(n)))
b = n//a
print(a+b-2)

Submission status: Accepted

Solution code:
import sys
import math
input = lambda: sys.stdin.readline().rstrip()

def main():
    n = int(input())
    min_ = n - 1
    for a in range(2, int(math.sqrt(n))+1):
        if n % a == 0:
            b = n // a
            min_ = min((a-1) + (b-1), min_)
    print(min_)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
def makediv(n):
	lower_divisors , upper_divisors = [], []
	i = 1
	while i*i <= n:
		if n % i == 0:
			lower_divisors.append(i)
			if i != n // i:
				upper_divisors.append(n//i)
		i += 1
	return lower_divisors + upper_divisors[::-1]
k = makediv(n)
ans = 10**18
for i in k:
	t = n//i
	ans = min((t-1)+(i-1), ans)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
d = 1
ret = -1
while d * d <= n:
    if n % d == 0:
        ret = d + n // d
    d += 1
print(ret - 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import os, sys, re, math

N = int(input())

s = math.sqrt(N)
upper = math.ceil(s)
lower = math.floor(s)

val1 = N
val2 = N
while val1 == N and val2 == N:
    if N % upper == 0:
        val1 = N // upper + upper - 2
    if N % lower == 0:
        val2 = N // lower + lower - 2
    upper += 1
    lower -= 1

print(min(val1, val2))

Submission status: Accepted

Solution code:
import math

n = int(input())
nn = int(math.sqrt(n))
for i in range(nn, 0, -1):
	if n / i == n // i:
		j = n // i
		print (i - 1 + j - 1)
		exit()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

N = int(input())
result = 10**12
for i in range(1, int(N ** 0.5)+1):
    if N % i == 0:
        result = min(result, (N // i) + i - 2)

print(result)

Submission status: Accepted

Solution code:
n = int(input())
ans = 100000000000000
for i in range(1, 1000001):
  if n % i == 0:
    a = i - 1
    b = n // i - 1
    ans = min(ans, a + b)
print(ans)

Submission status: Accepted

Solution code:
import math
import sys
from collections import deque
import heapq
import copy
import itertools
from itertools import permutations
def mi() : return map(int,sys.stdin.readline().split())
def ii() : return int(sys.stdin.readline().rstrip())
def i() : return sys.stdin.readline().rstrip()
a=ii()
b=int(math.sqrt(a))
for i in range(b):
  if a%(b-i)==0:
    print(int(a/(b-i))+b-i-2)
    sys.exit()

Submission status: Accepted

Solution code:

from math import sqrt

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

Submission status: Accepted

Solution code:
def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

N=int(input())
div=make_divisors(N)

from math import sqrt

tmpmin=10**16
for i in range(len(div)):
  if tmpmin>abs(sqrt(N)-div[i]):
    tmpmin=abs(sqrt(N)-div[i])
    minidx=i

a=div[minidx]
b=N//a

print(a-1+b-1)

Submission status: Accepted

Solution code:
n = int(input())
lower_divisors, upper_divisors = [], []
i = 1
while i*i <= n:
    if n % i == 0:
        lower_divisors.append(i)
        upper_divisors.append(n // i)
    i += 1

ans = (lower_divisors[-1] - 1) + (upper_divisors[-1] - 1)

print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
mi = 10**15
ans_i = 0
ans_j = 0
for i in range(2, 10**6+1):
    if N % i == 0:
        j = N // i
        if mi > (i+j-2):
            mi = i+j-2
            ans_i = i
            ans_j = j
if ans_i+ans_j != 0:print(ans_i+ans_j-2)
else:print(N-1)

Submission status: Accepted

Solution code:
n=int(input())
tmp=float("inf")
for a in range(1,int(n**(1/2))+1):
    if n%a==0:
        b=n/a
        if tmp>a+b:
            tmp=a+b
print(int(tmp-2))

Submission status: Accepted

Solution code:
import math
N=int(input())
sqN=math.ceil(math.sqrt(N))
ans=10**15
for i in range(1,sqN+1):
  if N%i==0:
    b=int(N/i)
    if ans>=(i+b-2):
      ans=i+b-2
print(ans)

Submission status: Accepted

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

ans = 10**13

for a in range(1,math.ceil(math.sqrt(n))+1):
    b = n//a
    if a*b == n:
        ans = min(a+b-2,ans)
    else:
        pass

print(ans)

Submission status: Accepted

Solution code:
import itertools
N = int(input())
#print(N)

ans = []
for i in range(1,10**6+1):
    if N%i == 0:
        #print(i)
        j = N//i
        ans.append(i+j-2)
    else:
        continue

print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
from math import ceil
n=int(input())
x=1
for i in range(ceil(pow(n,0.5)),1,-1):
  if n%i==0:
    x=i
    break
y=n//x
print(x+y-2)

Submission status: Accepted

Solution code:
def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors

n=int(input())

l=make_divisors(n)

print(n//l[-1]+l[-1]-2)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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


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

    div = make_divisors(n)
    res = f_inf
    for num in div:
        x, y = num, n // num
        res = min(res, x - 1 + y - 1)
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
# C - Walk on Multiplication Table
# https://atcoder.jp/contests/abc144/tasks/abc144_c

n = int(input())

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

ans = N - 1
for i in range(2, N + 1):
    if i * i > N:
        break
    div, mod = divmod(N, i)
    if mod == 0:
        v = i - 1 + div - 1
        ans = min(v, ans)
print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**6)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
x = int(n**0.5)
while n%x!=0:
    x-=1
x,y = x-1, n//x -1
print(x+y)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import *

N = int(input())
ans = 10**18
i = 1

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

print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())

v = math.floor(math.sqrt(n))
while True:
    if n % v == 0:
        print(v + (n // v) - 2)
        exit()
    v -= 1

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
m=float("inf")
for i in range(1,1000001):
    if n%i==0:
        m=min(m,n//i+i-2)
print(m)

Submission status: Accepted

Solution code:
import math

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

for i in reversed(range(1,n+1)) :
    if N % i == 0 :
        a = i
        b = N // i
        break

print(a+b-2)

Submission status: Accepted

Solution code:
N = int(input())
curr_min = float('inf')
for i in range(1, int(N**0.5 + 1)):
    if N // i * i == N:
        curr_min = min(curr_min, (i - 1) + (N // i - 1))
print(curr_min)

Submission status: Accepted

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

x=1
y=1
for i in range(n_sqrt):
    if n%(i+1)==0:
        x=i+1
        y=n//(i+1)

print(x+y-2)

Submission status: Accepted

Solution code:
import math
import numpy as np

n = int(input())
n_2 = int(math.sqrt(n) + 1)
x = np.arange(1, n_2)
div = x[n % x == 0]
ans = 10 ** 12
for i in div:
    j = (n // i) + i - 2
    ans = min(ans, j)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
ans = 10 ** 20 + 7
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 numpy as np

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import permutations

N = int(input())
i, j = 1, 1
max0 = -1
ans = 0
while i <= N // i:
    if N % i != 0:
        i += 1
        continue
    else:
        j = N // i
        if max0 > abs(i-j) or max0 == -1:
            max0 = abs(i-j)
            ans = i + j
        i += 1
print(ans-2)

Submission status: Accepted

Solution code:
import math
n=int(input())
least=n+1
for i in range(math.floor(math.sqrt(n)),1,-1):
  if n%i==0 and least>n//i+i:
    least=n//i+i

print(least-2)

Submission status: Accepted

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

import math
N = int(readline())

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())

i = 1
while True:
   if (i ** 2) >  N:
      break
   else:
      i += 1
while i > 1:
   if N % i == 0:
      break
   else:
      i -= 1
j = N // i
print(i + j - 2)

Submission status: Accepted

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

Submission status: Accepted

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

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

solve()

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools
import queue
from collections import deque
from fractions import Fraction




def main():
    num = int(input())
    ans = num - 1

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

    print(ans)




if __name__ == '__main__':
    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:
N=int(input())
ans=1000000000000
import math

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

print(ans)