/
euler135.py
53 lines (48 loc) · 1.11 KB
/
euler135.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
#Project Euler 135: Same differences
from math import sqrt
def poss_candidate(n):
if n % 2 == 0:
c = 2
for i in xrange(3, int(sqrt(n))+1):
if n % i == 0:
c += 1
else:
c = 1
for i in xrange(3, int(sqrt(n))+1, 2):
if n % i == 0:
c += 1
return c >= 5
def first_divisors(n):
if n % 2 == 0:
l = [1, 2]
for i in xrange(3, int(sqrt(n)) + 1):
if n % i == 0:
l.append(i)
else:
l = [1]
for i in xrange(3, int(sqrt(n)) + 1, 2):
if n % i == 0:
l.append(i)
return l
def f(n):
c = 0
for i in first_divisors(n):
a, b = i, n/i
if (a != b):
if (a + b) % 4 == 0:
if 3*b > a:
c += 1
if 3*a > b:
c += 1
else:
if (a + b) % 4 == 0:
c += 1
return c == 10
def main(n):
c = 0
for i in xrange(1, n):
if poss_candidate(i):
if f(i):
c += 1
return c
print main(10**6)