Problem:


Score : 300 points

Problem Statement

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

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

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

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

Constraints

2 \leq N \leq 10^{12}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

10

Sample Output 1

5

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

Sample Input 2

50

Sample Output 2

13

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

Sample Input 3

10000000019

Sample Output 3

10000000018

Both input and output may be enormous.



Sample input:
10


Sample output:
5

Submission status: Accepted

Solution code:
N = int(input())


def solve(n):
    i = 1
    ans = N - 1
    while i * i <= n:
        if n % i == 0:
            ans_tmp = ((N // i) - 1) + (i - 1)
            if ans > ans_tmp:
                ans = ans_tmp
        i += 1
    return ans


print(solve(N))

Submission status: Accepted

Solution code:
import math

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

if n == 2:
    print(1)
    exit()

for i in range(1, r+1)[::-1]:
    if n % i == 0:
        print(i + int(n/i) - 2)
        exit()

Submission status: Accepted

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

N = int(input())

left_side = 1
right_side = N

# 平方根から下げていく
square = int(math.sqrt(N))
for i in reversed(range(1, square+1)):
    if N % i == 0:
        left_side = i
        right_side = N // i
        break

distance = (left_side - 1) + (right_side - 1)
print(distance)

Submission status: Accepted

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

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

yakusu = make_divisors(N)
l = len(yakusu)
if l % 2 == 0:
    i = yakusu[l//2-1]
    j = yakusu[l//2]
else:
    i = yakusu[l//2]
    j = yakusu[l//2]

ans = i + j - 2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

kanousei = []

yakusuu_list = make_divisors(n)

for i in yakusuu_list[:-(-len(make_divisors(n))//2)]:
    if n%i==0:
        kanousei.append([i, int(n/i)])

saisyou = n + 1
index = 0
        
for i in kanousei:
    if sum(i) < saisyou:
        saisyou = sum(i)

print(saisyou-2)

Submission status: Accepted

Solution code:
import math 
n = int(input())
s = int(math.sqrt(n))
ans = 0
for i in reversed(range(1,s+1)):
  if n % i == 0:
    ans = i + int(n / i) - 2
    break
      
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:
    L.append(i+int(N/i)-2)
  else:
    pass
print(min(L))

Submission status: Accepted

Solution code:
from sys import stdin
import math

n = int(stdin.readline().rstrip())


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

li = make_divisors(n)

total = n+1

for i in range(len(li)):
    a = li[i]
    b = n//a
    total = min(total,a+b)

print(total-2)

Submission status: Accepted

Solution code:
import itertools
import collections

def prime_factorize(n):
	a = []
	while n % 2 == 0:
		a.append(2)
		n //= 2
	f = 3
	while f * f <= n:
		if n % f == 0:
			a.append(f)
			n //= f
		else:
			f += 2
	if n != 1:
		a.append(n)
	return a

def calc_dist(x, y):
	return abs(x - 1) + abs(y - 1)

n = int(input())
ll = prime_factorize(n)
c = collections.Counter(ll)
kk = list(c.keys())
vv = list(c.values())
pp = [list(range(vv[i] + 1)) for i in range(len(kk))]

min_dist = 10 ** 12
# print(c)
# print(kk)
# print(vv)
for bb in itertools.product(*pp):
	l = 1
	r = 1
	for i in range(len(kk)):
		l *= max(kk[i] ** bb[i], 1)
		r *= max(kk[i] ** (vv[i] - bb[i]), 1)
	#print(bb, l, r, calc_dist(l, r))
	min_dist = min(min_dist, calc_dist(l, r))
print(min_dist)

Submission status: Accepted

Solution code:
import sys
import math

N = int(sys.stdin.readline())

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

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

Submission status: Accepted

Solution code:
N = int(input())

divisors = []

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

if y != 1:
	print((y-1)+(x-1))
else:
	print(x-1)

Submission status: Accepted

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

max = math.floor(N**0.5)
A = 1
B = 1
for i in range(max):
    if N % (max - i) == 0:
        A = (max - i)
        B = N // A
        break

print(A + B - 2)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline


def main():
    n = int(input())
    x = int(n**0.5)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
f = 10**12

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

for i in make_divisors(n):
    z = i + n//i - 2
    # print('i:',i,' make_divisors(n):', make_divisors(n),' z:',z,' f:',f)
    if z < f:
        f = z

print(f)

Submission status: Accepted

Solution code:

n = int(input())

def make_divisor(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

vals = make_divisor(n)
result = n - 1
for val in vals:
    if val == 1 or val == n:
        continue
    result = min(result, int(n/val)+val - 2)

print(result)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import math
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

def findRoot(N):
    root = round(math.sqrt(N+1))
    return root

def main():
    N=int(input())  #数値入力 「N」だけの入力のとき
    for i in list(reversed(range(1, findRoot(N)+1))):
        a, b = divmod(N, i)
        if b == 0:
            c = N // a
            print(a+c-2)
            break
            

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
n = int(input())
x = 1
y = 1
N = int(math.sqrt(n))
for i in range(N):
    if i == 0:
        X = n-i
        Y = 1
    if n % (i+1) == 0:
        x = i+1
        y = n // (i+1)
        if (X+Y) > (x+y):
            X = x
            Y = y
print(X + Y - 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.append(n//i)

    return divisors

N = int(input())

combo = make_divisors(N)
sum = [sum(combo[i:i+2]) for i in range(0,len(combo),2)]
minimum = min(sum) - 2

print(minimum)

Submission status: Accepted

Solution code:
import sys,collections,math,random;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append((i,n//i))
    return divisors
    
n = I()
p = make_divisors(n)
ans = n
for i in range(len(p)):
    ans = min(ans, p[i][0] + p[i][1] -2)
print(ans)

Submission status: Accepted

Solution code:
import math

def main():
    Input = [input() for i in range(1)]
    N = int(Input[0])
    x = math.sqrt(N)
    if x.is_integer():
        print(int(x+N/x-2))
    else:
        while x >= 1:
            x = math.ceil(x)-1
            y = N/x
            if y.is_integer():
                print(int(x+y-2))
                break

        
        
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
import math 
arr = []
for i in range(1,n):
  if i > math.ceil(n**0.5):
    break
  if n % i == 0:
    arr.append(i+n/i-2)
print(int(min(arr)))

Submission status: Accepted

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

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+N//i)

print(min(divisors)-2)

Submission status: Accepted

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

import pdb
import sys
import math
F = sys.stdin
N = int(F.readline())
sqN = int(math.sqrt(N))
ans = 0

for i in range(1, sqN+1):
    if N % i == 0:
        A = int(i - 1)
        B = int((N / i) - 1)
        if ans == 0 or A + B < ans:
            ans = A + B

print(ans)

Submission status: Accepted

Solution code:
import sys
import os
import itertools

n = int(input())

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

    return divisors

ans = make_divisors(n)
m = max(ans)
print( (m-1) + ans[m]-1)

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())
p = make_divisors(n)
print(min([a+n//a-2 for a in p]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
A = int(input())
p = int(math.sqrt(A)) + 1
all_lis = []
ans_lis = []

for i in range(1,p,1):
    if A % i == 0:
        kumi = [i,int(A/i)]
        all_lis.append(kumi)

for j in all_lis:
    ans_lis.append(j[0]+j[1])
    
print(int(min(ans_lis)-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((n//i, i))
            if i != n // i:
                divisors.append((n//i, i))
    return set(divisors)


pairs = list(make_divisors(n))
min_pair = n-1

for p in pairs:
    result = p[0] + p[1] -2
    if result < min_pair:
        min_pair = result

print(min_pair)

Submission status: Accepted

Solution code:
import math

N = int(input())

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

Submission status: Accepted

Solution code:
n=int(input())

fac=1
s=int(n**(1/2)+1)
for i in range(s):
    if n%(s-i)==0:
        fac=s-i
        break

ans=fac+(n//fac)-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())
L=make_divisors(N)
#print(L)
if len(L)%2==1:
    print((L[len(L)-1])*2-2)
else:
    print(L[len(L)-1]+L[len(L)-2]-2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
s=input()
b=0
c=0
a=int(s)
d=int(math.sqrt(a))
for i in range(1,d+1,1):
  if a%i==0:
    b=i
    c=a/i
e=int(b-1+c-1)
print(e)

Submission status: Accepted

Solution code:
N =int(input())
ans = 0
L =[]
for i in range(2,1000001):
    if N % i == 0:
        M = N/i
        L.append(M+i)
        ans += 1
if ans == 0:
    print(N-1)
else:
    print(round(min(L)-2))

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy

# 入力 =stdin.readline().rstrip()
# 分割 = stdin.readline().rstrip().split()

'''
条件に合うものを絞り込む
data = [1, 2, 3]
s = [x for x in data if x % 3 == 0]
'''

'''
文字配列を数値に
l_si = ['-10', '0', '100']

l_si_i = [int(s) for s in l_si]
'''

'''
a = [2, 4, 6, 8]
b = [3, 6, 9]

print(list(set(a) | set(b)))  # 和集合
print(list(set(a) & set(b)))  # 積集合
'''

'''
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
count = 0
while len([x for x in A if x % 2 == 0]) == N:
    A = list(map(lambda y: y / 2, A))
    count += 1
print(count)
'''

'''
.append(要素)
.extend(要素)
.insert(場所,要素)
del 配列名[場所]
-1で末尾で末尾から一つ前
'''
'''
間に空白追加
' '.join(lis)
'''
'''
copy.copy(配列名)
'''
'''
sort()50
sorted(reverse=true)
'''
'''
2分探索
bisect.bisect_left(リスト, 要素)
'''


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


num = int(stdin.readline().rstrip())
yaku = make_divisors(num)
yaku.sort()

res = []
if (int(len(yaku)) % 2 == 0):
    res.append(yaku[int(len(yaku) / 2 - 1)])
    res.append(yaku[int((len(yaku)) / 2)])
    print(int(res[0]-1)+int(res[1]-1))
else:
    res.append(yaku[int((len(yaku)) / 2)])
    print(int(res[0]-1)+int(res[0]-1))

Submission status: Accepted

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

main()

Submission status: Accepted

Solution code:
n = int(input())

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

    divisors.sort()
    return divisors

divs = make_divisors(n)
lengths = list()
o = int(len(divs)//2 + len(divs) % 2)
for i in range(o):
    a = divs[i]
    b = divs[::-1][i]

    lengths.append(a-1+b-1)


print(min(lengths))

Submission status: Accepted

Solution code:
from itertools import product


def main():
    nn = n = input()
    if n == 1:
        print(0)
        return
    factors = {}
    p = 2
    while n % p == 0:
        factors[p] = factors.get(p, 0) + 1
        n //= p
    p = 3
    while nn >= p * p:
        while n % p == 0:
            factors[p] = factors.get(p, 0) + 1
            n //= p
        p += 2
    if n > 1:
        factors[n] = factors.get(n, 0) + 1
    
    s = []
    for f in factors:
        n = factors[f]
        s.append([(f ** i, f ** (n - i)) for i in xrange(n + 1)])

    best = float('inf')
    for case in product(*s):
        a = b = 1
        for aa, bb in case:
            a *= aa
            b *= bb
        best = min(best, a + b - 2)
    print(best)


main()

Submission status: Accepted

Solution code:
import numpy
import math

N = int(input())
k = math.floor(numpy.sqrt(N))
for i in range(k):
    if N%(k-i) == 0:
        print(int(k-i+N/(k-i)-2))
        break

Submission status: Accepted

Solution code:
n = int(input())
a = n - 1
p = 1
for c in range(2, int((n ** 0.5) * 2)):
    if p % 2 == 1 and c > (n ** 0.5):
        break
    if n % c == 0:
        p = c
        w = int(n / c)
        if (w + c - 2) < a:
            a = (w + c -2)
        else:
            continue
    else:
        continue
print(a)

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())
n_list = make_divisors(N)
move_list = list()
for i in n_list:
    a = N / i
    move_list.append(a+i-2)
move_list.sort()
print(int(move_list[0]))

Submission status: Accepted

Solution code:
N = int(input())

def make_yakusu(x):
  all_yakusu=[]
  for i in range(1,int(x**0.5)+1):
    if x % i == 0:
      kumi_yakusu=[int(i), int(x / i)]
      all_yakusu.append(kumi_yakusu)
  return(all_yakusu)

tp = make_yakusu(N)
idouryou_list = []
for i in range(len(tp)):
  xn = tp[i][0]
  yn = tp[i][1]
  idouryou_list.append(int(xn) + int(yn) -2)
print(min(idouryou_list))

Submission status: Accepted

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

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(set(table))
    return table

l = sorted(divisor(N))
kazu = math.ceil(len(l)/2)

now=0
for i in l[:kazu]:
    tmp = N/i
    tmp2 = i + tmp
    if now==0:
        now=tmp2
    if tmp2 < now:
        now=tmp2

print(int(now-2))

Submission status: Accepted

Solution code:
n=int(input())
s=n**0.5
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()
import bisect as bi
b=bi.bisect_left(d,s)
print(min(n//d[b-1]+d[b-1]-2,n//d[b]+d[b]-2))

Submission status: Accepted

Solution code:
import math

N=int(input())
x=math.sqrt(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
  
if x.is_integer():
  a=len(make_divisors(N))
  b=sorted((make_divisors(N)))[a//2]
  print(2*b-2)
  
else:  
  c=len(make_divisors(N))
  d=sorted((make_divisors(N)))[c//2]
  e=sorted((make_divisors(N)))[(c//2)-1]
  print(d+e-2)

Submission status: Accepted

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

a = int(input())

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

print(min([x for x in make_divisors(a)]))

Submission status: Accepted

Solution code:
import math
n=int(input())
sn=int(math.sqrt(n))
tl=list(range(1,sn+1))
ans=0
for i in range(1,sn+1):
    a=n/tl[-i]
    if a.is_integer():
        ans=int(a)
        break
b=int(n/ans)
print(ans-1+b-1)

Submission status: Accepted

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

ans_i=0
ans_j=0
if len(a)==0:
    print(n-1)

else:
    min=abs(a[0]-b[0])
    for i in range(len(a)):
        if min >= abs(a[i]-b[i]):
            min=abs(a[i]-b[i])
            ans_i=a[i]
            ans_j=b[i] 
    print((ans_j-1)+(ans_i-1))

Submission status: Accepted

Solution code:
import numpy as np

A = int(input())

tmp = int(np.floor(np.sqrt(A)))
for i in range(tmp):
    if A % (tmp - i) == 0:
        base1 = tmp - i
        break
base2 = A // base1

print(base1 + base2 - 2)

Submission status: Accepted

Solution code:
N = int(input())
list = []

i = 1
while i <= 10**6:
  if N % i == 0:
    list.append(int(i-1+N/i-1))
    i+=1
  else:
    i+=1
    
print(min(list))

Submission status: Accepted

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

first = int(np.sqrt(N))

for i in range(first, 0, -1):
    q, mod = divmod(N, i)
    if mod == 0:
        result = (i - 1) + (q - 1)
        break

print(result)

Submission status: Accepted

Solution code:
N = int(input())
min = N
max_i = 0
max_j = 0
for i in range(1,1000000):
    if N % i == 0:
        j = N // i
        if abs(i - j) < min:
            min = abs(i - j)
            max_i = i
            max_j = j
for i in range(1000000,N // 1000000 + 1):
    if N % i == 0:
        j = N // i
        if abs(i - j) < min:
            min = abs(i - j)
            max_i = i
            max_j = j

print(max_i + max_j - 2)

Submission status: Accepted

Solution code:
N = int(input())

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

    # divisors.sort()
    return divisors

min_cnt = 10**13
divisor = make_divisors(N)

for i in range(len(divisor)):
    b = N // divisor[i]
    min_cnt = min(min_cnt, (divisor[i]-1 + b-1))
print(min_cnt)

Submission status: Accepted

Solution code:
i = 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

l = []
for x, y in zip(sorted(make_divisors(i)), sorted(make_divisors(i),reverse=True)):
	l.append(x + y - 2)
print(min(l))

Submission status: Accepted

Solution code:
import math

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

if N == 2 or N == 3:
    print(N-1)
elif sq*sq == N:
    print(2*sq-2)
else:
    for i in reversed(range(1,sq+1)):
        if N % i == 0:
            print(i+(N//i)-2)
            break

Submission status: Accepted

Solution code:
import math
N =int(input())
sq=int(math.sqrt(N))
for i in range(sq,0,-1):
    if N%i==0:
        print(i+N//i-2)
        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)
    return divisors

import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
N = I()

ans = int(0)
cou = []

cou = make_divisors(N)
if cou[-1]!=int(N**(0.5)):
    ans = cou[-1]+cou[-2]-2
else:
    ans = 2*cou[-1]-2

#print(cou)
print(ans)

Submission status: Accepted

Solution code:
# alone
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)

result = sorted(divisors)
#print(result)

if len(result) == 2:
    final_i = result[0]
    final_j = result[1]
else:
    #print(len(result))
    if len(result)%2 == 0:
        final_i = result[len(result)//2 - 1]
        final_j = result[len(result)//2 ]
            
    else:
        final_i = result[(len(result))//2]
        final_j = result[(len(result))//2]
            
#print(final_i,final_j)        
print(final_i+final_j - 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.append(n//i)

    divisors.sort()
    return divisors

n = int(input())

arr = make_divisors(n)
#print(arr)
useIndex = int(len(arr)/2)
#print(useIndex)

x = arr[useIndex - 1] - 1
y = arr[useIndex] - 1
#print(x,y)
print(x + y)

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())
ret = make_divisors(N)
if ret[-1]**2 == N:
    ret.append(ret[-1])

min = 9999999999999
for i in range(int(len(ret)/2)):
    now = (ret[i*2] -1) + (ret[i*2+1] -1)
    min = now if now < min else min
    
print(min)

Submission status: Accepted

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

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

l = len(divisors)
if l % 2 == 0:
    a = divisors[:int(l/2)]
    b = divisors[int(l/2):]
    b.sort(reverse=True)
    c = []
    for i in range(int(l/2)):
        c.append(a[i]+b[i])
    print(min(c)-2)
        
else:
    a = divisors[:int(l/2)]
    b = divisors[int((l+1)/2):]
    b.sort(reverse=True)
    c = divisors[int((l-1)/2)]
    d = []
    for i in range(int((l-1)/2)):
        d.append(a[i]+b[i])
    print(min(min(d), c*2) - 2)

Submission status: Accepted

Solution code:
n = int(input())
ans = float('inf')
for i in range(1, int(-(-n**0.5//1))+1):
  if n % i == 0:
    ans = min(ans, (i-1)+n//i-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

tmp = make_divisors(N)
if len(tmp) % 2 == 0:
    print(tmp[-1] + tmp[-2] - 2)
else:
    print(tmp[-1]*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
l = make_divisors(n)
if l[len(l)//2]**2 == n:
    print(l[len(l)//2]*2-2)
else:
    print(l[len(l)//2-1]+l[len(l)//2]-2)

Submission status: Accepted

Solution code:
N=int(input())
import math
SQ=int(math.sqrt(N))
minval=10**12
a=1
b=1
for i in range(1,SQ+1):
  if N%i==0:
    newa=i
    newb=N//i
    if abs(newa-newb) < minval:
      minval=abs(newa-newb)
      a=newa
      b=newb

print(a+b-2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
ans = 1000000000001

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

Submission status: Accepted

Solution code:
N = int(input())
import math
def prime(n):
  sq = math.ceil(n**(1/2))
  for i in range(sq,1,-1):
    if n%i==0:
      return i,False
    
  return 1,True

A,B = prime(N)
if B:
  print(N-1)
  
else:
  print((N//A+A-2))

Submission status: Accepted

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

    # divisors.sort()
    return divisors

N = int(input())

A = make_divisors(N)
result = pow(10,12)
#print(A)
for i in A:
    #print(i)
    B = (N/i-1)+(i-1)
    #print(B)
    result = min(result,B)
    
print(int(result))

Submission status: Accepted

Solution code:
n = int(input())

cd = []
for i in range(1, int(n**0.5)+1):
    if n % i == 0:
        cd.append(i)
        if i != n // i:
            cd.append(n//i)
min = float('inf')
if len(cd) % 2 == 0:
    for i in range(0, len(cd) - 1,  2):
        if cd[i] + cd[i+1] < min:
            min = cd[i] + cd[i+1]
    print(min - 2)
else:
    print(cd[len(cd) - 1] * 2 - 2)

Submission status: Accepted

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

print(m)

Submission status: Accepted

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

def balance_fact(n):
  rn = int(math.sqrt(n))
  m = 1
  k = 0
  for i in range(rn):
    j = rn-i
    if n%j == 0:
      m = n//j
      k = j
      break
  return (m, k)  
    
m, k = balance_fact(n)
print(m+k-2)

Submission status: Accepted

Solution code:
import math
n=int(input())
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
if is_prime(n)==True:
    print(int(n-1))
else:
    N=int(math.sqrt(n))
    for i in reversed(range(N)):
        if n%(i+1)==0:
            print(int((n/(i+1))+i-1))
            break

Submission status: Accepted

Solution code:
n=int(input())

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

print(i+j-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

a = int(input())
b = make_divisors(a)
idx = len(b) // 2
if len(b) % 2 == 0:
  target = (b[idx],b[idx-1])
else:
  target = (b[idx],b[idx])
print(target[0]-1 + target[1]-1)

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())
list =make_divisors(N)
list_rev=sorted(list, reverse=True)

ans = 10**15
for i in range(len(list)//2 + 1):
  tmp = (list[i]-1)+(list_rev[i]-1)
  if tmp < ans:
    ans=tmp

print(ans)

Submission status: Accepted

Solution code:
from math import sqrt
from math import ceil

min_move = 10**12

n = int(input())

for i in range(1,ceil(sqrt(n)+0.1)):
  if n%i == 0:
    #print(n,i)
    b = n//i
    if min_move >  i+b-2:
      min_move = i+b-2

print(min_move)

Submission status: Accepted

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

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()
ans = []
j_list = []

for j in divisors:
  if int(N/j) in j_list:
    break
  ans.append(j+int(N/j)-2)
  j_list.append(j)

print(min(ans))

Submission status: Accepted

Solution code:
abc = input()
abc_int = int(abc)
prolist = []
chk = int(abc_int**0.5)+2
for c1 in range(chk):
	if abc_int % (c1+1) == 0:
		prolist.append(c1+1+abc_int//(c1+1)-2)
print(min(prolist))

Submission status: Accepted

Solution code:
n = int(input())

ans = 10**10000
for i in range(1,10**6+1):
  if n%i == 0:
    a = i
    b = n//i
    ans = min(ans,a-1 + b-1)

print(ans)