-
Notifications
You must be signed in to change notification settings - Fork 0
/
p173.py
58 lines (53 loc) · 1.62 KB
/
p173.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
#!/usr/bin/pypy
from math import sqrt, floor
def ntiles(n):
return (4*n -4)
def main(limit):
N = int((limit + 4) / 4.0)
outer = {}
for n in xrange(3, N+1):
outer[n] = 4*n -4 # ntiles
cont = 0
vals = outer.values()
fvals1 = [vals[i] for i in xrange(0,len(vals),2)]
fvals2 = [vals[i] for i in xrange(1,len(vals),2)]
for n in range(3,5):
if n % 2 == 1:
fvals = fvals1
else:
fvals = fvals2
for pos1 in xrange(len(fvals)+1):
for pos2 in xrange(pos1, len(fvals)+1):
buf = [fvals[i] for i in xrange(pos1, pos2)]
s = sum(fvals[pos1:pos2])
print n, pos1, pos2, buf
if s and s <= limit:
cont +=1
print cont
def main2(limit):
N = int((limit + 4) / 4.0)
outer = {}
for n in xrange(3, N+1):
outer[n] = 4*n -4 # ntiles
cont = 0
vals = outer.values()
fvals1 = [vals[i] for i in xrange(0,len(vals),2)]
fvals2 = [vals[i] for i in xrange(1,len(vals),2)]
for n in range(3,5):
if n % 2 == 1:
fvals = fvals1
else:
fvals = fvals2
for pos1 in xrange(len(fvals)+1):
for pos2 in xrange(pos1, len(fvals)+1):
buf = [fvals[i] for i in xrange(pos1, pos2)]
s = sum(fvals[pos1:pos2])
#print n, pos1, pos2, buf, s > limit
if s and s <= limit:
cont +=1
else:
if s > limit:
break
print cont
if __name__ == "__main__":
main2(1000000)