https://app.codility.com/programmers/lessons/10-prime_and_composite_numbers/min_perimeter_rectangle/

 

MinPerimeterRectangle coding task - Learn to Code - Codility

Find the minimal perimeter of any rectangle whose area equals N.

app.codility.com

 

  • 직사각형 면적을 나타내는 정수 N
  • N = A*B
  • 2*(A+B)의 값들 중에서 최소 값 찾기
  • O(sqrt(N))

 

import math
def solution(N):
    minValue = float('inf')
    value = 0
    for D in range(1, int(math.sqrt(N))+1):
        if N % D == 0:
            if D*D == N:
                value = 2*(D+D)
                minValue = min(value, minValue)
                break
            else:
                M = N//D
                value = 2*(D+M)
                minValue = min(value, minValue)
    return minValue

+ Recent posts