Tasks Details
easy
Find the minimal perimeter of any rectangle whose area equals N.
Task Score
100%
Correctness
100%
Performance
100%
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–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Python
Time spent on task 4 minutes
Notes
not defined yet
Task timeline
Code: 02:07:28 UTC,
java,
autosave
Code: 02:08:46 UTC,
py,
autosave
Code: 02:09:11 UTC,
py,
autosave
Code: 02:09:40 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'float'> found.
Code: 02:10:03 UTC,
py,
autosave
Code: 02:10:04 UTC,
py,
verify,
result: Passed
Analysis
Code: 02:10:12 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import math
def solution(N):
# write your code in Python 3.6
i = int(math.sqrt(N))
while i >= 1:
if N % i == 0:
return 2 * (N // i + i)
i -= 1
User test case 1:
[36]
Analysis
Code: 02:10:30 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import math
def solution(N):
# write your code in Python 3.6
i = int(math.sqrt(N))
while i >= 1:
if N % i == 0:
return 2 * (N // i + i)
i -= 1
User test case 1:
[36]
Analysis
Code: 02:10:33 UTC,
py,
final,
score: 
100
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(sqrt(N))