Last active
October 27, 2015 17:41
-
-
Save zacbrac/259b0c5001de224d68e7 to your computer and use it in GitHub Desktop.
Finds the distinct factors of n
This file contains 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
def factor(n): | |
factors = [] | |
for x in range(1,n): | |
if len(factors) > 1 and x == factors[-1][1] or len(factors) > 1 and factors[-1][0] == factors[-1][1]: | |
break | |
elif n % x == 0: | |
if x != (n / x): | |
factors.append([x,(n / x)]) | |
return factors | |
def re_factor(number_to_factor): | |
factors = factor(number_to_factor) | |
factored_factors = [] | |
quads = [] | |
new = set() | |
done = [] | |
for factor_set in factors: | |
temp_list = [] | |
if factor(factor_set[0]): | |
temp_list.append(factor(factor_set[0])) | |
if factor(factor_set[1]): | |
temp_list.append(factor(factor_set[1])) | |
factored_factors.append(temp_list) | |
for result in factored_factors: | |
if len(result) > 1: | |
for pair in result[0]: | |
for secondary_pair in result[1]: | |
quads.append([pair[0], pair[1], secondary_pair[0], secondary_pair[1]]) | |
for quad in quads: | |
if len(set(quad)) >= 4: | |
new.add(frozenset(quad)) | |
for item in new: | |
done.append(sorted(list(item))) | |
return done | |
print re_factor(144) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment