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:
def 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 Python
Time spent on task 7 minutes
Notes
not defined yet
Code: 13:52:33 UTC,
java,
autosave
Code: 13:53:09 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Code: 13:54:00 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.040 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 64, in main sol = __import__('solution') File "/tmp/solution.py", line 5 i = 0: ^ SyntaxError: invalid syntax
Code: 13:54:06 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
0 1 2 3 4
Code: 13:55:09 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 64, in main sol = __import__('solution') File "/tmp/solution.py", line 8 if N%i== 0 ^ SyntaxError: invalid syntax
Code: 13:55:18 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 8, in solution if N%i == 0: ZeroDivisionError: integer division or modulo by zero
Code: 13:55:58 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 9, in solution if N%i == 0: ZeroDivisionError: integer division or modulo by zerostdout:
-- 0
Code: 13:56:10 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.040 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
-- 1 1 -- 2 2 -- 3 3 -- 4 4
Code: 13:56:40 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
WRONG ANSWER,
got 0 expected 8
stdout:
-- 1 1 -- 2 2 -- 3 3 -- 4 4
Code: 13:57:03 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
WRONG ANSWER,
got 4 expected 8
stdout:
-- 1 -- 2 -- 3 -- 4
Code: 13:57:08 UTC,
py,
verify,
result: Passed
Analysis
expand all
Example tests
1.
0.036 s
OK
stdout:
-- 1 -- 2 -- 3 -- 4
Code: 13:58:16 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
i = 1
factors = 0
while (i * i < N):
if N%i == 0:
factors += 2
i += 1
return factors
User test case 1:
[64]
Analysis
Code: 13:58:47 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
i = 1
factors = 0
while (i * i < N):
if N%i == 0:
factors += 2
i += 1
if i*i == N:
factors += 1
return factors
User test case 1:
[64]
Analysis
Code: 13:58:57 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
i = 1
factors = 0
while (i * i < N):
if N%i == 0:
factors += 2
i += 1
if i*i == N:
factors += 1
return factors
User test case 1:
[64]
User test case 2:
[1]
Analysis
Code: 13:59:10 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
i = 1
factors = 0
while (i * i < N):
if N%i == 0:
factors += 2
i += 1
if i*i == N:
factors += 1
return factors
User test case 1:
[64]
User test case 2:
[1]
Analysis
Code: 13:59:16 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
i = 1
factors = 0
while (i * i < N):
if N%i == 0:
factors += 2
i += 1
if i*i == N:
factors += 1
return factors
User test case 1:
[64]
User test case 2:
[1]
Analysis
Code: 13:59:18 UTC,
py,
final,
score: 
100
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(sqrt(N))
expand all
Correctness tests
1.
0.036 s
OK
2.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
7.
0.036 s
OK
8.
0.036 s
OK
9.
0.036 s
OK
10.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
1.
0.036 s
OK
expand all
Performance tests
1.
0.036 s
OK
2.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
1.
0.036 s
OK
2.
0.040 s
OK
1.
0.040 s
OK
2.
0.040 s
OK
3.
0.040 s
OK
1.
0.040 s
OK
2.
0.044 s
OK
3.
0.044 s
OK