Skip to content

Commit cd327b9

Browse files
authored
Create MinPerimeterRectangle.md
1 parent 0e08225 commit cd327b9

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

MinPerimeterRectangle.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
### Task description
3+
4+
An integer N is given, representing the area of some rectangle.
5+
6+
The area of a rectangle whose sides are of length A and B is A * B, and the perimeter is 2 * (A + B).
7+
8+
The goal is to find the minimal perimeter of any rectangle whose area equals N. The sides of this rectangle should be only integers.
9+
10+
For example, given integer N = 30, rectangles of area 30 are:
11+
12+
* (1, 30), with a perimeter of 62,
13+
* (2, 15), with a perimeter of 34,
14+
* (3, 10), with a perimeter of 26,
15+
* (5, 6), with a perimeter of 22.
16+
17+
Write a function:
18+
19+
`function solution(N);`
20+
21+
that, given an integer N, returns the minimal perimeter of any rectangle whose area is exactly equal to N.
22+
23+
For example, given an integer N = 30, the function should return 22, as explained above.
24+
25+
Assume that:
26+
27+
* N is an integer within the range [1..1,000,000,000].
28+
29+
Complexity:
30+
31+
* expected worst-case time complexity is O(sqrt(N));
32+
* expected worst-case space complexity is O(1).
33+
34+
```javascript
35+
function solution(N) {
36+
// write your code in JavaScript (Node.js 6.4.0)
37+
for (var i = parseInt(Math.sqrt(N), 10); true ; i--)
38+
if (N % i == 0)
39+
return 2 * i + 2 * (N / i);
40+
}
41+
```

0 commit comments

Comments
 (0)