Created
June 21, 2025 05:14
-
-
Save vbresan/c26598c422a858d0a6a9cef535594268 to your computer and use it in GitHub Desktop.
Python function that returns a list of prime factors. Works for negative numbers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import List | |
| def solve(incoming: int) -> List[int]: | |
| """ | |
| Returns a list of prime factors. Works for negative numbers. | |
| """ | |
| if incoming >= -1 and incoming <= 1: | |
| return [incoming] | |
| multiplier = 1 if incoming > 0 else -1 | |
| incoming = abs(incoming) | |
| factors = [] | |
| for i in range(2, int(incoming ** 0.5) + 1): | |
| while incoming % i == 0: | |
| factors.append(i) | |
| incoming //= i | |
| if incoming > 1: | |
| factors.append(incoming) | |
| factors.sort() | |
| factors[0] *= multiplier | |
| return factors | |
| if __name__ == "__main__": | |
| print(solve(-1)) | |
| print(solve(1)) | |
| print(solve(0)) | |
| print(solve(-190)) | |
| print(solve(-191)) | |
| print(solve(192)) | |
| print(solve(-193)) | |
| print(solve(-194)) | |
| print(solve(195)) | |
| print(solve(196)) | |
| print(solve(197)) | |
| print(solve(198)) | |
| print(solve(199)) | |
| print(solve(3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment