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:
import math


def solve():
    n = read()
    result = think(n)
    write(result)


def read():
    return read_int(1)[0]


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_float(n):
    return list(map(lambda x: float(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(n):
    """
    >>> think(10)
    5
    >>> think(50)
    13
    >>> think(10000000019)
    10000000018
    """

    if is_prime(n):
        return n - 1  # from (1, 1) to (1, n) is the least. so distance is (n - 1)
    ceil = int(math.ceil(math.sqrt(n)))
    for a in range(ceil, -1, -1):
        if n % a == 0:
            b = n // a
            return (a - 1) + (b - 1)  # from (1, 1) to (a, b) is the least. so distance is (a - 1) + (b - 1)


def write(result):
    print(result)


def is_prime(n):
    """
    >>> is_prime(0)
    False
    >>> is_prime(1)
    False
    >>> is_prime(2)
    True
    >>> is_prime(3)
    True
    >>> is_prime(4)
    False
    >>> is_prime(5)
    True
    >>> is_prime(6)
    False
    >>> is_prime(7)
    True
    >>> is_prime(8)
    False
    >>> is_prime(9)
    False
    >>> is_prime(10)
    False
    >>> is_prime(11)
    True
    >>> is_prime(12)
    False
    >>> is_prime(13)
    True
    >>> is_prime(14)
    False
    >>> is_prime(15)
    False
    >>> is_prime(16)
    False
    >>> is_prime(17)
    True
    >>> is_prime(18)
    False
    >>> is_prime(19)
    True
    >>> is_prime(20)
    False
    >>> is_prime(23)
    True
    >>> is_prime(25)
    False
    >>> is_prime(29)
    True
    >>> is_prime(30)
    False
    >>> is_prime(31)
    True
    """

    if n <= 1:
        return False
    if n == 2:
        return True
    if n % 2 == 0:
        return False

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


if __name__ == '__main__':
    # import doctest
    # doctest.testmod()
    solve()

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:
from collections import Counter,defaultdict,deque
import sys,bisect,math,itertools,string,queue
from heapq import heappop, heappush
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
res = float('inf')
for i in range(1,int(math.sqrt(n))+1):
    if n%i == 0:
        res = min(res, i + n/i)
print(int(res-2))

Submission status: Accepted

Solution code:
n = int(input())
l = []
for i in range(1,1000001):
    a = n / i
    if a.is_integer():
        l.append(i+n/i)
print(int(min(l)-2))

Submission status: Accepted

Solution code:
import math
 
x = int(input())
 
ans = 100000000000
for i in range(1, int(math.sqrt(x)) + 1): 
    if x % i == 0:
        tmp = int((i - 1) + (x / i - 1))
        if ans > tmp:
            ans = tmp
 
print(tmp)

Submission status: Accepted

Solution code:
import math as m

n = int(input())

sn = int(m.sqrt(n))

while sn > 0:
    if n % sn == 0:
        print(sn+(n//sn)-2)
        break
    sn -= 1

Submission status: Accepted

Solution code:
import math

x = int(input())

tmp = []

for i in range(1, int(math.sqrt(x)) + 1):
    if x % i == 0:
        tmp.append(int((i - 1) + (x / i - 1)))

print(min(tmp))

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
import sys,bisect,math,itertools,string,queue
from heapq import heappop, heappush
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
res = float('inf')
for i in range(1,2*10**6):
    if (n/i).is_integer():
        res = min(res, i + n/i)
print(int(res-2))

Submission status: Accepted

Solution code:
import math
 
N = int(input())
ans = N
n = 1
while n <= math.sqrt(N):
    if N%n == 0:
        ans = min(ans, int(N/n + n -2))
    n += 1  

print(ans)

Submission status: Accepted

Solution code:
import math
N = int(input())
n = int(math.sqrt(N))
 
while n > 0:
    if N%n == 0:
        print(int(N/n + n - 2))
        break
    n -= 1

Submission status: Accepted

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

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
 
n = sorted(make_divisors(N))

sum_yakusuu = N
for _n in n:
    if _n == 1:
        continue
    if _n+N/_n < sum_yakusuu:
        sum_yakusuu = int(_n+N/_n)
        bef_n = _n
    else:
        if len(n) == 2:
            print(int(_n-1))
        else:
            print(int(bef_n+N/bef_n-2))
        break

Submission status: Accepted

Solution code:
import math

N = int(input())
# a, b, x = map(lambda i: int(i), input().split(' '))

def main():
    root_n = math.sqrt(N)
    answer = -1
    for a in range(1, int(root_n) + 1):
        if N % a == 0:
            b = N // a
            v = a + b - 2
            if answer == -1 or answer > v:
                answer = v
    print(v)

main()

Submission status: Accepted

Solution code:
import math
import sys

n = int(input())

ans = sys.maxsize

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np


def solve(N: int):
    result_list = []
    for i in range(1, int(np.sqrt(N)) + 1):
        if N % i == 0:
            result_list.append(i - 1 + N // i - 1)
        else:
            pass
    print(min(result_list))
    return


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

n = int(sqrt(N))
ans = 10**9

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

def sq(n):
    for i in range(math.ceil(math.sqrt(N)), -1, -1):
        if n % i == 0:
            return (i, n//i)

N = int(input())
a, b = sq(N)
print(a + b - 2)

Submission status: Accepted

Solution code:
# coding: utf-8

N = int(input())
num = N
for i in range(1, pow(10,6) + 1):
    if N / i == N // i:
        num = min(num, N // i + i - 2)
print(num)

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

print(min(l))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

print(ans-2)

Submission status: Accepted

Solution code:
import math

N = int(input())

A = int(math.sqrt(N))

for i in reversed(range(1, A + 1)):
    if N % i == 0:
        print(i + (N // i) - 2)
        exit()

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
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)
if len(a)%2==1:
  print(2*int(math.sqrt(n))-2)
if len(a)%2==0:
  b=a[len(a)//2-1]
  c=a[len(a)//2]
  print(b+c-2)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math
N = int(input())
tmp = math.ceil(N**(1/2))
ans = N
for i in range(1,tmp+1):
    if N%i == 0:
        A = max(N//i-1,0)
        B = max(i-1,0)
        ans = min(ans,A+B)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())

patterns = []

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

print(min(patterns))

Submission status: Accepted

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

sosu_dict = {}
a = N
b = N
#print(int(math.sqrt(N+1)))
for i in range(1, int(math.sqrt(N))+1):
    if a % i == 0:
        sosu_dict[i] = int(a/i)
        b = a/i
 

if b > 1:
    sosu_dict[int(b)] = int(a/b)

#print(sosu_dict)

sum = 10 ** 15 +1
for key in sosu_dict:
    if (key + sosu_dict[key]) < sum:
        sum = key + sosu_dict[key]

print(sum-2)

Submission status: Accepted

Solution code:
N = int(input())
m = int((N**0.5 )//1  + 1)
#print(m)

res = 1000000000000
for i in range(1, m):
    if N%i == 0:
        o =  int(N/i)
        if res > o + i:
            res = o + i
print(res-2)

Submission status: Accepted

Solution code:
import math


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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import itertools
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

tmp = make_divisors(n)
tmp.sort()
sq = np.sqrt(n)
tmpnum = n
for i in tmp:
    if i > sq:
        break
    else:
        tmpnum = min(tmpnum-1, n//i + i - 2)
print(tmpnum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

n = int(input())
ans = 0
a = 1
li = []
while a <= math.sqrt(n):
  b = n // a
  if a * b == n:
    li.append(a + b - 2)
  a += 1  
    
ans = min(li)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
ran = int(n**0.5) + 1
ans = 999999999999
for i in range(1, ran):
    j = n // i
    if j != int(n / i):
        continue
    if i * j != n:
        continue
    tmp = i + j - 2
    if tmp < ans:
        ans = tmp
print(ans)

Submission status: Accepted

Solution code:
M = int(input())

for i in reversed(range(2, int(-(-M**0.5//1))+1)): 
    if M % i == 0: # 割り切れるとき
        print(int(i + M / i -2))
        exit()
print(M-1)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())

a = 1
b = n

ans = n - 1

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import math

n = int(input())
ans = 0

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

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

N = int(input())
print(int(solv(N)))

Submission status: Accepted

Solution code:
N = int(input())
l = []
for i in range(1, 1000001):
    a = N / i
    if a.is_integer():
        l.append(i + N / i)
print(int(min(l) - 2))

Submission status: Accepted

Solution code:
import math

n = int(input())

result = n + 1
num = int(math.sqrt(n)) + 1

for i in range(2, num):
  if n % i == 0:
    if i + (n/i) < result:
      result = i + (n/i)

print(int(result-2))

Submission status: Accepted

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

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

    return divisors

div = make_divisors(N)

for i in range(len(div)//2):
  a = div[2*i]
  b = div[2*i+1]
  ans = a + b - 2
  if i==0:
    tmp = ans
  else:  
    tmp = min(ans, tmp)
print(tmp)

Submission status: Accepted

Solution code:
import math
def factorization(n):
    arr = []
    temp = int(math.sqrt(n)+1)
    for i in range(1,temp+1):
      if n % i == 0:
        arr.append(i+n/i-2)

    return arr
  
N = int(input())
A = factorization(N)
print(int(min(A)))

Submission status: Accepted

Solution code:
import math

n = int(input())
b = 0
list = []
sqrt = math.ceil(math.sqrt(n))
for i in range(1,sqrt+1):
	if(n%i==0):
		b = i
print(b + (n//b) -2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math

n = int(input())

cost = sys.maxsize
tmp = 0
k = math.sqrt(n)
for i in range(1,int(k)+1):
    q, mod = divmod(n,i)
    if(mod==0):
        tmp = i + int(q)
        cost = min(tmp,cost)
        tmp = 0

print(cost-2)

Submission status: Accepted

Solution code:
n = int(input())

ans = n - 1
for i in range(2, n):
    if i * i > n:
        break

    if n % i == 0:
        ans = min(ans, i + n // i - 2)

print(ans)

Submission status: Accepted

Solution code:
N = float(input())
l = []
for i in range(1,1000001):
  a = N/i
  if a.is_integer():
    l.append(i + N/i)
print(int(min(l)-2))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000)
INF = 1<<48


def solve(N: int):
    # 約数の全列挙
    def divisors(n):
        divisors = []
        for i in range(1, int(n**0.5)+1):
            if n % i == 0:
                divisors.append(i)
                if i != n // i:
                    divisors.append(n//i)
        # divisors.sort(reverse=True)  # ソート
        return divisors

    l = divisors(N)
    l.append(1)
    l.sort()

    ans = INF

    for i in l:
        M = N // i
        ans = min(ans, M+i-2)

    print(ans)

    return



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:
import sys
import math

n = int(input())

cost = sys.maxsize
tmp = 0
k = math.sqrt(n)
for i in range(1,int(k)+1):
    q, mod = divmod(n,i)
    if(mod==0):
        tmp = i + int(q) - 2
        cost = min(tmp,cost)
        tmp = 0

print(cost)

Submission status: Accepted

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

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

print(N-1)

Submission status: Accepted

Solution code:
import math

N = int(input())
a = N + 1
b = round(math.sqrt(N))

for i in range(b):
  j = i + 1
  if N % j == 0:
    if a > j + N / j:
      a = j + N / j
print(round(a) - 2)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#C
import numpy as np

n = int(input())

now_min = n
now_min_dex = 1
for i in range(1,int(np.ceil(np.sqrt(n)))+1):

    if n % i == 0:
        #print(i,n // i)
        tmp = (i + n // i)

        if now_min > tmp:
            #print('MIN')
            now_min = tmp
            now_min_dex = i

if now_min_dex == 1:
    print(now_min - 1)
else:
    print(now_min - 2)

Submission status: Accepted

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

def eval_ab(a, b):
    if a == 1 or b == 1:
        return a + b - 1
    else:
        return a + b - 2
    
min_e = N - 1
a = int(math.sqrt(N)) + 2
for i in range(1, a):

    if N % i == 0:
        j = N // i
        e = eval_ab(i, j)
        min_e = min(e, min_e)

print(min_e)

Submission status: Accepted

Solution code:
n = int(input())
i = 1
ans = n
 
while i * i <= n:
    j, m = divmod(n, i)
    if m == 0:
        ans = min(ans, i + j - 2)
    i += 1
 
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

print(N-1)

Submission status: Accepted

Solution code:

if __name__ == '__main__':
    N = int(input())
    res = 1
    for i in range(2, int(N**.5)+1):
        if N % i == 0:
            res = i
    print(res + N//res - 2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# Author:   bebebe
# Created:  2019-10-27
#


import math

def divisor(M):
    maxX = int(math.sqrt(M))
    for x in range(2, maxX + 1):
        if M % x == 0:
            return x, M // x
    return 1, M


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

ret_list = []
for a in range(1, N_sqrt + 1):
    if N % a == 0:
        ret_list.append(a + N // a - 2)

print(min(ret_list))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
n = int(N**0.5) + 1
a, b = 0, 0


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

if a == 0:
  a, b = N, 1

print(a + b - 2)

Submission status: Accepted

Solution code:

N = int(input())

factor = []

a = int(N**0.5)+1

l = []
for i in range(a):
    if N%(i+1) == 0:
        l.append(i+1)

b = max(l)

print(N//b + b -2)

Submission status: Accepted

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

numDivList = make_divisors(num)
midNum = len(numDivList) // 2

ans = 0
if len(numDivList) % 2 == 0:
	ans = numDivList[midNum-1] + numDivList[midNum] -2
else:
	ans = numDivList[midNum] * 2 -2

print(ans)

Submission status: Accepted

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

answer = n - 1

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

print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N=int(input())
m=N+1
for j in range(2,int(math.sqrt(N))+1):
    if N%j==0:
        a=j
        b=N//j
        if a>b:
            break
        else:
            m=min(m,(a+b))

print(m-2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(1, int(pow(N, 0.5))+1):
    if N % i == 0:
        j = N // i
        ans.append(i-1+j-1)

print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
#temp = N//2
max_val1 = 0
max_val2 = 0
ans = 0
ans_temp = 1000000000000
for i in range(1, int(np.sqrt(N))+1):
    if N % i == 0:
        max_val1 = i
        max_val2 = N // i
        ans = max_val1 + max_val2 - 2
        if ans < ans_temp:
            ans_temp = ans
        else:
            break
        #print(ans)
        #print(max_val1)
        #print(max_val2)

#print(ans)
print(ans)

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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

Submission status: Accepted

Solution code:
import math
 
A = int(input())
 
start = math.ceil(math.pow(A, 0.5))
while A % start != 0:
    start -= 1
 
print((start + (A // start) - 2))

Submission status: Accepted

Solution code:
import itertools

a = int(input())

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

ab = [[i,a//i] for i in md(a)]
abc = [sum(ab[i]) for i in range(len(ab))]

print(min(abc)-2)

Submission status: Accepted

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

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

    divisors.sort()
    return divisors

a = make_divisors(N)
if len(a)%2 == 1:
  print(2*int(math.sqrt(N))-2)
if len(a)%2 == 0:
  b = a[len(a)//2-1]
  c = a[len(a)//2]
  print(b+c-2)

Submission status: Accepted

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

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

    divisors.sort()
    return divisors

a = make_divisors(N)
if len(a)%2 == 1:
  print(2*int(math.sqrt(N))-2)
if len(a)%2 == 0:
  b = a[int(len(a)/2)-1]
  c = a[int(len(a)/2)]
  print(b+c-2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# C WA
# -*- coding: utf-8 -*-
import numpy as np
N = int(input())

first_num = 0
second_num = 0
min_diff = N
for i in range(1, int(np.sqrt(N) + 1)):
    now_diff = N // i - i
    if N % i == 0 and min_diff > now_diff:
        first_num = i
        second_num = N // i
        min_diff = now_diff
print(max(first_num + second_num - 2, 0))

Submission status: Accepted

Solution code:
import math

a = int(input())
list = []
sqrt = math.ceil(math.sqrt(a))
for i in range(sqrt):
  if (a%(i+1)==0):
    list.append(i+1)
b = max(list)
c = round(a/b)
print(b + c -2)

Submission status: Accepted

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

    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = 0
ans = N-1
s = 0
for x in range(1,1000001):
    s=N//x
    if N%x==0:
        a = (s-1)+(x-1)
        if ans>a:
             ans = a
print(ans)

Submission status: Accepted

Solution code:
import math

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)
n = len(l)
ans = []
for i in range(n):
	ans.append(l[i] + l[n-i-1])

print(min(ans)-2)

Submission status: Accepted

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

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

    # divisors.sort()
    return divisors


def main():
    N = int(input())
    V = make_divisors(N)
    if len(V) %2 == 1:
        # print(V)
        print(V[-1]*2-2)
        exit()
    # print(V,V[-2:])
    print(sum(V[-2:])-2)
        
if __name__ == '__main__':
    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)
    return divisors
N_list = make_divisors(N) # 消す

if N_list[-1]*N_list[-2] == N:
    print(N_list[-1]+N_list[-2]-2)
else:
    print(N_list[-1]+N_list[-1]-2)

Submission status: Accepted

Solution code:
n = int(input())
ans = 10**13    

for i in range(1,10**7):
  if (n % i) == 0: 
    j = n / i
    ans_ = ((i + j) - 2)
    if ans > ans_:
      ans = ans_
print(int(ans))

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
res = float('inf')
for n in range(1, int(np.sqrt(N))+1):
    if N % n == 0:
        t = N/n
        res = int(min(res, n+t))
print(res-2)

Submission status: Accepted

Solution code:
import math
x = int(input())
x_sqrt = int(math.sqrt(x))

min_num = x-1
for i in range(2,x_sqrt+2):
    if x % i == 0:
        num = i+int(x/i)-2
        if num < min_num:
            min_num = num
print(min_num)

Submission status: Accepted

Solution code:
# N=n**2となるnが最も移動が少なくて住む点
import math
N=int(input())
int_root_N=int(math.sqrt(N))
for i in range(int_root_N,0,-1):
    if N%i==0:
        min_n=i
        break

print(min_n-1+N//min_n-1)

Submission status: Accepted

Solution code:
import math
def anss(n):
    div = []
    for i in range(1, int(math.sqrt(n)) + 1):
        if n % i == 0:
            if n == i * i:
                div.append(i)
            else:
                div.append(i)
                div.append(n // i)
    div.sort()
    if len(div) % 2:
        return 2 * (div[len(div)// 2] - 1)
    else:
        return div[len(div) // 2] - 1 + div[(len(div) // 2) - 1] - 1
n = int(input())
print(anss(n))

Submission status: Accepted

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