-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem3.py
executable file
·55 lines (34 loc) · 984 Bytes
/
problem3.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
#!/usr/bin/python
"""
Project Euler
Problem #3
http://projecteuler.net/problem=3
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
"""
import math
def isPrime(num):
#1 and below, no prime
if num <= 1:
return False
#2 is the only even prime
if num == 2:
return True
#Even number is not prime
if num % 2 == 0:
return False
for i in range(3, int(num**0.5)+1, 2):
if num % i == 0:
return False
return True
largestPrime = 0
max = 600851475143
for i in range(1, int(math.sqrt(max))):
#Is this number prime?
if isPrime(i) and max % i == 0:
print str(i) + " is a prime number factor of " + str(max)
largestPrime = i
else:
print str(i) + " nope. Current largest prime is " + str(largestPrime)
i+=1
print "The largest prime factor of " + str(max) + " is " + str(largestPrime)