-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy path10_MinPerimeterRectangle.py
66 lines (49 loc) · 1.92 KB
/
10_MinPerimeterRectangle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
MinPerimeterRectangle - Find the minimal perimeter of any rectangle whose area equals N.
An integer N is given, representing the area of some rectangle.
The area of a rectangle whose sides are of length A and B is A * B, and the perimeter is 2 * (A + B).
The goal is to find the minimal perimeter of any rectangle whose area equals N.
The sides of this rectangle should be only integers.
For example, given integer N = 30, rectangles of area 30 are:
(1, 30), with a perimeter of 62,
(2, 15), with a perimeter of 34,
(3, 10), with a perimeter of 26,
(5, 6), with a perimeter of 22.
Write a function:
def solution(N)
that, given an integer N, returns the minimal perimeter of any rectangle whose area is exactly equal to N.
For example, given an integer N = 30, the function should return 22, as explained above.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..1,000,000,000].
Copyright 2009–2023 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
#
https://app.codility.com/demo/results/trainingWAMZPR-DH7/
"""
import math
import sys
import unittest
def solution(n: int) -> int:
"""
For all the integer factors of n
calculate the perimeter
return the smallest perimeter
"""
min_perim = sys.maxsize
for A in range(1, int(math.sqrt(n)) + 1):
if n % A == 0:
B = n // A
perimeter = 2 * (A + B)
min_perim = min(min_perim, perimeter)
return min_perim
class TestExercise(unittest.TestCase):
"""
"""
def test_examples(self):
self.assertEqual(solution(1), 4)
self.assertEqual(solution(2), 6)
self.assertEqual(solution(3), 8)
self.assertEqual(solution(4), 8)
self.assertEqual(solution(5), 12)
self.assertEqual(solution(30), 22)
if __name__ == "__main__":
unittest.main()