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 11
Time spent on task 1 minutes
Notes
not defined yet
Task timeline
Code: 05:23:50 UTC,
java,
autosave
// 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");
import java.util.*;
class Solution {
public int solution(int N) {
int ans = 0;
if(N==1){return 1;}
for(int i=1; i*i<=N; i++){
if(i*i==N){
ans ++;
}
else if(N%i==0){
ans +=2;
}
}
return ans;
// Implement your solution here
}
}
Code: 05:24:04 UTC,
java11,
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");
import java.util.*;
class Solution {
public int solution(int N) {
int ans = 0;
if(N==1){return 1;}
for(long i=1; i*i<=N; i++){
if(i*i==N){
ans ++;
}
else if(N%i==0){
ans +=2;
}
}
return ans;
// Implement your solution here
}
}
Analysis
Code: 05:24:11 UTC,
java11,
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");
import java.util.*;
class Solution {
public int solution(int N) {
int ans = 0;
if(N==1){return 1;}
for(long i=1; i*i<=N; i++){
if(i*i==N){
ans ++;
}
else if(N%i==0){
ans +=2;
}
}
return ans;
// Implement your solution here
}
}
Analysis
Code: 05:24:16 UTC,
java11,
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");
import java.util.*;
class Solution {
public int solution(int N) {
int ans = 0;
if(N==1){return 1;}
for(long i=1; i*i<=N; i++){
if(i*i==N){
ans ++;
}
else if(N%i==0){
ans +=2;
}
}
return ans;
// Implement your solution here
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(sqrt(N))
expand all
Correctness tests
1.
0.012 s
OK
2.
0.012 s
OK
1.
0.008 s
OK
2.
0.012 s
OK
3.
0.012 s
OK
4.
0.008 s
OK
5.
0.008 s
OK
6.
0.012 s
OK
7.
0.012 s
OK
8.
0.012 s
OK
9.
0.012 s
OK
10.
0.012 s
OK
1.
0.012 s
OK
2.
0.012 s
OK
1.
0.012 s
OK
2.
0.008 s
OK
3.
0.008 s
OK
1.
0.012 s
OK
2.
0.012 s
OK
1.
0.012 s
OK
2.
0.012 s
OK
1.
0.012 s
OK
2.
0.008 s
OK
1.
0.008 s
OK
expand all
Performance tests
1.
0.012 s
OK
2.
0.012 s
OK
1.
0.012 s
OK
2.
0.012 s
OK
3.
0.012 s
OK
1.
0.008 s
OK
2.
0.012 s
OK
3.
0.012 s
OK
1.
0.012 s
OK
2.
0.012 s
OK
1.
0.012 s
OK
2.
0.012 s
OK
3.
0.012 s
OK
1.
0.012 s
OK
2.
0.012 s
OK
3.
0.012 s
OK