Skip to content

Instantly share code, notes, and snippets.

@horvatha
Last active May 19, 2017 09:08
Show Gist options
  • Save horvatha/63337246127f5368a1e12405e132dbf9 to your computer and use it in GitHub Desktop.
Save horvatha/63337246127f5368a1e12405e132dbf9 to your computer and use it in GitHub Desktop.
import math
def prime_factorize(n):
factors = []
number = math.fabs(n)
while number > 1:
factor = get_next_prime_factor(number)
factors.append(factor)
number /= factor
if n < -1: # If we'd check for < 0, -1 would give us trouble
factors[0] = -factors[0]
return tuple(factors)
def get_next_prime_factor(n):
if n % 2 == 0:
return 2
# Not 'good' [also] checking non-prime numbers I guess?
# But the alternative, creating a list of prime numbers,
# wouldn't it be more demanding? Process of creating it.
for x in range(3, int(math.ceil(math.sqrt(n)) + 1), 2):
if n % x == 0:
return x
return int(n)
szamok = [645, 711, 417, 197, 208, 527, 910, 481, 574, 528, 256, 768, 625, 686, 759, 714, 192]
print(len(szamok))
for szam in szamok:
print(szam, *prime_factorize(szam))
print(sum(szamok))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment