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 66 minutes
Notes
not defined yet
Task timeline
Code: 06:58:48 UTC,
java,
autosave
Code: 07:43:36 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:
4.898979485566356
Code: 07:44:04 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 6, in solution for i in range(1, N ** (1/2)): TypeError: 'float' object cannot be interpreted as an integer
Code: 07:44:20 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:
1 2 3
Code: 07:44:48 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:
1 2 3
Code: 07:45:01 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:
1 2 3 4
Code: 08:00:54 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:
1 2 3 4
Code: 08:02:21 UTC,
py,
autosave
Code: 08:02:34 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.
Code: 08:02:44 UTC,
py,
verify,
result: Passed
Analysis
Code: 08:02:51 UTC,
py,
autosave
Code: 08:02:58 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import math
def solution(N):
squre_root = math.floor(N ** (1/2))
answer = 0
for i in range(1, squre_root + 1):
if i * i == N:
answer += 1
return answer
answer += 2
return answer
User test case 1:
[81]
Analysis
expand all
User tests
1.
0.001 s
RUNTIME ERROR,
invalid input, unexpected '[', expecting integer
Code: 08:03:04 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import math
def solution(N):
squre_root = math.floor(N ** (1/2))
answer = 0
for i in range(1, squre_root + 1):
if i * i == N:
answer += 1
return answer
answer += 2
return answer
User test case 1:
[81]
Analysis
Code: 08:03:30 UTC,
py,
autosave
Code: 08:03:43 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import math
def solution(N):
squre_root = math.floor(N ** (1/2))
answer = 0
for i in range(1, squre_root + 1):
if i * i == N:
answer += 1
return answer
if N % i == 0:
answer += 2
return answer
Code: 08:03:46 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import math
def solution(N):
squre_root = math.floor(N ** (1/2))
answer = 0
for i in range(1, squre_root + 1):
if i * i == N:
answer += 1
return answer
if N % i == 0:
answer += 2
return answer
User test case 1:
[81]
Analysis
Code: 08:04:22 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import math
def solution(N):
squre_root = math.floor(N ** (1/2))
answer = 0
for i in range(1, squre_root + 1):
if i * i == N:
answer += 1
return answer
if N % i == 0:
answer += 2
return answer
User test case 1:
[81]
Analysis
Code: 08:04:25 UTC,
py,
final,
score: 
100
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import math
def solution(N):
squre_root = math.floor(N ** (1/2))
answer = 0
for i in range(1, squre_root + 1):
if i * i == N:
answer += 1
return answer
if N % i == 0:
answer += 2
return answer
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.036 s
OK
1.
0.036 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