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:
function solution(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 JavaScript
Time spent on task 2 minutes
Notes
not defined yet
Code: 03:41:48 UTC,
java,
autosave
Code: 03:42:05 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N) {
// write your code in JavaScript (Node.js 8.9.4)
let [cnt, i] = [0, 1];
while (Math.pow(i,2) <= N){
if (N % i === 0){
cnt += Math.pow(i,2) === N ? 1 : 2;
}
i++;
}
return cnt
}
Code: 03:42:18 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N) {
// write your code in JavaScript (Node.js 8.9.4)
let [cnt, i] = [0, 1];
while (Math.pow(i,2) <= N){
if (N % i === 0) cnt += Math.pow(i,2) === N ? 1 : 2;
i++;
}
return cnt
}
Code: 03:42:29 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N) {
// write your code in JavaScript (Node.js 8.9.4)
let [cnt, i] = [0, 1];
while (Math.pow(i,2) <= N){
if (N % i) cnt += Math.pow(i,2) === N ? 1 : 2;
i++;
}
return cnt
}
Code: 03:42:39 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) {
// write your code in JavaScript (Node.js 8.9.4)
let [cnt, i] = [0, 1];
while (Math.pow(i,2) <= N){
if (!(N % i)) cnt += Math.pow(i,2) === N ? 1 : 2;
i++;
}
return cnt
}
Analysis
Code: 03:42:47 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) {
// write your code in JavaScript (Node.js 8.9.4)
let [cnt, i] = [0, 1];
while (Math.pow(i,2) <= N){
if (!(N % i)) cnt += Math.pow(i,2) === N ? 1 : 2;
i++;
}
return cnt
}
Analysis
Code: 03:42:50 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) {
// write your code in JavaScript (Node.js 8.9.4)
let [cnt, i] = [0, 1];
while (Math.pow(i,2) <= N){
if (!(N % i)) cnt += Math.pow(i,2) === N ? 1 : 2;
i++;
}
return cnt
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(sqrt(N))
expand all
Correctness tests
1.
0.068 s
OK
2.
0.072 s
OK
1.
0.072 s
OK
2.
0.072 s
OK
3.
0.068 s
OK
4.
0.072 s
OK
5.
0.068 s
OK
6.
0.076 s
OK
7.
0.072 s
OK
8.
0.072 s
OK
9.
0.068 s
OK
10.
0.072 s
OK
1.
0.072 s
OK
2.
0.072 s
OK
1.
0.068 s
OK
2.
0.072 s
OK
3.
0.072 s
OK
1.
0.072 s
OK
2.
0.068 s
OK
1.
0.072 s
OK
2.
0.072 s
OK
1.
0.072 s
OK
2.
0.068 s
OK
1.
0.068 s
OK
expand all
Performance tests
1.
0.072 s
OK
2.
0.072 s
OK
1.
0.072 s
OK
2.
0.068 s
OK
3.
0.072 s
OK
1.
0.068 s
OK
2.
0.072 s
OK
3.
0.072 s
OK
1.
0.072 s
OK
2.
0.072 s
OK
1.
0.072 s
OK
2.
0.072 s
OK
3.
0.072 s
OK
1.
0.076 s
OK
2.
0.076 s
OK
3.
0.076 s
OK