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:
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 - 1
    for d in div:
        tmp = (d - 1) + (n // d - 1)
        if tmp < ans:
            ans = tmp

    print(ans)

Submission status: Accepted

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

    divisors.sort()
    return divisors

def main():
    N=int(input())
    di=make_divisors(N)
    if di==[]:
        print(N-1)
    else:
        M=N//di[-1]
        print(di[-1]-1+M-1)

if __name__=="__main__":
    main()

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,n//i))
            if i != n // i:
                divisors.append((n//i,i))

    # divisors.sort()
    return divisors
N=int(input())
div=make_divisors(N)
ans=float("inf")
for x, y in div:
  ans=min(ans,(x-1)+(y-1))
print(ans)

Submission status: Accepted

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

    # divisors.sort()
    return divisors
a=make_divisors(n)
#print(a)
if a[-1]**2==n:
    print(a[-1]*2-2)
else:
    print(a[-1]+a[-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(reverse=True)
    return divisors

l = make_divisors(n)
min_ = float('inf')
for i in range(len(l)):
    min_ = min(min_, l[i]-1+n//l[i]-1)
print(min_)

Submission status: Accepted

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

Submission status: Accepted

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

    def trial_division(n):
        """
        nの約数のリストをO(sqrt(n))で求める
        """
        divs = []
        for i in range(1, int(n**0.5)+1):
            if n % i == 0:
                divs.append(i)
                if i != n//i:
                    divs.append(n//i)
        return divs
    divs = trial_division(N)
    ans = 10**14
    for d in divs:
        i = N//d
        ans = min(ans, d - 1 + i - 1)
    print(ans)


if __name__ == '__main__':
    main()

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:
        print(n//i + i - 2)
        break

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

print(ans)

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 and ans>i+N//i-2:
        ans=i+N//i-2
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import sqrt
from sys import exit

N = int(input())

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

Submission status: Accepted

Solution code:
n = int(input())

p = int(n ** (1/2))

while n % p != 0:
    p -= 1

print(int(p + n/p - 2))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
#sys.setrecursionlimit(150000)


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


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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import sqrt
N = int(input())
ans = 10**12+1
for x in range(1, int(sqrt(N))+1):
  if N % x == 0:
    y = N // x
    l = (x - 1) + (y - 1)
    ans = min(ans, l)
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())
a = n-1
for i in range(1,int(n**0.5)+1):
  if n%i==0:
    a = i+n//i-2
print(a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
input = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines  

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

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

ans = 10**15
for a, b in div:
  ans = min(ans, a+b-2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

ans=1000000000000000
n=int(input())
for i in range(1,int(math.sqrt(n))+2):
    if n%i==0:
        tmp = (i-1)+(int(n//i)-1)
        ans = min(tmp,ans)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

n = int(input())

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())


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

    # divisors.sort()
    return divisors

divisors = make_divisors(N)

if len(divisors) % 2 == 1:
    print(divisors[-1]*2-2)
else:
    print(divisors[-1]+divisors[-2]-2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import math
def main():
    N = int(input())
    ans = []
    for i in range(1,int(math.sqrt(N)+1)):
        if (N % i)==0:
            ans.append((N//i)+i-2)
    print(min(ans))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())

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

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(i+n//i-2)
        break

Submission status: Accepted

Solution code:
import numpy as np
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:
    print(l[int(len(l)/2)]+l[int(len(l)/2)-1]-2)
else:
    print(l[int(len(l)/2)]*2-2)

Submission status: Accepted

Solution code:
from math import sqrt, ceil

N = int(input())
a = 0

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

ans = (a - 1) + (N // a - 1)

print(ans)

Submission status: Accepted

Solution code:
#python3
from math import floor
from math import sqrt
INF = int(1e13)
def main():
    n = int(input())
    ans = INF
    for i in range(1, floor(sqrt(n))+1):
        if n%i == 0:
            ans = min(ans, i+n//i-2)
    print(ans)
main()

Submission status: Accepted

Solution code:
n = int(input())

for k in range(1, int(n ** 0.5) + 1):
    while n % k == 0:
        i = k
        j = n // k
        break
if j < 1:
    i = 1
    j = n

ans = max((i - 1), 0) + max((j - 1), 0)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

n = int(input())

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

Submission status: Accepted

Solution code:
N = int(input())

mul = []
for i in range (1,round(N**(1/2) + 1)):
    if N%i == 0:
        mul.append(i)
        mul.append(N//i)
#print(mul)

min = mul[0] + mul[1]
for i in range (0,len(mul),2):
    if mul[i] + mul[i+1] <= min:
        min = mul[i] + mul[i+1]

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


divisors = make_divisors(N)
ans = 10**12
for i in divisors:
  ans = min(ans, i+N//i)
  
print(ans-2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
n=int(input())
_n=int(math.sqrt(n))
m=10**18
for i in range(1,_n+1):
    if n % i ==0:
        m=min(i+n//i,m)
print(m-2)

Submission status: Accepted

Solution code:
N=int(input())
s=[]
t=[]
for i in range(1,int(N**0.5)+1):
  if N%i==0:
    s.append(i)
    t.append(N//i)
minimum=N-1
for j in range(len(s)):
  minimum=min(minimum,s[j]+t[j]-2)
print(minimum)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

from math import sqrt

def main():
  n = int(input())
  
  min = 10**12
  for i in range(1, int(sqrt(n))+1):
    if n % i != 0:
      continue
    x = n//i + i
    if min > x:
      min = x
  print(min - 2)
      

if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
N = int(input())
ans = 10**15

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

need_lst = []
for i in range(1,int(n**(1/2)+1)):
    if n%i==0:
        need = i+n//i
        need_lst.append(need-2)

print(min(need_lst))

Submission status: Accepted

Solution code:
N=int(input())
ans=10**13
from math import sqrt
for i in range(1, int(sqrt(N))+1):
    if N%i==0:
        temp=(i-1)+(N//i-1)
        if temp<ans:
            ans=temp
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

num_list = []

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

print(sum(num_list[-1])-2)

Submission status: Accepted

Solution code:
N = int(input())
import math
rn = math.sqrt(N)
n = math.floor(rn)
for i in range(n):
    if N % (n-i) == 0:
        x = n-i
        y = N // (n-i)
        break

print(x-1 + y-1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import itertools
import math
import sys

n = int(input())
ans = 10 ** 144
a = 1
max_a = math.sqrt(n)
while a <= max_a:
    if n % a == 0:
        ans = min(ans, a + n / a - 2)
    a += 1
print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

i = 1
result = 10 ** 18
while i*i <= n:
  if n % i == 0:
    result = min(result, (i - 1) + (n//i - 1))
  i += 1

print(int(result))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

N=int(input())
P=math.sqrt(N)
C=float("inf")
for i in range (1,int(P+1)):
	if N%i==0:
		C=min(C,(i+N//i))
print(C-2)

Submission status: Accepted

Solution code:
n=int(input())
num=int(n**0.5)+1
mini=float('inf')
for k in range(1,num+1):
    i=k
    j=0
    if n%i==0:
        j=n//i

    if j!=0 and mini>=i+j-2:
        mini=i+j-2

print(mini)

Submission status: Accepted

Solution code:
N = int(input())

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

print(tmp - 2)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

    def trial_division(n):
        divs = []
        for i in range(1, int(n**0.5)+1):
            if n % i == 0 and i != n//i:
                divs.append((i, n//i))
            elif n % i == 0 and i == n//i:
                divs.append((i, i))
        return divs
    divs = trial_division(N)
    ans = 10**13
    for d1, d2 in divs:
        ans = min(ans, d1 + d2 - 2)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())

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

print(ans)

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:
        j = N // i
        ans = min(ans, i + j - 2)

print(ans)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    ans = inf
    for i in range(1,int(sqrt(N)+1)):
        if N%i==0:
            ans = min(ans, i+N//i)
    print(ans-2)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    for n in range(int(N ** (1 / 2)) + 1)[::-1]:
        if N % n == 0:
            print(n - 1 + N // n - 1)
            return
main()

Submission status: Accepted

Solution code:
import math
N=int(input())
ans=10**13

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

print(int(ans-2))

Submission status: Accepted

Solution code:
def partions(M):#Mの約数列 O(n^(0.5+e))
    import math
    d=[]
    i=1
    while math.sqrt(M)>=i:
        if M%i==0:
            d.append(i)
            if i**2!=M:
                d.append(M//i)
        i=i+1
    d.sort()
    return d

def main():
    N=int(input())
    A=partions(N)
    if len(A)%2==1:
        print(2*A[(len(A)-1)//2]-2)
    else:
        print(A[len(A)//2-1]+A[len(A)//2]-2)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import sqrt
n = int(input())
stop = int(sqrt(n)) + 1
min_count = 10 ** 100
for a in range(1, stop):
    if n % a != 0:
        continue
    else:
        b = n // a
    count = a + b - 2
    if count < min_count:
        min_count = count
print(min_count)

Submission status: Accepted

Solution code:
N = int(input())
a,b = 0,0
t = 10**13
for i in range(1,int(N**0.5)+1):
	if N%i == 0:
		if t >= N//i + i:
			t = N//i + i
			


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

    # divisors.sort()
    return divisors

n_list = make_divisors(n)
n_ans = []

for i in range(0, len(n_list)-1, 2):
    n_ans.append(n_list[i]+n_list[i+1])

n_ans.sort()

print(n_ans[0]-2)

Submission status: Accepted

Solution code:
import math

n = int(input())
n2 = math.ceil(n**0.5)
x = 0

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

Submission status: Accepted

Solution code:
"""
author : halo2halo
date : 4, Feb, 2020
"""

import sys
import itertools

# import math

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
INF = 10 ** 13

N = int(readline())
temp = INF
a = 0
b = 0
for i in range(1, int(N**.5)+1):
    if N % i == 0:
        if temp >= abs(i - N // i):
            temp = abs(i - N // i)
            a = i
            b = N // i
print(a+b-2)

Submission status: Accepted

Solution code:
"""
author : halo2halo
date : 4, Feb, 2020
"""

import sys
import itertools

# import math

import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
INF = 10 ** 13

N = int(readline())
temp = INF
a = 0
b = 0
for i in range(1, int(N**.5)+1):
    if N % i == 0:
        if temp >= abs(i - N // i):
            temp = abs(i - N // i)
            a = i
            b = N // i
print(a+b-2)

Submission status: Accepted

Solution code:
n=int(input())
lim=int(n**.5)
step=float("inf")
for i in range(1,lim+1):
  a=i
  if n%a==0:
    b=n//a
    step=min(step,a+b-2)
print(step)

Submission status: Accepted

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

sq = int(sqrt(n)) + 1

x = True

while(x):
    if n%sq == 0:
        print(int(sq + n/sq - 2))
        x = False
    else:
        sq -= 1

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

n = int(input())
flag = 1000000000000

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

    if y == 0:
        z = x+i
        if z < flag:
            flag = z

print(int(flag-2))

Submission status: Accepted

Solution code:

def main():
    n = int(input())
    i = 1
    ans = 10**18
    while i*i <= n:
        j = n//i
        if i*j == n:
            ans = min(ans, i-1 + j-1)
        i+=1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

m = math.floor(math.sqrt(n))
ans = n-1
for i in range(1,m+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())

a = pow(10,12)

is_not_prime = False
#for k in range(2, int(math.sqrt(N)) + 1):
#    if N % k == 0:
#        is_not_prime = True
#        break
#if is_not_prime:
for i in range(1, int(math.sqrt(N))+1):
    #print(i)
    j = N/i
    if j.is_integer():

        if (i+int(j)) < a:
            a = i+int(j)
    #if j < 2:
    #    break

print(a - 2)
#else:
#    print(N - 1)

Submission status: Accepted

Solution code:
n = int(input())
i = 1

while True:
    if n % i == 0:
        ans = i + n // i - 2
    if i ** 2 > n:
        break
    i += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import sqrt, floor

def solve(n):
    m = floor(sqrt(n)) + 1
    for i in range(m + 1, 0, -1):
        if n % i == 0:
            return i + n // i - 2

_n = int(input())
print(solve(_n))

Submission status: Accepted

Solution code:
N = int(input())
N_sq = int(N**(1/2))+1

ret = 10**12+1
for k in range(1, N_sq):
    if N%k == 0:
        q = N//k
        ret = min(ret, q+k-2)
        
print(ret)

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:
        x,y = i, N//i
        break
print(x+y-2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

print(N//divs[-1]+divs[-1]-2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
ans = 10**12

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

print(ans)

Submission status: Accepted

Solution code:
import sys,math

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	N = II()
	ans = float('inf')
	for i in range(1,int(math.sqrt(N))+1):
		if(N%i==0):
			j = N//i
			ans  = min(i+j, ans)
	print(ans-2)
main()

Submission status: Accepted

Solution code:
import math

N=int(input())

ans=999999999999
for i in range(1,int(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())
num=[]
minv = []

for i in range(1,int(math.sqrt(n))+1):
    if n%i==0:
        num.append([i,n//i])
maxv = 10**13

for i in num:
    if maxv > (i[0]+i[1]):
        maxv = i[0]+i[1]
print(maxv-2)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()

def is_integer_num(n):
    if isinstance(n, int):
        return True
    if isinstance(n, float):
        return n.is_integer()
    return False

mi = INF
for i in range(1, 10**6 + 1):
 	if is_integer_num(N/i):
 		if mi > i + N//i:
	 		mi = i + N//i

print(mi - 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
a, b = 0, 0
for i in np.arange(np.floor(np.sqrt(N)), 0, -1):
    c = N/i
    if c.is_integer():
        a = int(i)
        break
b = int(N/a)
print(a+b-2)

Submission status: Accepted

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

    # divisors.sort()
    return divisors

n = int(input())
divisors = make_divisors(n)
ans = n + 1

for di in divisors:
    ans = min(ans, di + n // di - 2)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
from math import sqrt
def get_divs(n):
  divs = []
  for i in range(1, int(sqrt(n)) + 1):
    if n % i == 0:
      divs.append((i, n // i))
  return divs
d = get_divs(N)
ans = float('inf')
for x, y in d:
  ans = min(x + y - 2, ans)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
pair = [0,10**12]
for i in range(1,10**6+1):
    if N%i == 0:
        if abs(N//i-i) <= abs(pair[0]-pair[1]):
            pair = [i,N//i]
print(sum(pair)-2)

Submission status: Accepted

Solution code:
n = int(input())
ans = 0
t = int(n**(1/2)+1)


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

print(ans-2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

rootn = n ** 0.5

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

Submission status: Accepted

Solution code:
import math

n = int(input())

a = []
b = []

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

arr = []
for i in range(len(a)):
    arr.append(a[i] + b[i])

print(min(arr)-2)

Submission status: Accepted

Solution code:
N = int(input())
ab = 10**13
for i in range(1,10**6+1):
	if N % i == 0:
		ab = min(ab,(i-1)+(N//i-1))
	if i > N:
		break
print(ab)

Submission status: Accepted

Solution code:
N = int(input())
a = []
b = []
for i in range(1,10**6+1):
	if N % i == 0:
		b.append(N // i)
		a.append(i)
	if i > N:
		break
ab = 10**13
for i in range(len(a)):
	ab = min(ab,(a[i]-1)+(b[i]-1))
print(ab)

Submission status: Accepted

Solution code:
n = int(input())
import math 
result = []
for a in range(1, int(math.sqrt(n) // 1) + 1):
    if n % a == 0:
        b = n // a
        result.append([a, b])
    
best_count = float("inf")
for a_b in result:
    score = a_b[0] + a_b[1] - 2
    if best_count > score:
        best_count = score

print(best_count)