Untitled

Apr 29th, 2012
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. primes = []
2.
3. def getfac(n):
4.     if n == 1:
5.         return []
6.     for p in primes:
7.         if n % p == 0:
8.             return [p] + getfac(n / p)
9.     else:
10.         primes.append(n)
11.         return [n]
12.
13. faclist = [[], []] + [getfac(n) for n in range(2, 101)]
14.
15. palette = ['224186', '941520', 'df9c19', '502364', 'c02b5e', '483131', 'd03a20',
16.            '065371', '111113', '641821', '177c6f', '8c2261', 'b21b23', 'd1b341',
17.            '7f65a4', '385190', '63771c', 'bb506b', '3a9fbb', '836f6f', '789b65',
18.            '7ba3c2', '8482a6', 'bd96a1', 'b1a676']
19.
20. cmap = { prime: color for prime, color in zip(primes, palette) }
21.
22. pitch = 5
23. # 1: 17
24. # 2: 8 + 1 + 8
25. # 3: 5 + 1 + 5 + 1 + 5
26. div1, div2, div3, sep = pitch * 17, pitch * 8, pitch * 5, pitch * 2
27. unit = '<td width={div1} height={div1}'.format(div1=div1)
28. row = lambda height, *tds: '<tr>' + ''.join('<td {} height={} />'.format(td, height) for td in tds) + '</tr>'
29. col2 = lambda c0, c1: ('bgcolor="#{c[%d]}" width={div2}' % c0, 'width={pitch}', 'bgcolor="#{c[%d]}" width={div2}' % c1)
30. subtable = lambda *items: '{unit}><table border=0 cellspacing=0 cellpadding=0>' + ''.join(items) + '</table></td>'
31. squares = [
32.     # blank
33.     '{unit} />',
34.     # 1x1
35.     '{unit} bgcolor="#{c[0]}" />',
36.     # 1x2
37.     subtable(row(div1, *col2(0, 1))),
38.     # 3x1
39.     subtable(row(div3, 'bgcolor="#{c[0]}" width={div1}'),
40.              row(pitch, 'width={div1}'),
41.              row(div3, 'bgcolor="#{c[1]}" width={div1}'),
42.              row(pitch, 'width={div1}'),
43.              row(div3, 'bgcolor="#{c[2]}" width={div1}')),
44.     # 2x2
45.     subtable(row(div2, *col2(0, 1)),
46.              row(pitch, 'width={div1} colspan=3'),
47.              row(div2, *col2(2, 3))),
48.     # 2, 1, 2
49.     subtable(row(div3, 'bgcolor="#{c[0]}" width={div2} colspan=2', 'width={pitch}', 'bgcolor="#{c[1]}" width={div2} colspan=2'),
50.              row(pitch, 'width={div1} colspan=5'),
51.              row(div3, '', 'bgcolor="#{c[2]}" width={div2} colspan=3', ''),
52.              row(pitch, 'width={div1} colspan=5'),
53.              row(div3, 'bgcolor="#{c[3]}" width={div2} colspan=2', 'width={pitch}', 'bgcolor="#{c[4]}" width={div2} colspan=2')),
54.     # 3x2
55.     subtable(row(div3, *col2(0, 1)),
56.              row(pitch, 'width={div1} colspan=3'),
57.              row(div3, *col2(2, 3)),
58.              row(pitch, 'width={div1} colspan=3'),
59.              row(div3, *col2(4, 5)))  ]
60.
61. with open('20120429.html', 'wb') as f:
62.     print >>f, '<html><body bgcolor="#ccc8c7"><table border=0 cellspacing={sep} cellpadding=0>'.format(**globals())
63.
64.     for row in range(91, 0, -10):
65.         print >>f, '<tr>'
66.         for n in range(row, row + 10):
67.             colors = [cmap[fac] for fac in faclist[n]]
68.             print >>f, squares[len(colors)].format(c=colors, **globals())
69.         print >>f, '</tr>'
70.     print >>f, '</table></body></html>'