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–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Python
Time spent on task 8 minutes
Notes
not defined yet
Task timeline
Code: 08:20:54 UTC,
java,
autosave
Code: 08:23:58 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 'NoneType'> found.stdout:
5
Code: 08:24:16 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 'NoneType'> found.stdout:
5 1 2 3 4 5
Code: 08:25:00 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 'NoneType'> found.stdout:
5 1 0 2 0 3 0 4 2 5 0
Code: 08:25:14 UTC,
py,
autosave
Code: 08:25:22 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 'NoneType'> found.stdout:
5 1 1 30.0 2 2 15.0 3 3 10.0 4 5 5 6.0
Code: 08:25:46 UTC,
py,
autosave
Code: 08:25:59 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import math
def solution(N):
min_num = float('INF')
root_value = int(math.floor(N ** (1/2)))
print(root_value)
for i in range(1, root_value + 1):
print(i)
if N % i == 0:
print(i)
print(N / i)
Code: 08:26:25 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import math
def solution(N):
min_num = float('INF')
root_value = int(math.floor(N ** (1/2)))
print(root_value)
for i in range(1, root_value + 1):
print(i)
if N % i == 0:
min_num = min(min_num, i + N/i)print(i)
print(N / i)
Code: 08:26:37 UTC,
py,
autosave
Code: 08:26:48 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 'NoneType'> found.stdout:
11.0
Code: 08:27:07 UTC,
py,
autosave
Code: 08:27:10 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 'NoneType'> found.stdout:
1 30.0 2 15.0 3 10.0 5 6.0
Code: 08:27:37 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import math
def solution(N):
min_num = float('INF')
root_value = int(math.floor(N ** (1/2)))
for i in range(1, root_value + 1):
if N % i == 0:
print(i, N / i)
min_num = min(min_num, (i + N/i) * 2)
Code: 08:27:38 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import math
def solution(N):
min_num = float('INF')
root_value = int(math.floor(N ** (1/2)))
for i in range(1, root_value + 1):
if N % i == 0:
print(i, N / i)
min_num = min(min_num, (i + N/i) * 2)
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 'NoneType'> found.stdout:
1 30.0 2 15.0 3 10.0 5 6.0
Code: 08:27:47 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import math
def solution(N):
min_num = float('INF')
root_value = int(math.floor(N ** (1/2)))
for i in range(1, root_value + 1):
if N % i == 0:
print(i, N / i)
min_num = min(min_num, (i + N/i) * 2)
Code: 08:27:51 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import math
def solution(N):
min_num = float('INF')
root_value = int(math.floor(N ** (1/2)))
for i in range(1, root_value + 1):
if N % i == 0:
print(i, N / i)
min_num = min(min_num, int(i + N/i) * 2)
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 'NoneType'> found.stdout:
1 30.0 2 15.0 3 10.0 5 6.0
Code: 08:28:01 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import math
def solution(N):
min_num = float('INF')
root_value = int(math.floor(N ** (1/2)))
for i in range(1, root_value + 1):
if N % i == 0:
print(i, N / i)
min_num = min(min_num, int(i + N/i) * 2)
print(min_num)
Analysis
expand all
Example tests
1.
0.040 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
1 30.0 2 15.0 3 10.0 5 6.0 22
Code: 08:28:12 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import math
def solution(N):
min_num = float('INF')
root_value = int(math.floor(N ** (1/2)))
for i in range(1, root_value + 1):
if N % i == 0:
print(i, N / i)
min_num = min(min_num, int(i + N/i) * 2)
return min_num
Code: 08:28:14 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):
min_num = float('INF')
root_value = int(math.floor(N ** (1/2)))
for i in range(1, root_value + 1):
if N % i == 0:
min_num = min(min_num, int(i + N/i) * 2)
return min_num
Analysis
Code: 08:28:19 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):
min_num = float('INF')
root_value = int(math.floor(N ** (1/2)))
for i in range(1, root_value + 1):
if N % i == 0:
min_num = min(min_num, int(i + N/i) * 2)
return min_num
Analysis
Code: 08:28:21 UTC,
py,
final,
score: 
100
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import math
def solution(N):
min_num = float('INF')
root_value = int(math.floor(N ** (1/2)))
for i in range(1, root_value + 1):
if N % i == 0:
min_num = min(min_num, int(i + N/i) * 2)
return min_num
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(sqrt(N))