Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- from re import sub as sub_from_string
- def is_prime(num_to_test: int):
- for i in range(2, int(math.sqrt(num_to_test) + 2)):
- if num_to_test % i == 0:
- return False
- return True
- def next_prime(last_prime: int):
- next_test = last_prime + 1
- while True:
- if is_prime(next_test):
- return next_test
- else:
- next_test += 1
- num_to_fac = input('Num to factorize: \n')
- num_to_fac = int(sub_from_string('[^0-9]', '', num_to_fac))
- test_prime = 2
- prime_factors = []
- output_num = num_to_fac
- while True:
- mod = divmod(num_to_fac, test_prime)
- if mod[1] == 0:
- prime_factors.append(test_prime)
- num_to_fac = mod[0]
- else:
- test_prime = next_prime(test_prime)
- if is_prime(num_to_fac):
- prime_factors.append(int(num_to_fac))
- break
- print(output_num, 'has the following prime factors:')
- print(prime_factors)
- exit('DONE')
Add Comment
Please, Sign In to add comment