-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem3.py
More file actions
63 lines (50 loc) · 1.46 KB
/
problem3.py
File metadata and controls
63 lines (50 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Problem 3 from Project Euler
import sympy as sym
import numpy as np
from prime_seive import *
a = 600851475143 # Number we want to find largest prime factor of
def largest_prime_factor(X):
top = X//2
succ = False
i = 0
while not succ:
y = top - i
if X % y == 0:
succ = sym.primetest.isprime(y)
i += 1
return y
# print('Answer: {}'.format(largest_prime_factor(a)))
def lpf(X):
'''
Function to find the largest prime factor
'''
# Will end up duplicating some work, but what the heck.
# First, make sure the number itself isn't prime
top = int(X**2) # Will have a factor no larger than square root
if is_prime(X):
return X
else:
# It will be easiest to get the smaller factors first, I think...
p = prime_gen() # Pops out prime numbers in order
largest = 1
for pr in p:
if pr > top:
break
while X%pr == 0:
X = X//pr # May as well make the problem smaller
return largest
def rec_try(X):
top = int(X**.5)
if is_prime(X):
return X
else:
p = prime_gen()
# Find a prime divisor
i = next(p)
while X%i != 0:
i = next(p)
# Get through that when a prime divisor is found
# The largest prime divisor of this number is the largest of the
# original!
return rec_try(X//i)
print("Answer: {}".format(rec_try(a)))