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 9 minutes
Notes
not defined yet
Code: 17:32:20 UTC,
java,
autosave
Analysis
expand all
Example tests
1.
0.072 s
WRONG ANSWER,
got 2 expected 8
Code: 17:36:48 UTC,
js,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.072 s
WRONG ANSWER,
got 2 expected 8
stdout:
1 0
Code: 17:38:23 UTC,
js,
autosave
Code: 17:38:27 UTC,
js,
verify,
result: Passed
Analysis
expand all
Example tests
1.
0.076 s
OK
stdout:
1 0 2 0 3 0 4 0
Code: 17:38: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) {
var factors=0
for(var i=1;i<=Math.sqrt(N);i++){
if(N%i==0)
factors+=2;
}
return factors
}
User test case 1:
[9]
Analysis
Code: 17:39:39 UTC,
js,
autosave
Code: 17:39:49 UTC,
js,
autosave
Code: 17:39:54 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) {
var factors=0
var root=Math.sqrt(N)
for(var i=1;i<=root;i++){
if(N%i==0)
if(N==root)
factors++
else
factors+=2;
}
return factors
}
User test case 1:
[9]
Analysis
Code: 17:40:09 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) {
var factors=0
var root=Math.sqrt(N)
for(var i=1;i<=root;i++){
if(N%i==0)
if(i==root)
factors++
else
factors+=2;
}
return factors
}
User test case 1:
[9]
Analysis
Code: 17:40:20 UTC,
js,
autosave
Code: 17:40:26 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) {
var factors=0
var root=Math.sqrt(N)
for(var i=1;i<=root;i++){
if(N%i==0)
if(i==root)
factors++
else
factors+=2;
}
return factors
}
User test case 1:
[9]
User test case 2:
[1]
User test case 3:
[0]
User test case 4:
[13]
Analysis
expand all
User tests
1.
0.072 s
OK
function result: 3
function result: 3
1.
0.072 s
OK
function result: 1
function result: 1
1.
0.068 s
OK
function result: 0
function result: 0
1.
0.072 s
OK
function result: 2
function result: 2
Code: 17:41:03 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) {
var factors=0
var root=Math.sqrt(N)
for(var i=1;i<=root;i++){
if(N%i==0)
if(i==root)
factors++
else
factors+=2;
}
return factors
}
User test case 1:
[9]
User test case 2:
[1]
User test case 3:
[0]
User test case 4:
[13]
Analysis
expand all
User tests
1.
0.072 s
OK
function result: 3
function result: 3
1.
0.072 s
OK
function result: 1
function result: 1
1.
0.072 s
OK
function result: 0
function result: 0
1.
0.072 s
OK
function result: 2
function result: 2
Code: 17:41:06 UTC,
js,
final,
score: 
100
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.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
4.
0.068 s
OK
5.
0.068 s
OK
6.
0.068 s
OK
7.
0.068 s
OK
8.
0.068 s
OK
9.
0.068 s
OK
10.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
1.
0.068 s
OK
expand all
Performance tests
1.
0.104 s
OK
2.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
1.
0.072 s
OK
2.
0.072 s
OK
3.
0.072 s
OK
1.
0.068 s
OK
2.
0.072 s
OK
3.
0.072 s
OK