Skip to content

Instantly share code, notes, and snippets.

@bru32
Last active August 13, 2025 07:08
Show Gist options
  • Save bru32/e452bd012e9864b3a7f987217fa937f2 to your computer and use it in GitHub Desktop.
Save bru32/e452bd012e9864b3a7f987217fa937f2 to your computer and use it in GitHub Desktop.
Best way to get all factors of a number.
"""
Get all the factors of a number.
Bruce Wernick
13 August 2025
"""
import math
def get_factors(n):
arr = set()
for i in range(1, int(1 + math.sqrt(n))):
if n % i == 0:
arr.add(i)
arr.add(n // i)
return sorted(arr)
print(get_factors(840))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment