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-09-14 08:40:04 +00:00
|
|
|
def is_prime(number: int) -> bool:
|
|
|
|
"""Checks to see if a number is a prime in O(sqrt(n)).
|
|
|
|
A number is prime if it has exactly two factors: 1 and itself.
|
|
|
|
Returns boolean representing primality of given number (i.e., if the
|
|
|
|
result is true, then the number is indeed prime else it is not).
|
2020-10-25 03:23:16 +00:00
|
|
|
|
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)
|
2022-09-14 08:40:04 +00:00
|
|
|
False
|
2022-04-08 17:40:45 +00:00
|
|
|
>>> is_prime(1)
|
2022-09-14 08:40:04 +00:00
|
|
|
False
|
2020-10-06 14:54:39 +00:00
|
|
|
"""
|
2020-10-25 03:23:16 +00:00
|
|
|
|
2022-09-14 08:40:04 +00:00
|
|
|
if 1 < number < 4:
|
|
|
|
# 2 and 3 are primes
|
2018-10-19 12:48:28 +00:00
|
|
|
return True
|
2022-09-14 08:40:04 +00:00
|
|
|
elif number < 2 or number % 2 == 0 or number % 3 == 0:
|
|
|
|
# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
|
2018-10-19 12:48:28 +00:00
|
|
|
return False
|
2022-09-14 08:40:04 +00:00
|
|
|
|
|
|
|
# All primes number are in format of 6k +/- 1
|
|
|
|
for i in range(5, int(math.sqrt(number) + 1), 6):
|
|
|
|
if number % i == 0 or number % (i + 2) == 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() = }")
|