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:
class Solution { public int solution(int 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 Java 21
Time spent on task 14 minutes
Notes
not defined yet
Code: 10:12:18 UTC,
java,
autosave
Code: 10:25:08 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N) {
// write your code in Java SE 8
int sqrtN = (int)Math.sqrt(N);
//System.out.println(sqrtN);
int B = 0;
int peri = Integer.MAX_VALUE;
for (int A=1; A<=sqrtN; A++) {
if (N%A == 0) {
B = N/A;
peri = Math.min(2*(A+B), peri);
}
}
return peri;
}
}
User test case 1:
[30]
User test case 2:
[20]
User test case 3:
[10]
User test case 4:
[1]
User test case 5:
[5]
User test case 6:
[9]
Analysis
expand all
User tests
1.
0.004 s
OK
function result: 22
function result: 22
1.
0.004 s
OK
function result: 18
function result: 18
1.
0.004 s
OK
function result: 14
function result: 14
1.
0.004 s
OK
function result: 4
function result: 4
1.
0.004 s
OK
function result: 12
function result: 12
1.
0.004 s
OK
function result: 12
function result: 12
Code: 10:25:17 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N) {
// write your code in Java SE 8
int sqrtN = (int)Math.sqrt(N);
//System.out.println(sqrtN);
int B = 0;
int peri = Integer.MAX_VALUE;
for (int A=1; A<=sqrtN; A++) {
if (N%A == 0) {
B = N/A;
peri = Math.min(2*(A+B), peri);
}
}
return peri;
}
}
User test case 1:
[30]
User test case 2:
[20]
User test case 3:
[10]
User test case 4:
[1]
User test case 5:
[5]
User test case 6:
[9]
Analysis
expand all
User tests
1.
0.004 s
OK
function result: 22
function result: 22
1.
0.008 s
OK
function result: 18
function result: 18
1.
0.008 s
OK
function result: 14
function result: 14
1.
0.004 s
OK
function result: 4
function result: 4
1.
0.004 s
OK
function result: 12
function result: 12
1.
0.008 s
OK
function result: 12
function result: 12
Code: 10:25:22 UTC,
java,
final,
score: 
100
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N) {
// write your code in Java SE 8
int sqrtN = (int)Math.sqrt(N);
//System.out.println(sqrtN);
int B = 0;
int peri = Integer.MAX_VALUE;
for (int A=1; A<=sqrtN; A++) {
if (N%A == 0) {
B = N/A;
peri = Math.min(2*(A+B), peri);
}
}
return peri;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(sqrt(N))