Tasks Details
easy
1.
CountFactors
Count factors of given number n.
Task Score
78%
Correctness
100%
Performance
50%
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:
object Solution { def solution(n: Int): Int }
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 Scala
Time spent on task 9 minutes
Notes
not defined yet
Task timeline
Code: 04:35:08 UTC,
scala,
verify,
result: Failed
Analysis
Compile error
Solution.scala:10: error: ')' expected but integer literal found. else if (N % == 0) foo(i + 1, total + 1) ^ Solution.scala:12: error: illegal start of simple expression } ^ two errors found
Code: 04:35:47 UTC,
scala,
verify,
result: Passed
Analysis
Code: 04:36:01 UTC,
scala,
verify,
result: Passed
Analysis
Code: 04:36:13 UTC,
scala,
final,
score: 
78
Analysis summary
The following issues have been detected: timeout errors.
For example, for the input 268435456 the solution exceeded the time limit.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
2.214 s
OK
2.
2.200 s
OK
1.
2.232 s
OK
2.
2.211 s
OK
3.
2.225 s
OK
4.
2.206 s
OK
5.
2.232 s
OK
6.
2.225 s
OK
7.
2.227 s
OK
8.
2.228 s
OK
9.
2.223 s
OK
10.
2.203 s
OK
1.
2.222 s
OK
2.
2.199 s
OK
1.
2.237 s
OK
2.
2.231 s
OK
3.
2.237 s
OK
1.
2.225 s
OK
2.
2.232 s
OK
1.
2.223 s
OK
2.
2.230 s
OK
1.
2.227 s
OK
2.
2.223 s
OK
1.
2.215 s
OK
expand all
Performance tests
1.
2.216 s
OK
2.
2.224 s
OK
1.
2.221 s
OK
2.
2.235 s
OK
3.
2.225 s
OK
1.
2.322 s
OK
2.
2.354 s
OK
3.
2.335 s
OK
1.
2.526 s
OK
2.
3.070 s
TIMEOUT ERROR,
running time: 3.07 sec., time limit: 2.99 sec.
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
running time: 3.68 sec., time limit: 2.97 sec.
running time: 3.68 sec., time limit: 2.97 sec.
1.
3.681 s
TIMEOUT ERROR,
running time: 3.68 sec., time limit: 2.97 sec.
2.
4.610 s
TIMEOUT ERROR,
running time: 4.61 sec., time limit: 2.77 sec.
3.
3.602 s
TIMEOUT ERROR,
running time: 3.60 sec., time limit: 2.95 sec.
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
running time: 5.79 sec., time limit: 2.98 sec.
running time: 5.79 sec., time limit: 2.98 sec.
1.
5.792 s
TIMEOUT ERROR,
running time: 5.79 sec., time limit: 2.98 sec.
2.
8.000 s
TIMEOUT ERROR,
running time: >8.00 sec., time limit: 3.00 sec.
3.
9.000 s
TIMEOUT ERROR,
running time: >9.00 sec., time limit: 3.05 sec.