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:
function 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 JavaScript
Time spent on task 9 minutes
Notes
not defined yet
Code: 13:08:13 UTC,
java,
autosave
Code: 13:15:19 UTC,
js,
autosave
Code: 13:15:30 UTC,
js,
autosave
Code: 13:15:40 UTC,
js,
autosave
Code: 13:15:51 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N) {
let minPerimeter = Number.MAX_SAFE_INTEGER;
for (let i = 1; i * i <= M; i++){
if (N % i === 0) {
minPerimeter = Math.min(minPerimeter, 2 * i * (N / i));
}
}
return minPerimeter;
}
Analysis
expand all
Example tests
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
ReferenceError: M is not defined at solution (solution.js:7:30) at solutionWrapper (/tmp/exec.js:394:28) at Promise.resolve.then (/tmp/exec.js:420:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
Code: 13:15:58 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N) {
let minPerimeter = Number.MAX_SAFE_INTEGER;
for (let i = 1; i * i <= N; i++){
if (N % i === 0) {
minPerimeter = Math.min(minPerimeter, 2 * i * (N / i));
}
}
return minPerimeter;
}
Analysis
expand all
Example tests
1.
0.072 s
WRONG ANSWER,
got 60 expected 22
Code: 13:16:18 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N) {
let minPerimeter = Number.MAX_SAFE_INTEGER;
for (let i = 1; i * i <= N; i++){
if (N % i === 0) {
minPerimeter = Math.min(minPerimeter, 2 * i + (N / i));
}
}
return minPerimeter;
}
Code: 13:16:28 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N) {
let minPerimeter = Number.MAX_SAFE_INTEGER;
for (let i = 1; i * i <= N; i++){
if (N % i === 0) {
minPerimeter = Math.min(minPerimeter, 2 * (i + N / i));
}
}
return minPerimeter;
}
Analysis
Code: 13:16:33 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N) {
let minPerimeter = Number.MAX_SAFE_INTEGER;
for (let i = 1; i * i <= N; i++){
if (N % i === 0) {
minPerimeter = Math.min(minPerimeter, 2 * (i + N / i));
}
}
return minPerimeter;
}
Analysis
Code: 13:16:36 UTC,
js,
final,
score: 
100
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N) {
let minPerimeter = Number.MAX_SAFE_INTEGER;
for (let i = 1; i * i <= N; i++){
if (N % i === 0) {
minPerimeter = Math.min(minPerimeter, 2 * (i + N / i));
}
}
return minPerimeter;
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(sqrt(N))