-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlargest-prime-factor.py
91 lines (61 loc) · 2.51 KB
/
largest-prime-factor.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import unittest
from math import sqrt
def largest_prime_factor(num):
"""The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143.
finds the largest prime factor of num"""
possible_factors = generator_primes(num)
possible_factors.reverse()
for factor in possible_factors:
if (num % factor) == 0:
return(factor)
return num
def generator_primes(x):
"""generates all prime numbers up to sqrt(x) and one more"""
primes = []
sqrt_x = sqrt(x)
#print(sqrt_x)
bigger_than_sqrt = False
for maybe_prime in range(2, x):
# steps through every number from 2 to our number x
# print("evaluating {}".format(maybe_prime))
is_prime = True
if (maybe_prime % 10000) == 0:
print('at {}'.format(maybe_prime))
if maybe_prime > sqrt_x:
#print("{} is bigger than {}; finding one more prime".format(maybe_prime, sqrt_x))
bigger_than_sqrt = True
for prime in primes:
#steps through every number already in our list of primes
#print("dividing {} by {}".format(maybe_prime, prime))
if (maybe_prime%prime) == 0:
#print("{} is divisible by {}; {} is not prime".format(maybe_prime, prime, maybe_prime))
is_prime = False
break
if is_prime:
primes.append(maybe_prime)
#print("added {} to primes".format(maybe_prime))
if bigger_than_sqrt:
#print("returning b/c bigger than sqrt")
return primes
#print("done evaluating {}".format(maybe_prime))
#print('returning b/c done')
return primes
print(largest_prime_factor(600851475143))
class test_primeGen_largePrime(unittest.TestCase):
"""tests the functions generator_primes() and largest_prime_factor()"""
def test_generator_primes(self):
test_primes = []
check_primes = [ [], [2], [2, 3],[2, 3, 5], [2, 3, 5, 7, 11] ]
test_list = [ 1, 3, 6, 23, 100]
for n in test_list:
test_primes.append(generator_primes(n))
self.assertEqual(check_primes, test_primes)
def test_largest_prime_factor(self):
test_factors = []
check_factors = [ 3, 3, 5, 5]
test_list = [ 3, 6, 20, 100]
for n in test_list:
test_factors.append(largest_prime_factor(n))
self.assertEqual(check_factors, test_factors)
if __name__ == '__main__':
unittest.main()