Created
January 18, 2021 09:55
-
-
Save Colk-tech/2970385e20fd22b27b12e7a0d370a519 to your computer and use it in GitHub Desktop.
学校の情報理論の副産物です。整数をできるだけ大きい2つの素数に分解します。
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 Tuple | |
from sympy import sieve | |
def prime_factorization(n: int) -> Tuple[int, int]: | |
if n < 0: | |
raise RuntimeError("Invalid number has passed. Please specify number in the range: 1~") | |
first_factor = find_greatest_factor(n) | |
second_factor = find_greatest_factor(int(n / first_factor)) | |
if first_factor * second_factor != n: | |
raise RuntimeError(f"The number: {n} can not to be divided in two prime numbers.") | |
return first_factor, second_factor | |
def find_greatest_factor(n: int) -> int: | |
is_found = False | |
for i in reversed(range(2, n)): | |
if n % i == 0 and i in sieve: | |
is_found = True | |
break | |
if is_found: | |
return i | |
else: | |
return n | |
if __name__ == "__main__": | |
print("Number?") | |
user_input = int(input()) | |
print(prime_factorization(user_input)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment