This repository was archived by the owner on Apr 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblemThree.py
More file actions
67 lines (56 loc) · 1.5 KB
/
problemThree.py
File metadata and controls
67 lines (56 loc) · 1.5 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
59
60
61
62
63
64
65
66
67
# The prime factors of 13195 are 5, 7, 13 and 29.
# What is the largest prime factor of the number 600851475143 ?
# result 6857
# [Finished in 0.083s]
def isPrime(checkNumber):
if checkNumber <= 1: return False
if checkNumber == 2: return True
if checkNumber == 3: return True
if checkNumber == 5: return True
switcher = [ 1, 7, 11, 13, 17, 19, 23, 29 ]
if checkNumber % 30 not in switcher: return False
a = 7
skipCase = 1
while(a*a <= checkNumber):
number = checkNumber % a
if number == 0: return False
if skipCase == 1:
a += 4
skipCase = 2
elif skipCase == 2:
a += 2
skipCase = 3
elif skipCase == 3:
a += 4
skipCase = 4
elif skipCase == 4:
a += 2
skipCase = 5
elif skipCase == 5:
a += 4
skipCase = 6
elif skipCase == 6:
a += 6
skipCase = 7
elif skipCase == 7:
a += 2
skipCase = 8
elif skipCase == 8:
a += 6
skipCase = 1
return True
def largestPrime(number):
if isPrime(number): return number
limit = number
a = 2
while(a < limit):
if limit % a == 0:
limit /= a
if isPrime(limit):
maxFactor = limit
break
if isPrime(a):
maxFactor = a
a += 1
return maxFactor
print largestPrime(600851475143)