-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem 3.py
More file actions
executable file
·44 lines (35 loc) · 1.19 KB
/
Problem 3.py
File metadata and controls
executable file
·44 lines (35 loc) · 1.19 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
#!/usr/bin/env python
__author__ = 'Ned Udomkesmalee'
#What is the largest prime factor of the number 600851475143?
def primes_up_to(n):
#Sieve of Eratosthenes Implementation to create Prime Numbers up to n
if n <= 2:
return []
sieve = [True] * (n+1)
for x in range(3, int(n ** 0.5) + 1, 2):
for y in range(3, (n // x) + 1, 2):
sieve[(x * y)] = False
return [2]+[i for i in range(3, n, 2) if sieve[i]]
def problem3():
print("Find the largest prime factor of n")
while True:
n = raw_input("What is n? ")
try:
n = int(n)
if n > 0:
primes = primes_up_to(int(n ** .5) + 1)
primes.reverse()
found_flag = False
for prime in primes:
if n % prime == 0:
print("Answer = %i" % prime)
found_flag = True
break
if not found_flag:
"Answer = 1"
break
else:
print "*** n must be positive. ***"
except ValueError:
print "*** %s is not a valid integer. ***" % n
problem3()