2019-07-16 23:09:53 +00:00
|
|
|
"""
|
2020-10-25 03:23:16 +00:00
|
|
|
Project Euler Problem 3: https://projecteuler.net/problem=3
|
2019-07-16 23:09:53 +00:00
|
|
|
|
2020-10-25 03:23:16 +00:00
|
|
|
Largest prime factor
|
|
|
|
|
|
|
|
The prime factors of 13195 are 5, 7, 13 and 29.
|
|
|
|
|
|
|
|
What is the largest prime factor of the number 600851475143?
|
2020-10-06 14:54:39 +00:00
|
|
|
|
2020-10-25 03:23:16 +00:00
|
|
|
References:
|
|
|
|
- https://en.wikipedia.org/wiki/Prime_number#Unique_factorization
|
|
|
|
"""
|
2018-10-19 12:48:28 +00:00
|
|
|
import math
|
|
|
|
|
2019-07-16 23:09:53 +00:00
|
|
|
|
2022-04-08 17:40:45 +00:00
|
|
|
def is_prime(num: int) -> bool:
|
2020-10-25 03:23:16 +00:00
|
|
|
"""
|
|
|
|
Returns boolean representing primality of given number num.
|
|
|
|
|
2022-04-08 17:40:45 +00:00
|
|
|
>>> is_prime(2)
|
2020-10-06 14:54:39 +00:00
|
|
|
True
|
2022-04-08 17:40:45 +00:00
|
|
|
>>> is_prime(3)
|
2020-10-06 14:54:39 +00:00
|
|
|
True
|
2022-04-08 17:40:45 +00:00
|
|
|
>>> is_prime(27)
|
2020-10-06 14:54:39 +00:00
|
|
|
False
|
2022-04-08 17:40:45 +00:00
|
|
|
>>> is_prime(2999)
|
2020-10-06 14:54:39 +00:00
|
|
|
True
|
2022-04-08 17:40:45 +00:00
|
|
|
>>> is_prime(0)
|
2020-10-06 14:54:39 +00:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2020-10-25 03:23:16 +00:00
|
|
|
ValueError: Parameter num must be greater than or equal to two.
|
2022-04-08 17:40:45 +00:00
|
|
|
>>> is_prime(1)
|
2020-10-06 14:54:39 +00:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2020-10-25 03:23:16 +00:00
|
|
|
ValueError: Parameter num must be greater than or equal to two.
|
2020-10-06 14:54:39 +00:00
|
|
|
"""
|
2020-10-25 03:23:16 +00:00
|
|
|
|
2020-10-06 14:54:39 +00:00
|
|
|
if num <= 1:
|
2020-10-25 03:23:16 +00:00
|
|
|
raise ValueError("Parameter num must be greater than or equal to two.")
|
2020-10-06 14:54:39 +00:00
|
|
|
if num == 2:
|
2018-10-19 12:48:28 +00:00
|
|
|
return True
|
2020-10-06 14:54:39 +00:00
|
|
|
elif num % 2 == 0:
|
2018-10-19 12:48:28 +00:00
|
|
|
return False
|
2020-10-06 14:54:39 +00:00
|
|
|
for i in range(3, int(math.sqrt(num)) + 1, 2):
|
|
|
|
if num % i == 0:
|
2018-10-19 12:48:28 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2019-07-16 23:09:53 +00:00
|
|
|
|
2020-10-06 14:54:39 +00:00
|
|
|
def solution(n: int = 600851475143) -> int:
|
2020-10-25 03:23:16 +00:00
|
|
|
"""
|
|
|
|
Returns the largest prime factor of a given number n.
|
|
|
|
|
2019-07-16 23:09:53 +00:00
|
|
|
>>> solution(13195)
|
|
|
|
29
|
|
|
|
>>> solution(10)
|
|
|
|
5
|
|
|
|
>>> solution(17)
|
|
|
|
17
|
2019-07-18 17:05:14 +00:00
|
|
|
>>> solution(3.4)
|
|
|
|
3
|
|
|
|
>>> solution(0)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2020-10-25 03:23:16 +00:00
|
|
|
ValueError: Parameter n must be greater than or equal to one.
|
2019-07-18 17:05:14 +00:00
|
|
|
>>> solution(-17)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2020-10-25 03:23:16 +00:00
|
|
|
ValueError: Parameter n must be greater than or equal to one.
|
2019-07-18 17:05:14 +00:00
|
|
|
>>> solution([])
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2020-10-25 03:23:16 +00:00
|
|
|
TypeError: Parameter n must be int or castable to int.
|
2019-07-18 17:05:14 +00:00
|
|
|
>>> solution("asd")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2020-10-25 03:23:16 +00:00
|
|
|
TypeError: Parameter n must be int or castable to int.
|
2019-07-16 23:09:53 +00:00
|
|
|
"""
|
2020-10-25 03:23:16 +00:00
|
|
|
|
2019-07-18 17:05:14 +00:00
|
|
|
try:
|
|
|
|
n = int(n)
|
2020-05-22 06:10:11 +00:00
|
|
|
except (TypeError, ValueError):
|
2020-10-25 03:23:16 +00:00
|
|
|
raise TypeError("Parameter n must be int or castable to int.")
|
2019-07-18 17:05:14 +00:00
|
|
|
if n <= 0:
|
2020-10-25 03:23:16 +00:00
|
|
|
raise ValueError("Parameter n must be greater than or equal to one.")
|
2020-10-06 14:54:39 +00:00
|
|
|
max_number = 0
|
2022-04-08 17:40:45 +00:00
|
|
|
if is_prime(n):
|
2020-10-06 14:54:39 +00:00
|
|
|
return n
|
|
|
|
while n % 2 == 0:
|
|
|
|
n //= 2
|
2022-04-08 17:40:45 +00:00
|
|
|
if is_prime(n):
|
2019-07-16 23:09:53 +00:00
|
|
|
return n
|
2020-10-06 14:54:39 +00:00
|
|
|
for i in range(3, int(math.sqrt(n)) + 1, 2):
|
|
|
|
if n % i == 0:
|
2022-04-08 17:40:45 +00:00
|
|
|
if is_prime(n // i):
|
2021-10-11 16:33:44 +00:00
|
|
|
max_number = n // i
|
2020-10-06 14:54:39 +00:00
|
|
|
break
|
2022-04-08 17:40:45 +00:00
|
|
|
elif is_prime(i):
|
2020-10-06 14:54:39 +00:00
|
|
|
max_number = i
|
|
|
|
return max_number
|
2019-07-16 23:09:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-10-25 03:23:16 +00:00
|
|
|
print(f"{solution() = }")
|