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 itertools
n = int(input())

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

div_list = make_divisors(n)
#print(div_list)

mn =2**50
for i in div_list:
    mn = min(mn,i+n//i)

print(mn-2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

waru = int(n**0.5)
while True:
    if n%waru == 0:
        a = waru
        b = n//waru
        break
    else:
        waru -= 1
print(a+b-2)

Submission status: Accepted

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

rn = int(math.sqrt(n))

ans_l = []
for i in range(1, rn+1):
  if n%i == 0:
    ans_l.append(i+n//i-2)

print(min(ans_l))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import math
import itertools
input_n = int(input())

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

if (len(yakusu_list) % 2) == 1:
  tmp = yakusu_list[len(yakusu_list)//2 + 1 -1] + yakusu_list[-(len(yakusu_list)//2 + 1)]
  print(tmp -2)
else:
  tmp = yakusu_list[len(yakusu_list)//2 - 1] + yakusu_list[-(len(yakusu_list)//2)]
  print(tmp -2)

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
import numpy as np
import sys
sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

# 最短距離
# 素因数分解
N = I()

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

# c = sympy.divisors(N)
c = make_divisors(N)
lenc = len(c)
if (lenc % 2)==0:
    a = c[lenc // 2 - 1]
    b = c[lenc // 2]
else:
    a = c[lenc // 2]
    b = a

print(a + b -2)

#H,N = LI()
#AB = [LI() for _ in range(N)]
#A,B = zip(*AB)
#Ap = np.array(A)
#C = np.zeros(N + 1)

# if ans:
#     print('Yes')
# else:
#     print('No')

Submission status: Accepted

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


n = int(input())
low, up = make_divisors(n)
if len(low) != 1:
    # ans = low[0] + up[0]
    # for i in range(1, len(low)):
    #     tmp = low[i] + up[i]
    #     if tmp < ans:
    #         ans = tmp
    print(low[-1] + up[-1] - 2)
else:
    print(n - 1)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

max_div_num = 1

for i in range(2, int(n**(1/2) + 1)):
    if n % i == 0:
        max_div_num = i

x = max_div_num
y = n // max_div_num

print(x + y - 2)

Submission status: Accepted

Solution code:
n = int(input())

max_div_num = 1

for i in range(2, int(n**(1/2) + 1)):
    if n % i == 0:
        max_div_num = max(i, max_div_num)

x = max_div_num
y = n // max_div_num

print(x + y - 2)

Submission status: Accepted

Solution code:
N=int(input())
move=int(N**.5)
while N%move!=0:
    move-=1
print(N//move+move-2)

Submission status: Accepted

Solution code:
from sys import exit
import math

def main():
  N = int(input())
  ran = int(math.sqrt(N))
  ans = N
  
  for i in range(1,ran+1):
    if N % i == 0:
      #print('L')
      if N / i + i <= ans + 1:
        ans = ((N / i) + i) -2
        #print(i)
  
  print(int(ans))
  
  
  
main()

Submission status: Accepted

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

Submission status: Accepted

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

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

for i in range(start_digit, 0, -1):
    q, r = divmod(N, i)
    if r == 0:
        output = i+q-2
        break
        
print(output)

Submission status: Accepted

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

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, i+N//i-2])
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
ans = N-1

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

Submission status: Accepted

Solution code:
n=int(input())
result=float('inf')
for i in range(1,int(n**.5)+1):
  if i*(n//i)==n:
    result=min(result,abs(i+n//i)-2)
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import time
import math
import itertools as it
def inpl():
    return list(map(int, input().split()))
st = time.perf_counter()
# ------------------------------

N = int(input())

mn = 1001001001001
a = 1
while a*a <= N:
    if N % a == 0:
        mn = min(mn, a + N//a - 2)
    a += 1
print(mn)
    

# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
ans = float("inf")
for i in range(1, n+1):
    if n%i==0:
        ans = min(ans, i+n//i)
    if i*i>n:
        break
print(ans-2)

Submission status: Accepted

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

Submission status: Accepted

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

def mass_search(x):
  divisor = 0
  limit = math.floor(math.sqrt(x))
  for i in range(1,limit+1):
    if N % i == 0:
      divisor = max(divisor, i)
  x = divisor
  y = N//divisor
  return x+y-2

print(mass_search(N))

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = int(N**(1/2))
for i in range(S,0,-1):
    if N%i == 0:
        A = N//i
        B = i
        break

print(A+B-2)

Submission status: Accepted

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

Submission status: Accepted

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

def resolve():
    N = int(input())
    factors = divisors(N)
    ans = float("inf")
    for x, y in factors:
        ans = min(ans, (x-1)+(y-1))
    print(ans)
    

    
if '__main__' == __name__:
    resolve()

Submission status: Accepted

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

n = int(input())
ans = n
divisor_num = divisor(n)

for i in divisor_num:
    j = n // i
    tmp = i + j - 2
    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

n = int(input())
ans = n-1
x = int(math.ceil(math.sqrt(n)))

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

ans = []
for i in range(1,10**6+1):
    if n % i == 0:
        ans.append(i+(n//i)-2)

print(min(ans))

Submission status: Accepted

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

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

Submission status: Accepted

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

N = int(input())
div = make_divisors(N)
ans = 10 ** 13
for d in div:
    ans = min(ans, d + N // d - 2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

        i += 1

    return lower_divisors + upper_divisors[::-1]

n = int(input())
d = divisor(n)
if len(d) == 2:
    print(d[1] - 1)
elif len(d) % 2 == 0:
    k = len(d)//2
    u = d[k-1]
    v = d[k]
    print((u-1) + (v-1))
else:
    k = len(d)//2
    v = d[k]
    print((v-1)*2)

Submission status: Accepted

Solution code:
#144_C
import math

n = int(input())
m = math.floor(math.sqrt(n))

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

Submission status: Accepted

Solution code:
import numpy
n = int(input())
l = []
ans = n

def is_prime(q):
    q = abs(q)
    if q == 2: return True
    if q < 2 or q&1 == 0: return False
    return pow(2, q-1, q) == 1

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

Submission status: Accepted

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

  
n = int(input())
d = make_divisors(n)
ans = 10**13
for i in d:
    j = n // i
    ans = min(ans, i+j-2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


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

  div = 0
  for i in range(int(math.sqrt(N)), 0, -1):
    if N % i == 0:
      div = i
      break

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


if(__name__ == '__main__'):
  main()

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

print(ans)

Submission status: Accepted

Solution code:
import math

def is_prime(n):
    if n == 1: return False

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

    return True

n = int(input())

if is_prime(n):
    print(n -1)
    exit()

ans = n

for k in range(2, int(math.sqrt(n)) + 1):
    if n % k == 0:
        temp_1 = n/k
        temp_2 = k
        ans = min(ans,temp_1 + temp_2)

print(int(ans-2))

Submission status: Accepted

Solution code:
import math
n = int(input())
p = 10 ** 12
m = math.floor(math.sqrt(n))

for i in range(1,m + 1):
    if n % i == 0:
        j = n // i
        p = min(p,i-1+j-1)

print(p)

Submission status: Accepted

Solution code:
import math

n = int(input())

def solve():
  for k in range(2, int(math.sqrt(n)) + 1):
    if n % k == 0:
      return False
  
s = solve()
if s != False:
  print(n-1)
  exit()
  
ans = 10**12
for k in range(2, int(math.sqrt(n)) + 1):
  if n % k == 0:
    ans = min(ans,k+n//k)
  
print(ans-2)

Submission status: Accepted

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

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

Submission status: Accepted

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

l = divisor(n)
c = len(l)

ans = 100100100100100
for i in range(c):
    x = l[i] - 1
    y = l[c-i-1] - 1
    tot = x + y
    ans = min(ans,tot)
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

han=999999999999999999999999999999999999999999
b=0



for a in range(1,int(math.sqrt(N))+1):
    if N%a==0:
        A=N/a
        if A>a:
            gou=A-a
        else:
            gou=a-A
        b=a
        if gou<han:
            han=gou
            kot=A+a

print(int(kot)-2)

Submission status: Accepted

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

Submission status: Accepted

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

a=make_divisors(n)

b=len(a)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

print(ans)

Submission status: Accepted

Solution code:
import math
N = int(input())
res = N
for i in range(1, int(math.sqrt(N)) + 1):
    j = N // i
    if j * i == N:
        res = min(res, j - 1 + i - 1)
print(res)

Submission status: Accepted

Solution code:

def solve():
    ans = 10**12
    for i in range(1, int(N**0.5)+1):
        if N % i == 0:
            ans = min(ans, i + (N//i) -2)
    if N == 2:
        print(1)
    elif N == 3:
        print(2)
    else:
        print(ans)
            
if __name__ == "__main__":  
    N = int(input())
    solve()

Submission status: Accepted

Solution code:
from collections import defaultdict

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import defaultdict, deque

x = int(input()); m = int(x ** 0.5)
while x % m: m -= 1
print(m + (x // m) - 2)

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    ret = MAX

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

n = int(input())
ans = 10 ** 15
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 math
n=int(input())
a=0
b=10**12
for i in range(1, 1+int(math.sqrt(n))):
	if n%i==0:
		a=(i-1)+(n//i)-1
	if a<b:
		b=a
print(b)

Submission status: Accepted

Solution code:
n = int(input())

steps = []

for x in range(int(n ** 0.5)):
  if n % (x + 1) == 0:
    steps.append(x + n // (x + 1) - 1)
print(min(steps))

Submission status: Accepted

Solution code:
n=int(input())

steps = []

for x in range(int(n**0.5)):
  if n % (x+1)==0:
    steps.append(x+n//(x+1)-1)

print(min(steps))

Submission status: Accepted

Solution code:
n = int(input())

steps = []

for x in range(int(n ** 0.5)):
    if n % (x + 1) == 0:
        steps.append(x + n // (x + 1) - 1)

print(min(steps))

Submission status: Accepted

Solution code:
n = int(input())

steps = []
for x in range(int(n ** 0.5)):
  if n % (x + 1) == 0:
    steps.append(x + n // (x + 1) - 1)
print(min(steps))

Submission status: Accepted

Solution code:
from math import sqrt
 
N = int(input())
 
search_max = int(sqrt(N))
 
min_number = 10**12
for x in range(1, search_max + 1):
    if N % x == 0:
        y = N // x
        if x + y < min_number:
            min_number = x + y
print(min_number-2)

Submission status: Accepted

Solution code:
N = int(input())
search_max = int(N**0.5)
 
min_number = 10**12
for x in range(1, search_max + 1):
    if N % x == 0:
        y = N // x
        if x + y < min_number:
         
            min_number = x + y
print(min_number-2)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from math import sqrt

N = int(input())

search_max = int(sqrt(N))

min_number = 10**12
for x in range(1, search_max + 1):
    if N % x == 0:
        y = N // x
        if x + y < min_number:
            min_number = x + y

print(min_number-2)

Submission status: Accepted

Solution code:
def main():
    import math
    n = int(input())
    n_sq = int(math.sqrt(n)) + 1
    tmp = 10 ** 13
    for i in range(n_sq, 1, -1):
        if n % i == 0:
            j = n // i
            k = (i-1) + (j-1)
            if tmp > k :
                tmp = k
    if tmp != 10 ** 13:
        ans = tmp
    else:
        ans = n-1
    print(ans)


    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math
n = int(input())
ans = n - 1

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

Submission status: Accepted

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

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)
INF = float("inf")

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

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

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

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

Solution code:
import math
import sys

def I(): 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 main():
    n = I()
    x = 1
    y = n
    for i in range(int(math.sqrt(n)), 0, -1):
        if n%i == 0:
            x = i
            y = n//i
            break
    ans = x + y -2
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def Divisors(n):
    L=[]
    k=1
    while k*k <= n:
        if n%k== 0:
            L.append(k)
        k+=1
    return L

N=int(input())
T=Divisors(N)

M=10**20
for a in T:
    M=min(M,a+(N//a)-2)
print(M)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
def gcd(*numbers): reduce(math.gcd, numbers)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
count = 0
ans = 0

N = k()
ans = N-1

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

print(ans)

Submission status: Accepted

Solution code:
from itertools import product
n = int(input())

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
x = np.arange(1, int(N ** 0.5 + 1))
div = x[N % x==0]
print(int(np.min(div + N / div)) - 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

N = int(input())
# S = input()
# n, *a = map(int, open(0))
# M, D = map(int, input().split())
# A = list(map(int, input().split()))
# S = input()
memo = 10 ** 100
for i in range(1, int(math.sqrt(N)) + 1):
    if N % i == 0 and memo > (N // i) + i:
        memo = i + (N // i)

print(memo - 2)

Submission status: Accepted

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


if __name__ == '__main__':
    main5()

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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


y = make_divisors(int(input()))

if len(y) % 2 > 0:
    print(y[len(y) // 2] * 2 - 2)
else:
    print(y[len(y) // 2 - 1] + y[len(y) // 2] - 2)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())

# nを割れる整数を1からルートnまで列挙
def divisors(num):
   arr = []
   for i in range(1, int(num**0.5) + 1):
       if num % i == 0:
           arr.append(i)
           if i != num // i:
               arr.append(num//i)
   return arr

n_div = divisors(n)
arr_ = [int(i + n / i) for i in n_div]
print(min(arr_) - 2)

Submission status: Accepted

Solution code:
n=int(input())

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

print(int(result-2))

Submission status: Accepted

Solution code:
n=int(input())

div = n
result = 10**12

for i in range(1,1000001):
  if n % i == 0:
    div = n // i
    result = min(result, i+div)

print(int(result-2))

Submission status: Accepted

Solution code:
def make_divisors(n):
    ans = 10 ** 12 + 10
    i = 1
    while i*i <= n:
        if n % i == 0:
            t = 0
            if i != n // i:
                t = i + (n//i) - 2
            else:
                t = i * 2 - 2
            #print(i, n//i, t)
            if ans > t:
                ans = t
        i += 1
    return ans

n = int(input())
print(make_divisors(n))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N=int(input())
check=1
while check**2<=N:
  if N%check==0:
    keep=N//check
  check+=1
print(keep+N//keep-2)

Submission status: Accepted

Solution code:
import math
n = int(input())
m = math.floor(math.sqrt(n)) + 1
ans = 10**12

for a in range(1,m+1):
    b = n/a
    if b.is_integer():
        move = a+b-2
    else:
        continue
    ans = min(ans,move)
print(int(ans))

Submission status: Accepted

Solution code:
import math

N = int(input())
even = False

if N % 2 == 0:
    even = True

factor1 = int(math.floor(N ** (1/2)))

# if even:  # Nが偶数の時はfactor1を偶数にしておく
#     if factor1 % 2 == 1:
#         factor1 -= 1
# else:
#     if factor1 % 2 == 0:
#         factor1 -= 1

while True:
    if N % factor1 == 0:
        factor2 = int(N / factor1)
        break
    else:
        factor1 -= 1

print(factor1 + factor2 - 2)

Submission status: Accepted

Solution code:
import math

n = int(input())

bi_factors = []

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

inf = float('INF')
ans = inf

for point in bi_factors:
    d = point[0] + point[1] - 2
    if ans > d:
        ans = d

print(ans)

Submission status: Accepted

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