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:
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–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used C++
Time spent on task 2 minutes
Notes
not defined yet
Task timeline
Code: 14:36:21 UTC,
java,
autosave
Code: 14:36:46 UTC,
cpp,
autosave
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N){
int res = 0;
float sqr = sqrt(N);
for(int i=1;i<=sqr;i++){
if (N % i == 0 ) res+=2;
//don't double count sq root
if ( i == sqr ) res-=1;
}
return res;
};
Code: 14:36:46 UTC,
cpp,
verify,
result: Failed
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N){
int res = 0;
float sqr = sqrt(N);
for(int i=1;i<=sqr;i++){
if (N % i == 0 ) res+=2;
//don't double count sq root
if ( i == sqr ) res-=1;
}
return res;
};
Analysis
Compile error
func.cpp: In function 'int solution(int)': func.cpp:10:21: error: 'sqrt' was not declared in this scope float sqr = sqrt(N); ^
Code: 14:37:08 UTC,
cpp,
verify,
result: Passed
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
#include <math.h>
int solution(int N){
int res = 0;
float sqr = sqrt(N);
for(int i=1;i<=sqr;i++){
if (N % i == 0 ) res+=2;
//don't double count sq root
if ( i == sqr ) res-=1;
}
return res;
};
Analysis
Code: 14:37:24 UTC,
cpp,
verify,
result: Passed
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
#include <math.h>
int solution(int N){
int res = 0;
float sqr = sqrt(N);
for(int i=1;i<=sqr;i++){
if (N % i == 0 ) res+=2;
//don't double count sq root
if ( i == sqr ) res-=1;
}
return res;
};
Analysis
Code: 14:37:27 UTC,
cpp,
final,
score: 
100
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
#include <math.h>
int solution(int N){
int res = 0;
float sqr = sqrt(N);
for(int i=1;i<=sqr;i++){
if (N % i == 0 ) res+=2;
//don't double count sq root
if ( i == sqr ) res-=1;
}
return res;
};
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(sqrt(N))
expand all
Correctness tests
1.
0.001 s
OK
2.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
4.
0.001 s
OK
5.
0.001 s
OK
6.
0.001 s
OK
7.
0.001 s
OK
8.
0.001 s
OK
9.
0.001 s
OK
10.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
1.
0.001 s
OK
expand all
Performance tests
1.
0.001 s
OK
2.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK