-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_003_00.py
More file actions
79 lines (60 loc) · 2.19 KB
/
problem_003_00.py
File metadata and controls
79 lines (60 loc) · 2.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
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
##Largest prime factor
##Problem 3
##The prime factors of 13195 are 5, 7, 13 and 29.
##
##What is the largest prime factor of the number 600851475143 ?
import numpy as np
import timeit
def primesfrom2to(n):
""" Input n>=6, Returns a array of primes, 2 <= p < n """
sieve = numpy.ones(n/3 + (n%6==2), dtype=numpy.bool)
for i in range(1,int((n**0.5)/3+1)):
if sieve[i]:
k=3*i+1|1
sieve[ k*k/3 ::2*k] = False
sieve[k*(k-2*(i&1)+4)/3::2*k] = False
return numpy.r_[2,3,((3*numpy.nonzero(sieve)[0][1:]+1)|1)]
def calculatedivisors(maxlimit):
divisors = []
npdivisors = set()
## npdivisors.add(2)
k = 0
while k == 0:
for i in range(2, int(maxlimit/2)):
if maxlimit % i == 0:
npd = np.ones(len(npdivisors), dtype=bool)
m = 0
try:
for j in npdivisors:
if i % j == 0:
npd[m] = False
break
m += 1
if npd.all():
npdivisors.add(i)
## print("Appending ",i)
product = 1
for element in npdivisors:
## print(element)
product = element * product
## print(product)
if product == maxlimit:
k = 1
## print("got it")
return npdivisors
except:
npdivisors.add(i)
print(npdivisors)
## print(divisors)
return npdivisors
def main():
listdivisors = calculatedivisors(int(600851475143
))
print(max(listdivisors))
##listprime = calculateprime(int(600851475143))
## print(max(listprime))
print("timing...")
t = timeit.Timer("calculatedivisors(int(600851475143))","from __main__ import calculatedivisors")
print(t.timeit())
print("job done")
main()