Tasks Details
easy
1.
CountFactors
Count factors of given number n.
Task Score
100%
Correctness
100%
Performance
100%
A positive integer D is a factor of a positive integer N if there exists an integer M such that N = D * M.
For example, 6 is a factor of 24, because M = 4 satisfies the above condition (24 = 6 * 4).
Write a function:
class Solution { public int solution(int N); }
that, given a positive integer N, returns the number of its factors.
For example, given N = 24, the function should return 8, because 24 has 8 factors, namely 1, 2, 3, 4, 6, 8, 12, 24. There are no other factors of 24.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..2,147,483,647].
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 6 minutes
Notes
not defined yet
Code: 17:23:05 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println 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 r = (int)Math.sqrt(N);
int count = 0;
for (int a=1; a<=r; a++){
if (N%a==0){
int b = N/a;
if (a==b) count++;
else count+=2;
}
}
return count;
}
}
Analysis
Code: 17:23:30 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println 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 r = (int)Math.sqrt(N);
int count = 0;
for (int a=1; a<=r; a++){
if (N%a==0){
int b = N/a;
if (a==b) count++;
else count+=2;
}
}
return count;
}
}
Analysis
Code: 17:23:37 UTC,
java,
final,
score: 
100
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println 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 r = (int)Math.sqrt(N);
int count = 0;
for (int a=1; a<=r; a++){
if (N%a==0){
int b = N/a;
if (a==b) count++;
else count+=2;
}
}
return count;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(sqrt(N))
expand all
Correctness tests
1.
1.325 s
OK
2.
1.336 s
OK
1.
1.321 s
OK
2.
1.322 s
OK
3.
1.325 s
OK
4.
1.317 s
OK
5.
1.321 s
OK
6.
1.311 s
OK
7.
1.335 s
OK
8.
1.322 s
OK
9.
1.318 s
OK
10.
1.308 s
OK
1.
1.265 s
OK
2.
1.135 s
OK
1.
1.316 s
OK
2.
1.318 s
OK
3.
1.318 s
OK
1.
1.318 s
OK
2.
1.067 s
OK
1.
1.313 s
OK
2.
1.328 s
OK
1.
1.313 s
OK
2.
1.323 s
OK
1.
1.321 s
OK
expand all
Performance tests
1.
1.298 s
OK
2.
1.319 s
OK
1.
1.317 s
OK
2.
1.317 s
OK
3.
1.322 s
OK
1.
1.322 s
OK
2.
1.324 s
OK
3.
1.317 s
OK
1.
1.181 s
OK
2.
1.325 s
OK
1.
1.323 s
OK
2.
1.328 s
OK
3.
1.306 s
OK
1.
1.317 s
OK
2.
1.324 s
OK
3.
1.328 s
OK