-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp012.py
63 lines (53 loc) · 1.54 KB
/
p012.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
import math
def is_square(n):
return math.sqrt(n).is_integer()
def eratosthenes_sieve(n):
# Create a candidate list within which non-primes will be
# marked as None; only candidates below sqrt(n) need be checked.
candidates = list(range(n+1))
fin = int(n**0.5)
# Loop over the candidates, marking out each multiple.
for i in xrange(2, fin+1):
if candidates[i]:
candidates[2*i::i] = [None] * (n//i - 1)
# Filter out non-primes and return the list.
return [i for i in candidates[2:] if i]
def primeFactors1(num):
potential = eratosthenes_sieve(num)
factors = list()
for x in potential:
if((num%x) == 0):
factors.append(x)
return factors
def primeFactors2(num):
if(num%2 == 0):
return 2
a = int(math.ceil(math.sqrt(num)))
b2 = int(a*a - num)
while (is_square(b2) == False):
a += 1
b2 = a*a - num
#result = a-math.sqrt(b2)
return int(a-math.sqrt(b2))
def numDivisors(num, factorFunction):
factors = factorFunction(num)#primeFactors(num)
curNum = num
numDiv = 1
for x in factors:
power = 0
while((curNum%x) == 0):
power += 1
curNum /= x
numDiv *= (power+1)
return numDiv
count = 0
cur = 0
largest = 0
while (largest < 500):
count += 1
cur += count
result = numDivisors(cur, primeFactors1)
if(result > largest):
largest = result
print largest
print 'Answer: ' + str(largest) + ' factors for number ' + str(cur)