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–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Java 8
Time spent on task 3 minutes
Notes
not defined yet
Task timeline
Code: 15:01:11 UTC,
java,
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");
class Solution {
public int solution(int N) {
// write your code in Java SE 8
int count = 0, i=1;
double max = Math.sqrt(N);
for(; i < max; i++) {
if(N % i ==0) {
count += 2;
}
}
if(i*i == N){
count++;
}
return count;
}
}
Analysis
Code: 15:01:31 UTC,
java,
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");
class Solution {
public int solution(int N) {
// write your code in Java SE 8
int count = 0, i=1;
double max = Math.sqrt(N);
for(; i < max; i++) {
if(N % i ==0) {
count += 2;
}
}
if(i*i == N){
count++;
}
return count;
}
}
User test case 1:
2,147,483,647
Analysis
expand all
User tests
1.
0.001 s
RUNTIME ERROR,
invalid input, unexpected token at the end of input: 147
Code: 15:01:53 UTC,
java,
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");
class Solution {
public int solution(int N) {
// write your code in Java SE 8
int count = 0, i=1;
double max = Math.sqrt(N);
for(; i < max; i++) {
if(N % i ==0) {
count += 2;
}
}
if(i*i == N){
count++;
}
return count;
}
}
User test case 1:
[2147483647]
Analysis
Code: 15:02:05 UTC,
java,
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");
class Solution {
public int solution(int N) {
// write your code in Java SE 8
int count = 0, i=1;
double max = Math.sqrt(N);
for(; i < max; i++) {
if(N % i ==0) {
count += 2;
}
}
if(i*i == N){
count++;
}
return count;
}
}
User test case 1:
[2147483647]
Analysis
Code: 15:02:17 UTC,
java,
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");
class Solution {
public int solution(int N) {
// write your code in Java SE 8
int count = 0, i=1;
double max = Math.sqrt(N);
for(; i < max; i++) {
if(N % i ==0) {
count += 2;
}
}
if(i*i == N){
count++;
}
return count;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(sqrt(N))
expand all
Correctness tests
1.
0.084 s
OK
2.
0.126 s
OK
1.
0.041 s
OK
2.
0.031 s
OK
3.
0.087 s
OK
4.
0.030 s
OK
5.
0.032 s
OK
6.
0.049 s
OK
7.
0.094 s
OK
8.
0.082 s
OK
9.
0.031 s
OK
10.
0.030 s
OK
1.
0.085 s
OK
2.
0.129 s
OK
1.
0.085 s
OK
2.
0.140 s
OK
3.
0.039 s
OK
1.
0.051 s
OK
2.
0.181 s
OK
1.
0.031 s
OK
2.
0.043 s
OK
1.
0.064 s
OK
2.
0.076 s
OK
1.
0.053 s
OK
expand all
Performance tests
1.
0.031 s
OK
2.
0.030 s
OK
1.
0.123 s
OK
2.
0.094 s
OK
3.
0.065 s
OK
1.
0.215 s
OK
2.
0.129 s
OK
3.
0.059 s
OK
1.
0.077 s
OK
2.
0.048 s
OK
1.
0.074 s
OK
2.
0.032 s
OK
3.
0.053 s
OK
1.
0.031 s
OK
2.
0.055 s
OK
3.
0.033 s
OK