he-025
1.000
Challenge · difficulty 3/5
# HumanEval/25
Implement a file **`solution.py`** that completes the function below. Keep the given name and signature; your file must define it at module level.
```python
from typing import List
def factorize(n: int) -> List[int]:
""" Return list of prime factors of given integer in the order from smallest to largest.
Each of the factors should be listed number of times corresponding to how many times it appeares in factorization.
Input number should be equal to the product of all factors
>>> factorize(8)
[2, 2, 2]
>>> factorize(25)
[5, 5]
>>> factorize(70)
[2, 5, 7]
"""
```
<!-- imported from OpenAI HumanEval (HumanEval/25) -->
tests/test_he_025.py
# Auto-generated from OpenAI HumanEval HumanEval/25. Do not edit by hand.
from solution import * # noqa: F401,F403 (prompt helpers may be referenced by tests)
from solution import factorize as candidate
METADATA = {
'author': 'jt',
'dataset': 'test'
}
def check(candidate):
assert candidate(2) == [2]
assert candidate(4) == [2, 2]
assert candidate(8) == [2, 2, 2]
assert candidate(3 * 19) == [3, 19]
assert candidate(3 * 19 * 3 * 19) == [3, 3, 19, 19]
assert candidate(3 * 19 * 3 * 19 * 3 * 19) == [3, 3, 3, 19, 19, 19]
assert candidate(3 * 19 * 19 * 19) == [3, 19, 19, 19]
assert candidate(3 * 2 * 3) == [2, 3, 3]
def test_he_025():
check(candidate)
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.
Proposed solution
```python
from typing import List
def factorize(n: int) -> List[int]:
""" Return list of prime factors of given integer in the order from smallest to largest.
Each of the factors should be listed number of times corresponding to how many times it appeares in factorization.
Input number should be equal to the product of all factors
>>> factorize(8)
[2, 2, 2]
>>> factorize(25)
[5, 5]
>>> factorize(70)
[2, 5, 7]
"""
factors = []
# Handle factor 2 separately to optimize the loop below
while n % 2 == 0:
factors.append(2)
n //= 2
# Check odd factors from 3 upwards
i = 3
while i * i <= n:
while n % i == 0:
factors.append(i)
n //= i
i += 2
# If n is still greater than 1, it's a prime factor
if n > 1:
factors.append(n)
return factors
```Test output (stdout)
. [100%] 1 passed in 0.01s