Tasks Details
easy
1.
CountFactors
Count factors of given number n.
Task Score
85%
Correctness
100%
Performance
66%
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 1 minutes
Notes
not defined yet
Task timeline
Code: 13:49:07 UTC,
java,
autosave
Code: 13:49:10 UTC,
java,
verify,
result: Failed
// 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 result = 1;
Map<Integer, Integer> hashMap = new HashMap<>();
int count = 0;
int beforeDiv = 2;
int div = 2;
while (N > 1) {
if (N % div == 0) {
N /= div;
hashMap.put(div, hashMap.getOrDefault(div, 0) + 1);
//다시 초기화 할필요가 없다
count ++;
// div = 2;
} else {
if(count > 0){
result *= (count +1);
count = 0;
}
div++;
}
}
if(count > 0){
result *= (count +1);
}
return result;
}
}
Analysis
Compile error
Solution.java:11: error: cannot find symbol Map<Integer, Integer> hashMap = new HashMap<>(); ^ symbol: class Map location: class Solution Solution.java:11: error: cannot find symbol Map<Integer, Integer> hashMap = new HashMap<>(); ^ symbol: class HashMap location: class Solution 2 errors
Code: 13:49:18 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 result = 1;
Map<Integer, Integer> hashMap = new HashMap<>();
int count = 0;
int beforeDiv = 2;
int div = 2;
while (N > 1) {
if (N % div == 0) {
N /= div;
hashMap.put(div, hashMap.getOrDefault(div, 0) + 1);
//다시 초기화 할필요가 없다
count ++;
// div = 2;
} else {
if(count > 0){
result *= (count +1);
count = 0;
}
div++;
}
}
if(count > 0){
result *= (count +1);
}
return result;
}
}
Analysis
Code: 13:49:23 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 result = 1;
Map<Integer, Integer> hashMap = new HashMap<>();
int count = 0;
int beforeDiv = 2;
int div = 2;
while (N > 1) {
if (N % div == 0) {
N /= div;
hashMap.put(div, hashMap.getOrDefault(div, 0) + 1);
//다시 초기화 할필요가 없다
count ++;
// div = 2;
} else {
if(count > 0){
result *= (count +1);
count = 0;
}
div++;
}
}
if(count > 0){
result *= (count +1);
}
return result;
}
}
Analysis
Code: 13:49:28 UTC,
java,
final,
score: 
85
// 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 result = 1;
Map<Integer, Integer> hashMap = new HashMap<>();
int count = 0;
int beforeDiv = 2;
int div = 2;
while (N > 1) {
if (N % div == 0) {
N /= div;
hashMap.put(div, hashMap.getOrDefault(div, 0) + 1);
//다시 초기화 할필요가 없다
count ++;
// div = 2;
} else {
if(count > 0){
result *= (count +1);
count = 0;
}
div++;
}
}
if(count > 0){
result *= (count +1);
}
return result;
}
}
Analysis summary
The following issues have been detected: timeout errors.
For example, for the input 780291637 the solution exceeded the time limit.
Analysis
Detected time complexity:
O(sqrt(N)) or O(N)
expand all
Correctness tests
1.
0.004 s
OK
2.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK
5.
0.004 s
OK
6.
0.004 s
OK
7.
0.004 s
OK
8.
0.004 s
OK
9.
0.008 s
OK
10.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.008 s
OK
1.
0.008 s
OK
2.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
1.
0.004 s
OK
expand all
Performance tests
1.
0.004 s
OK
2.
0.004 s
OK
1.
0.004 s
OK
2.
0.012 s
OK
3.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
1.
0.008 s
OK
2.
0.004 s
OK
big3
N=479,001,600=12!, N=780291637(prime), N=449,991,369
N=479,001,600=12!, N=780291637(prime), N=449,991,369
✘
TIMEOUT ERROR
Killed. Hard limit reached: 6.000 sec.
Killed. Hard limit reached: 6.000 sec.
1.
0.004 s
OK
2.
6.000 s
TIMEOUT ERROR,
Killed. Hard limit reached: 6.000 sec.
3.
0.004 s
OK
extreme_maxint
N=1,000,000,000, N=MAX_INT, N=2147,395,600
N=1,000,000,000, N=MAX_INT, N=2147,395,600
✘
TIMEOUT ERROR
Killed. Hard limit reached: 6.000 sec.
Killed. Hard limit reached: 6.000 sec.
1.
0.004 s
OK
2.
6.000 s
TIMEOUT ERROR,
Killed. Hard limit reached: 6.000 sec.
3.
0.004 s
OK