Created
July 28, 2018 06:23
-
-
Save beiweiqiang/852276dd45aa6c3dc0bebf6dc3fa0b31 to your computer and use it in GitHub Desktop.
primer factor
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
# Prime Factorization - | |
# Have the user enter a number and | |
# find all Prime Factors (if there are any) and display them. | |
# primer factors: | |
# table https://en.wikipedia.org/wiki/Table_of_prime_factors | |
def prime_factor(n): | |
if n == 1: | |
return [1] | |
factor = [] | |
def factorization(x): | |
""" | |
将 x 因式分解, | |
:param x: | |
:return: | |
""" | |
i = 2 | |
while x % i != 0: | |
i += 1 | |
if i >= x: | |
return None, x | |
else: | |
return i, int(x / i) | |
rest = n | |
while True: | |
fac, rest = factorization(rest) | |
if fac is not None: | |
factor.append(fac) | |
else: | |
factor.append(rest) | |
break; | |
# 如果返回 list 中含有 1, remove 掉 | |
if 1 in factor: factor.remove(1) | |
return list(set(factor)) | |
def shell(): | |
while True: | |
print(">>> ", end='') | |
entry = input() | |
if entry == "quit": | |
break | |
if not entry.isdigit(): | |
print("You did not enter a number. Try again") | |
else: | |
print(prime_factor(int(entry))) | |
if __name__ == '__main__': | |
shell() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment