Clingo with Python, (削除) 1600 (削除ここまで) 1689(削除) 1689 (削除ここまで) 1740 primes
Specifically, I fix the positions of the digits in a pattern designed for close packing of 4 and 5 digit numbers, with occasional 2 and 3 digit numbers on the boundary, and add constraints saying that each number is a distinct prime. The current packing uses a little more than halflarge fraction of all 4 digit primes (557854 of 1061) and of all 5 digit primes starting with 1 or 2 (1047 of 2016).
#script (python)
import re
def primesto(n):
p = [False]*2 + [True]*(n - 2)
for k in xrange(n):
if p[k]:
yield k
p[k*k::k] = [False]*(-(-n//k) - k)
def main(prg):
width = prg.get_const('width')
height = prg.get_const('height')
primes = [plist(primesto(30000))
counts = {4: 4, 5: 4}
def get_bands(counts):
if counts:
for k, v in counts.iteritems():
counts1 = counts.copy()
if v == 1:
del counts1[k]
else:
counts1[k] = v - 1
for p in primestoget_bands(30000counts1)]:
yield (k,) + p
else:
yield ()
def get_img(xbands, yoffset):
rtile = ''.join(x'#'*k + y' ' for k in bands)%17
tile = tile[offset:] + tile[:offset]
def get_edge(it, corner):
if not next(it): return \True
if not next(it): return corner
if not next(xit): ==return 0True
or y == 0 if not next(it): andreturn rTrue
in if not next(0,it): 1,return 5,True
6, 7, 9, 10, 11, 12 return False
def get_cell(x, 16)y):
or return \
((x == 0 and get_edge((get_cell(i, y) for i in xrange(1, width - 1)), ornot 1 <= y ==< height - 1)) or \
(y == 0 and rget_edge((get_cell(x, j) for j in xrange(1, 2,height 3,- 71)), 8,not 9,1 13,<= 14,x 15,< 16width - 1)) or \
(0 < (x <== width - 1 and get_edge((get_cell(i, y) for i in xrange(width - 2, 0, <-1)), not 1 <= y < height - 1)) andor r\
in (0,y == height - 1 and get_edge((get_cell(x, j) for j in xrange(height - 2, 30, 5-1)), 6,not 7,1 8,<= 9,x 11,< 12,width 13,- 14,1)) 16or \
(1 <= x < width - 1 and 1 <= y < height - 1 and tile[(x + y)%len(tile)] == '#')
img = [[get_img[[get_cell(x, y) for x in xrange(width)] for y in xrange(height)]
def get_spaces():
for y, row in enumerate(img):
for m in re.finditer('#{2,}', ''.join(' #'[cell] for cell in row)):
yield [(x, y) for x in range(m.start(), m.end())]
for x, col in enumerate(zip(*img)):
for m in re.finditer('#{2,}', ''.join(' #'[cell] for cell in col)):
yield [(x, y) for y in range(m.start(), m.end())]
spaces = list(get_spaces())
return img, spaces
img, spaces = max((get_img(p, o) for p in get_bands(counts) for o in xrange(p[0] + 1)), key=lambda (img, spaces): len(spaces))
def data():
for p in primes:
yield 'prime({}, {}).\n'.format(p, len(str(p)))
for j, d in enumerate(str(p)):
yield 'primedigit({}, {}, {}).\n'.format(p, j, int(d))
for i, space in enumerate(spaces):
yield 'space({}, {}).\n'.format(i, len(space))
for j, c in enumerate(space):
yield 'cell({}, {}, {}).\n'.format(i, j, c)
def on_model(model):
digit = {}
for atom in model.atoms():
if atom.name() == 'digit':
c, d = atom.args()
digit[c] = d
for y in xrange(height):
print ''.join(str(digit.get((x, y), ' ')) for x in xrange(width))
prg.add('data', [], ''.join(data()))
prg.ground([('base', []), ('data', [])])
prg.solve_async(on_model=on_model).wait()
#end.
#program base.
1 {loc(P, S) : prime(P, L)} 1 :- space(S, L).
:- prime(P, L), 2 {loc(P, S) : space(S, L)}.
digit(C, D) :- loc(P, S), cell(S, J, C), primedigit(P, J, D).
:- cell(S, J, C), 2 {digit(C, D) : D=0..9}.
#show digit/2.
Warning: at ×ばつ60, this takes about 78 minutes and uses 3 GB of memory. You may want to start with smaller sizes if you just want to see it running.
Output (×ばつ60, 16891740 primes):
53 223 9127 13 281 293 4153 2574217 72439137 163239 3169 239 6133223 151127 271281 51479281 9173 4253 229 257 1579239 4193
953 24733719 165728349 115972039 220637159 12834603 2498917491 198436299 694717207 1058919463 1560719793 59033301 212214483 188695657 620639 10061
97 2061126417 38517307 275292687 106019479 158314767 286493571 2992710957 652921341 1126114057 159075813 23516689 127392383 2270919553 4492647 3
1562915679 59396679 295676217 269591303 656919037 267295821 2892111551 352918797 2185928723 252533457 94216581 248595309 1078112043 23834441 2
1013314327 79494783 199195881 281816131 805321107 220272549 2887112953 355924001 2066312113 236717247 10699973 164518287 1356716573 183715519 149
1436920177 40274967 111195647 222831907 622924439 251897237 2023328979 153124781 2035326083 180479859 54076691 100093701 2219325237 20399041 11637
3203 22372953 154676781 286872851 94797079 2957310139 213774001 754920981 1182720341 1045323531 80871913 118975081 153837639 616325169 17457 29437
193743 63593907 104876949 116991237 334310937 151213203 2484719213 367723971 1450318367 113995227 47835879 109498191 1572717827 60114561 13716927
39 55191613 152998369 298673911 764923327 159231571 2815125741 558115889 1372919417 229933083 30673049 250374421 2266925111 90438147 104910391
49 40519151 294233019 162293499 413911839 241335851 1813123669 475116823 1937314891 287599829 32519403 272991069 1027326099 38638737 2139721911 2
2968317389 123433217 204417963 547713337 169935779 1200719841 130716007 1808910313 277937673 76077703 226395897 2192923911 17774409 1013926737 131
4801 124732711 188399697 26478563 2435929611 122392699 676120231 2148127631 2008915823 73497127 256937937 280878171 363123623 229736067 14489 1307
353 12569751 166612179 23577877 1045712263 235617331 280317923 1785126153 1127911443 41337603 152711429 246718861 259328871 225678291 120599 20129
73 136493251 129115087 148728949 135374519 2101922501 472915667 2523728837 280972543 85813617 211692837 2556115217 58394129 1432320357 27729879
2 7507 175514127 1723927479 30492521 1039111423 1530717497 990712409 114716599 179577297 12171249 2418114813 151316301 295310501 1396724151 2503
21487 183131777 1806120143 78177109 2981912239 1631922063 400723761 150319931 297591579 86698929 1066326641 158233221 318718251 2227929429 253431
165533511 136932389 980327103 113839767 2672327763 154323399 2222918481 190095869 71038431 119712141 2014715259 16376367 1141129131 2673117789 137
4349 19793941 17836719 1508329269 180411811 696112829 1388314929 2743111953 62774817 273379787 133376469 459121377 205072473 2821917033 11311 1453
941 232513 82434549 1891111491 137512777 409111351 1052928879 2698121061 74113607 222595441 182575861 939715493 206818941 2344715101 614437 28579
3 93229 2620918719 65997841 1251118701 2010723087 927723561 217398597 161836637 10637487 2875119681 142076229 792725321 1078916417 2647914717 373
15131 13151 148976997 289727947 1622326669 1720324179 87195981 102114297 134695683 541918979 293474937 1921116787 814724421 2958720089 13417 6619
2531 2199713127 56695507 1318310973 2887913121 208125759 159918353 251219413 68836343 2574117713 123477211 759121523 2705912143 1578710477 265911
28753107 245919381 280196871 1261118803 103120117 1702920849 256739091 33894051 221892333 2209311399 19335987 1065121617 1535913147 296912281 9251
2213 75473 2149121713 125774027 388915299 2671115031 2641714389 62117789 136192617 225714993 541718493 244811103 1999327983 330122739 126987 2339
709 2441 1675922853 277491297 736915959 1098719531 1606912799 11512269 180974943 122533917 629910093 268394657 2425125999 854316603 17311617 12919
114923 85391021 1975115313 2632111287 172114969 236291009 156492503 14999907 1003925247 279434523 358316411 1014112979 2423922397 37278819 19497
818191 86935039 1783721019 2682113619 619929947 237412087 259316793 54373529 1975929671 262272861 502122259 1835320921 2155717291 75376703 254474
257711523 256674919 1194121379 169323071 1740120051 223039133 75298761 175396029 1369111813 91817517 1739314503 2785128927 127717707 240298311 701
3847 23227311 277992939 670328183 2645912347 2637110357 44096143 204435437 299896011 505913789 117797573 1477123629 990121773 1957128703 5059 8609
347 200237 215034799 334717333 1424928069 2134717921 50779803 207438543 224819601 220328151 145499059 2166110457 387712119 1219715661 23593 21937
1 37759 1851713399 2706119207 771721407 141593433 181997187 77275717 2707322571 133317949 627119489 2855912251 1921310271 96198363 238732879 2693
13883 10663 1894720023 1496912583 45671193 164535011 182291889 943120147 161414723 2128325447 409915887 1914129921 140514679 19077459 286434 2069
2887 1814914341 2181714759 978720593 289012267 103211567 34579901 2277714411 240711229 108712041 1051320483 1210710889 81676163 101813517 29297947
25999983 1765924799 415913099 2748113763 121094733 19019029 108895281 2806917231 10196947 1438929311 2117922709 450711657 230872113 163698167 2081
6971 119811 920924359 2383129399 2860711117 81232111 179239341 144499187 745925931 250317433 2305320347 982916481 2591317293 291915021 8681 17419
313 13241 316321323 1070914737 2947318311 54831559 151011279 197632273 955120509 216177283 2752721739 721128549 1641122193 283098011 29887 2293 3
921011 1873119231 300116127 222736197 171915107 63617151 2476321013 161932551 202922943 1818128057 2409713229 25579277 118137229 260171721 479
1
19927 1291922549 574920641 282838053 229436529 92275737 2869729021 185531483 229323027 1193324977 1871917623 56473347 150616791 201291213 6113157
8573 1441123447 286124499 118074273 292878963 20636257 2062721881 285173343 517127329 1973917627 1638128307 70012393 136814493 152173613 212111117
19709167 479915991 2765317029 201732803 58136673 221593797 1772928219 26634621 1907322277 1840112071 953913043 183676661 115196323 73078297 919433
7219 15677 1154924517 1417711777 12131609 220679941 122112437 808126189 123778831 2712710771 187911467 2923124091 274792371 28013331 27607 14947
967 6247 1209722727 2920913967 71091471 174317451 126131367 301911113 161897477 2332111527 745724007 2522923557 133971277 27294969 1276829 28097
712073 288711887 106393181 244218999 87795641 2070713411 258417321 571722901 1660728001 1015120939 30793889 269535231 191818863 971926357 14333
420011 226923671 291015669 199631291 58519421 1115912689 217274903 831121647 2203119483 2755112157 44514861 252195381 144479067 494311471 28099283
230632663 2859122123 285132833 82971663 296118677 1453319891 51898039 1346319141 1132923321 166717417 257992027 121439949 88216389 1312123459 7351
7487 19753967 2333915901 23331831 298811013 183295527 979112697 103135573 2758116547 685714029 1765723333 220913623 83699007 178396047 12739 18539
307 170997 1564714369 29179719 294436199 108317499 748120663 239933413 1451923339 784125097 1073912973 203479791 92819643 227832459 218353 9371 1
7 1316316843 259818329 88671451 166515279 2859713441 9433 2273927647 2290728643 702712007 290334463 195016269 11877523 1399722861 2836899 1
18143 168835743 190372591 53334211 2234327551 191399461 349927689 2149911279 2401914533 85132731 231673001 153316857 639713757 199613259 2473277
22483 149393943 203599011 69778933 1393118593 230416569 967926891 2474922381 2574722109 82094721 127811733 236332377 832911549 143275387 113171459
213416203 224835351 14279851 255796133 1211329573 15234513 2763128517 1289914083 552715773 184133109 140099967 46575113 2723929063 118211427 17257
9883911 190698713 63738783 274099613 1665727253 63433851 2599725537 2137916993 550117683 277393037 122414007 42835651 2728312487 181696779 26251
367 277331 20037243 241093023 230214751 976710459 186614931 2420325639 252125867 2620312101 149296373 82314201 198018123 1577315227 37589 10601
1 79631 194691061 51011549 2380110159 137811381 243718047 1455126687 2761720411 19312129 130012011 298791583 582112899 236099749 2754126339 883
1
25127 239113389 12374013 1928915647 199271669 396725933 2514724443 1043321557 91517853 106273449 214936089 766928771 221291847 1385921491 7121263
8501 125472309 16972797 1760916189 258012003 247722637 1674715973 2002128697 16272741 222712729 240011361 195125667 291292131 1456123633 296332297
19577359 52279551 259696553 2368911597 82916451 1669920123 1006710259 913329287 127575923 189138233 70691423 1644710133 206931373 790123773 711633
4937 9391 16139 24379 50117 179815119 253037829 201124019 133997691 2054312479 539314143 1972720873 109935323 53871493 93709
81118947 57915701 2038912413 1317127997 7951
4 164773697 111975261 346117359 108911657 1537326371 969720443 1981911239 205094093 22077013 503
135303 15767 4259 197313499 93713597 5
31 3919 3117393 199 3917 7199397 379 113 971 1733 317193 1913319 1979 1931 3331173 773 3391 139937 3931397 7119
Clingo with Python, (削除) 1600 (削除ここまで) 1689 primes
Specifically, I fix the positions of the digits in a pattern designed for close packing of 4 and 5 digit numbers, with occasional 2 and 3 digit numbers on the boundary, and add constraints saying that each number is a distinct prime. The current packing uses a little more than half of all 4 digit primes (557 of 1061) and of all 5 digit primes starting with 1 or 2 (1047 of 2016).
#script (python)
import re
def primesto(n):
p = [False]*2 + [True]*(n - 2)
for k in xrange(n):
if p[k]:
yield k
p[k*k::k] = [False]*(-(-n//k) - k)
def main(prg):
width = prg.get_const('width')
height = prg.get_const('height')
primes = [p for p in primesto(30000)]
def get_img(x, y):
r = (x + y)%17
return \
((x == 0 or y == 0) and r in (0, 1, 5, 6, 7, 9, 10, 11, 12, 16)) or \
((x == width - 1 or y == height - 1) and r in (1, 2, 3, 7, 8, 9, 13, 14, 15, 16)) or \
(0 < x < width - 1 and 0 < y < height - 1 and r in (0, 1, 2, 3, 5, 6, 7, 8, 9, 11, 12, 13, 14, 16))
img = [[get_img(x, y) for x in xrange(width)] for y in xrange(height)]
def get_spaces():
for y, row in enumerate(img):
for m in re.finditer('#{2,}', ''.join(' #'[cell] for cell in row)):
yield [(x, y) for x in range(m.start(), m.end())]
for x, col in enumerate(zip(*img)):
for m in re.finditer('#{2,}', ''.join(' #'[cell] for cell in col)):
yield [(x, y) for y in range(m.start(), m.end())]
spaces = list(get_spaces())
def data():
for p in primes:
yield 'prime({}, {}).\n'.format(p, len(str(p)))
for j, d in enumerate(str(p)):
yield 'primedigit({}, {}, {}).\n'.format(p, j, int(d))
for i, space in enumerate(spaces):
yield 'space({}, {}).\n'.format(i, len(space))
for j, c in enumerate(space):
yield 'cell({}, {}, {}).\n'.format(i, j, c)
def on_model(model):
digit = {}
for atom in model.atoms():
if atom.name() == 'digit':
c, d = atom.args()
digit[c] = d
for y in xrange(height):
print ''.join(str(digit.get((x, y), ' ')) for x in xrange(width))
prg.add('data', [], ''.join(data()))
prg.ground([('base', []), ('data', [])])
prg.solve_async(on_model=on_model).wait()
#end.
#program base.
1 {loc(P, S) : prime(P, L)} 1 :- space(S, L).
:- prime(P, L), 2 {loc(P, S) : space(S, L)}.
digit(C, D) :- loc(P, S), cell(S, J, C), primedigit(P, J, D).
:- cell(S, J, C), 2 {digit(C, D) : D=0..9}.
#show digit/2.
Warning: at ×ばつ60, this takes about 7 minutes and uses 3 GB of memory. You may want to start with smaller sizes if you just want to see it running.
Output (×ばつ60, 1689 primes):
53 223 9127 281 257 7243 163 239 6133 151 271 5147 229 157 419
953 24733 1657 11597 22063 1283 24989 19843 6947 10589 15607 5903 21221 18869 6
9 20611 3851 27529 10601 1583 28649 29927 6529 11261 15907 2351 12739 22709 449
15629 5939 29567 26959 6569 26729 28921 3529 21859 25253 9421 24859 10781 2383
10133 7949 19919 28181 8053 22027 28871 3559 20663 23671 1069 16451 13567 18371
14369 4027 11119 22283 6229 25189 20233 1531 20353 18047 5407 10009 22193 2039 1
3203 2237 15467 28687 9479 29573 21377 7549 11827 10453 8087 11897 15383 6163 1
193 6359 10487 11699 3343 15121 24847 3677 14503 11399 4783 10949 15727 6011 137
3 5519 15299 29867 7649 15923 28151 5581 13729 22993 3067 25037 22669 9043 1049
4 4051 29423 16229 4139 24133 18131 4751 19373 28759 3251 27299 10273 3863 21397
29683 12343 20441 5477 16993 12007 1307 18089 27793 7607 22639 21929 1777 10139
4801 12473 18839 2647 24359 12239 6761 21481 20089 7349 25693 28087 3631 22973
353 12569 16661 2357 10457 23561 2803 17851 11279 4133 15271 24671 2593 22567 1
7 13649 12911 1487 13537 21019 4729 25237 28097 8581 21169 25561 5839 14323 277
17551 17239 3049 10391 15307 9907 11471 17957 1217 24181 15131 2953 13967 2503
18313 18061 7817 29819 16319 4007 15031 29759 8669 10663 15823 3187 22279 25343
16553 13693 9803 11383 26723 1543 22229 19009 7103 11971 20147 1637 11411 26731
4349 19793 1783 15083 18041 6961 13883 27431 6277 27337 13337 4591 20507 28219
941 23251 8243 18911 13751 4091 10529 26981 7411 22259 18257 9397 20681 23447 6
9 26209 6599 12511 20107 9277 21739 16183 1063 28751 14207 7927 10789 26479 373
14897 2897 16223 17203 8719 10211 13469 5419 29347 19211 8147 29587 13417 6619
21997 5669 13183 28879 2081 15991 25121 6883 25741 12347 7591 27059 15787 26591
28753 2459 28019 12611 1031 17029 25673 3389 22189 22093 1933 10651 15359 2969 9
2213 7547 21491 12577 3889 26711 26417 6211 13619 22571 5417 24481 19993 3301 1
709 2441 16759 27749 7369 10987 16069 1151 18097 12253 6299 26839 24251 8543 173
1 8539 19751 26321 1721 23629 15649 1499 10039 27943 3583 10141 24239 3727 1949
8 8693 17837 26821 6199 23741 25931 5437 19759 26227 5021 18353 21557 7537 25447
25771 25667 11941 1693 17401 22303 7529 17539 13691 9181 17393 27851 1277 24029
3847 23227 27799 6703 26459 26371 4409 20443 29989 5059 11779 14771 9901 19571
347 20023 21503 3347 14249 21347 5077 20743 22481 2203 14549 21661 3877 12197 2
3 18517 27061 7717 14159 18199 7727 27073 13331 6271 28559 19213 9619 23873 269
18947 14969 4567 16453 18229 9431 16141 21283 4099 19141 14051 1907 28643 2069
18149 21817 9787 28901 10321 3457 22777 24071 1087 10513 12107 8167 10181 29297
25999 17659 4159 27481 12109 1901 10889 28069 1019 14389 21179 4507 23087 16369
6971 11981 9209 23831 28607 8123 17923 14449 7459 25031 23053 9829 25913 29191
313 13241 3163 10709 29473 5483 15101 19763 9551 21617 27527 7211 16411 28309 2
9 18731 3001 22273 17191 6361 24763 16193 2029 18181 24097 2557 11813 26017 479
12919 5749 28283 22943 9227 28697 18553 2293 11933 18719 5647 15061 20129 6113
14411 2861 11807 29287 2063 20627 28517 5171 19739 16381 7001 13681 15217 21211
19709 4799 27653 20173 5813 22159 17729 2663 19073 18401 9539 18367 11519 7307 9
7219 1567 11549 14177 1213 22067 12211 8081 12377 27127 1879 29231 27479 2801 2
967 6247 12097 29209 7109 17431 12613 3019 16189 23321 7457 25229 13397 2729 127
7 2887 10639 24421 8779 20707 25841 5717 16607 10151 3079 26953 19181 9719 1433
4 2269 29101 19963 5851 11159 21727 8311 22031 27551 4451 25219 14447 4943 28099
23063 28591 28513 8297 29611 14533 5189 13463 11329 1667 25799 12143 8821 13121
7487 19753 23339 2333 29881 18329 9791 10313 27581 6857 17657 22091 8369 17839
307 17099 15647 2917 29443 10831 7481 23993 14519 7841 10739 20347 9281 22783 2
7 13163 25981 8867 16651 28597 9433 22739 22907 7027 29033 19501 1187 13997 283
16883 19037 5333 22343 19139 3499 21499 24019 8513 23167 15331 6397 19961 2473
14939 20359 6977 13931 23041 9679 24749 25747 8209 12781 23633 8329 14327 11317
21341 22483 1427 25579 12113 1523 27631 12899 5527 18413 14009 4657 27239 11821
9883 19069 6373 27409 16657 6343 25997 21379 5501 27739 12241 4283 27283 18169
367 27733 2003 24109 23021 9767 18661 24203 2521 26203 14929 8231 19801 15773 3
7 19469 5101 23801 13781 2437 14551 27617 1931 13001 29879 5821 23609 27541 883
23911 1237 19289 19927 3967 25147 10433 9151 10627 21493 7669 22129 13859 7121
12547 1697 17609 25801 2477 16747 20021 1627 22271 24001 1951 29129 14561 29633
19577 5227 25969 23689 8291 16699 10067 9133 12757 18913 7069 16447 20693 7901 7
4937 9391 16139 24379 5011 17981 25303 2011 13399 20543 5393 19727 10993 5387 9
811 5791 20389 13171 7951 16477 11197 3461 10891 15373 9697 19819 20509 2207 503
13 1973 937 311 3917 719 113 1733 317 191 3331 773 139 3931 71
Clingo with Python, (削除) 1600 (削除ここまで) (削除) 1689 (削除ここまで) 1740 primes
Specifically, I fix the positions of the digits in a pattern designed for close packing of 4 and 5 digit numbers, with occasional 2 and 3 digit numbers on the boundary, and add constraints saying that each number is a distinct prime. The current packing uses a large fraction of all 4 digit primes (854 of 1061).
#script (python)
import re
def primesto(n):
p = [False]*2 + [True]*(n - 2)
for k in xrange(n):
if p[k]:
yield k
p[k*k::k] = [False]*(-(-n//k) - k)
def main(prg):
width = prg.get_const('width')
height = prg.get_const('height')
primes = list(primesto(30000))
counts = {4: 4, 5: 4}
def get_bands(counts):
if counts:
for k, v in counts.iteritems():
counts1 = counts.copy()
if v == 1:
del counts1[k]
else:
counts1[k] = v - 1
for p in get_bands(counts1):
yield (k,) + p
else:
yield ()
def get_img(bands, offset):
tile = ''.join('#'*k + ' ' for k in bands)
tile = tile[offset:] + tile[:offset]
def get_edge(it, corner):
if not next(it): return True
if not next(it): return corner
if not next(it): return True
if not next(it): return True
if not next(it): return True
return False
def get_cell(x, y):
return \
(x == 0 and get_edge((get_cell(i, y) for i in xrange(1, width - 1)), not 1 <= y < height - 1)) or \
(y == 0 and get_edge((get_cell(x, j) for j in xrange(1, height - 1)), not 1 <= x < width - 1)) or \
(x == width - 1 and get_edge((get_cell(i, y) for i in xrange(width - 2, 0, -1)), not 1 <= y < height - 1)) or \
(y == height - 1 and get_edge((get_cell(x, j) for j in xrange(height - 2, 0, -1)), not 1 <= x < width - 1)) or \
(1 <= x < width - 1 and 1 <= y < height - 1 and tile[(x + y)%len(tile)] == '#')
img = [[get_cell(x, y) for x in xrange(width)] for y in xrange(height)]
def get_spaces():
for y, row in enumerate(img):
for m in re.finditer('#{2,}', ''.join(' #'[cell] for cell in row)):
yield [(x, y) for x in range(m.start(), m.end())]
for x, col in enumerate(zip(*img)):
for m in re.finditer('#{2,}', ''.join(' #'[cell] for cell in col)):
yield [(x, y) for y in range(m.start(), m.end())]
spaces = list(get_spaces())
return img, spaces
img, spaces = max((get_img(p, o) for p in get_bands(counts) for o in xrange(p[0] + 1)), key=lambda (img, spaces): len(spaces))
def data():
for p in primes:
yield 'prime({}, {}).\n'.format(p, len(str(p)))
for j, d in enumerate(str(p)):
yield 'primedigit({}, {}, {}).\n'.format(p, j, int(d))
for i, space in enumerate(spaces):
yield 'space({}, {}).\n'.format(i, len(space))
for j, c in enumerate(space):
yield 'cell({}, {}, {}).\n'.format(i, j, c)
def on_model(model):
digit = {}
for atom in model.atoms():
if atom.name() == 'digit':
c, d = atom.args()
digit[c] = d
for y in xrange(height):
print ''.join(str(digit.get((x, y), ' ')) for x in xrange(width))
prg.add('data', [], ''.join(data()))
prg.ground([('base', []), ('data', [])])
prg.solve_async(on_model=on_model).wait()
#end.
#program base.
1 {loc(P, S) : prime(P, L)} 1 :- space(S, L).
:- prime(P, L), 2 {loc(P, S) : space(S, L)}.
digit(C, D) :- loc(P, S), cell(S, J, C), primedigit(P, J, D).
:- cell(S, J, C), 2 {digit(C, D) : D=0..9}.
#show digit/2.
Warning: at ×ばつ60, this takes about 8 minutes and uses 3 GB of memory. You may want to start with smaller sizes if you just want to see it running.
Output (×ばつ60, 1740 primes):
13 293 4153 4217 9137 239 3169 223 127 281 9281 9173 4253 257 9239 3
719 28349 2039 7159 4603 17491 6299 17207 19463 19793 3301 4483 5657 20639 10061
7 26417 7307 2687 9479 14767 3571 10957 21341 14057 5813 6689 2383 19553 2647 3
15679 6679 6217 1303 19037 5821 11551 18797 28723 3457 6581 5309 12043 4441 2
14327 4783 5881 6131 21107 2549 12953 24001 12113 7247 9973 8287 16573 5519 149
20177 4967 5647 1907 24439 7237 28979 24781 26083 9859 6691 3701 25237 9041 1637
2953 6781 2851 7079 10139 4001 20981 20341 23531 1913 5081 7639 25169 7457 29437
743 3907 6949 1237 10937 3203 19213 23971 18367 5227 5879 8191 17827 4561 16927
9 1613 8369 3911 23327 1571 25741 15889 19417 3083 3049 4421 25111 8147 10391
9 9151 3019 3499 11839 5851 23669 16823 14891 9829 9403 1069 26099 8737 21911 2
17389 3217 7963 13337 5779 19841 16007 10313 7673 7703 5897 23911 4409 26737 131
2711 9697 8563 29611 2699 20231 27631 15823 7127 7937 8171 23623 6067 14489 1307
751 2179 7877 12263 7331 17923 26153 11443 7603 1429 8861 28871 8291 20599 20129
3 3251 5087 28949 4519 22501 15667 28837 2543 3617 2837 15217 4129 20357 29879
2 7507 4127 27479 2521 11423 17497 12409 6599 7297 1249 14813 6301 10501 24151
21487 1777 20143 7109 12239 22063 23761 9931 1579 8929 26641 3221 18251 29429 1
3511 2389 27103 9767 27763 23399 18481 5869 8431 2141 15259 6367 29131 17789 137
941 6719 29269 1811 12829 14929 11953 4817 9787 6469 21377 2473 17033 11311 1453
3 4549 11491 2777 11351 28879 21061 3607 5441 5861 15493 8941 15101 14437 28579
3 3229 18719 7841 18701 23087 23561 8597 6637 7487 19681 6229 25321 16417 14717
15131 13151 6997 27947 26669 24179 5981 4297 5683 18979 4937 16787 24421 20089
2531 13127 5507 10973 13121 25759 8353 9413 6343 17713 7211 21523 12143 10477 1
107 19381 6871 18803 20117 20849 9091 4051 2333 11399 5987 21617 13147 12281 251
3 21713 4027 15299 15031 14389 7789 2617 4993 18493 1103 27983 22739 26987 2339
22853 1297 15959 19531 12799 2269 4943 3917 10093 4657 25999 16603 11617 12919
14923 1021 15313 11287 14969 1009 2503 9907 25247 4523 16411 12979 22397 8819 7
18191 5039 21019 13619 29947 2087 6793 3529 29671 2861 22259 20921 17291 6703 4
1523 4919 21379 23071 20051 9133 8761 6029 11813 7517 14503 28927 17707 8311 701
311 2939 28183 12347 10357 6143 5437 6011 13789 7573 23629 21773 28703 5059 8609
7 4799 17333 28069 17921 9803 8543 9601 28151 9059 10457 12119 15661 3593 21937
1 7759 13399 19207 21407 3433 7187 5717 22571 7949 19489 12251 10271 8363 2879 3
13883 10663 20023 12583 1193 5011 1889 20147 4723 25447 15887 29921 4679 7459 4
2887 14341 14759 20593 2267 1567 9901 14411 1229 12041 20483 10889 6163 3517 947
983 24799 13099 13763 4733 9029 5281 17231 6947 29311 22709 11657 2113 8167 2081
1 24359 29399 11117 2111 9341 9187 25931 7433 20347 16481 17293 5021 8681 17419
21323 14737 18311 1559 1279 2273 20509 7283 21739 28549 22193 8011 9887 2293 3
21011 19231 16127 6197 5107 7151 21013 2551 22943 28057 13229 9277 7229 1721 1
19927 22549 20641 8053 6529 5737 29021 1483 23027 24977 17623 3347 6791 1213 157
8573 23447 24499 4273 8963 6257 21881 3343 27329 17627 28307 2393 4493 3613 1117
167 15991 17029 2803 6673 3797 28219 4621 22277 12071 13043 6661 6323 8297 19433
7 24517 11777 1609 9941 2437 26189 8831 10771 11467 24091 2371 3331 7607 14947
22727 13967 1471 7451 1367 11113 7477 11527 24007 23557 1277 4969 6829 28097
12073 11887 3181 8999 5641 13411 7321 22901 28001 20939 3889 5231 8863 26357 3
20011 23671 5669 1291 9421 12689 4903 21647 19483 12157 4861 5381 9067 11471 283
2663 22123 2833 1663 8677 19891 8039 19141 23321 17417 2027 9949 6389 23459 7351
967 15901 1831 1013 5527 12697 5573 16547 14029 23333 3623 9007 6047 12739 18539
7 14369 9719 6199 7499 20663 3413 23339 25097 12973 9791 9643 2459 18353 9371 1
16843 8329 1451 5279 13441 9433 27647 28643 12007 4463 6269 7523 22861 6899 1
18143 5743 2591 4211 27551 9461 27689 11279 14533 2731 3001 6857 13757 3259 277
22483 3943 9011 8933 18593 6569 26891 22381 22109 4721 1733 2377 11549 5387 1459
6203 5351 9851 6133 29573 4513 28517 14083 15773 3109 9967 5113 29063 1427 17257
911 8713 8783 9613 27253 3851 25537 16993 17683 3037 4007 5651 12487 6779 26251
1 7243 3023 4751 10459 4931 25639 25867 12101 6373 4201 8123 15227 7589 10601
1 9631 1061 1549 10159 1381 18047 26687 20411 2129 2011 1583 12899 9749 26339 1
25127 3389 4013 15647 1669 25933 24443 21557 7853 3449 6089 28771 1847 21491 263
8501 2309 2797 16189 2003 22637 15973 28697 2741 2729 1361 25667 2131 23633 2297
359 9551 6553 11597 6451 20123 10259 29287 5923 8233 1423 10133 1373 23773 11633
7 5119 7829 24019 7691 12479 14143 20873 5323 1493 3709 18947 5701 12413 27997
4 3697 5261 17359 1657 26371 20443 11239 4093 7013 5303 15767 4259 13499 13597 5
31 3919 7393 199 9397 379 971 193 3319 1979 1931 173 3391 937 397 19
Clingo with Python, 1600(削除) 1600 (削除ここまで) 1689 primes
Specifically, I fix the positions of the digits in a pattern designed for maximalclose packing of 4 and 5 digit numbers (with, with occasional 2, 3, and 43 digit numbers on the boundary), and add constraints to saysaying that each number is a distinct prime.
The pattern I chose has the property that most initial digits are also final digits, so we can restrict the list The current packing uses a little more than half of all 4 digit primes (557 of 1061) and of all 5 digit primes to those beginning instarting with 1, 3, 7, or 9. Mirror-reflecting the pattern would let me relax this restriction, but the reduced list2 (1047 of primes significantly improves memory usage and speed anyway, so I left it this way2016).
#script (python)
import re
def primesto(n):
p = [False]*2 + [True]*(n - 2)
for k in xrange(n):
if p[k]:
yield k
p[k*k::k] = [False]*(-(-n//k) - k)
def main(prg):
width = prg.get_const('width')
height = prg.get_const('height')
primes = [p for p in primesto(10**5) if p//10000 in [0, 1, 3, 7, 9]]
img = [[False]*width for y in xrange(height30000)]
for o in xrange(max(width, height)//6*-6,def maxget_img(width, height)//6*6 + 1x, 6y):
x0r = max(0,x o+ y)%17
y0return =\
max(0, -o)
s = min(width(x -== x0,0 heightor -y y0== 0) and r in (0, 1, 5, 6, for7, k9, in10, xrange(s):
11, 12, 16)) or \
x2 = x0 + max(0,(x k== width - 2)
1 or y == height - 1) and r in (1, 2, x33, =7, x08, +9, min(s13, k14, +15, 316)) or \
img[y0 + k][x2:x3] = [True]*(x3 - x2)
0 < x < width - 1 and for0 x,< y in< [(x0height - 1, y0and +r 1),in (x0 + 10, y0 - 1), (x0 + s2, y0 + s3, -5, 2)6, (x07, +8, s9, -11, 212, y013, +14, s16)]:)
img = if[[get_img(x, 0y) <=for x <in xrange(width and 0)] <=for y <in xrange(height:
img[y][x] = True)]
def get_spaces():
for y, row in enumerate(img):
for m in re.finditer('#{2,}', ''.join(' #'[cell] for cell in row)):
yield [(x, y) for x in range(m.start(), m.end())]
for x, col in enumerate(zip(*img)):
for m in re.finditer('#{2,}', ''.join(' #'[cell] for cell in col)):
yield [(x, y) for y in range(m.start(), m.end())]
spaces = list(get_spaces())
def data():
for p in primes:
yield 'prime({}, {}).\n'.format(p, len(str(p)))
for j, d in enumerate(str(p)):
yield 'primedigit({}, {}, {}).\n'.format(p, j, int(d))
for i, space in enumerate(spaces):
yield 'space({}, {}).\n'.format(i, len(space))
for j, c in enumerate(space):
yield 'cell({}, {}, {}).\n'.format(i, j, c)
def on_model(model):
digit = {}
for atom in model.atoms():
if atom.name() == 'digit':
c, d = atom.args()
digit[c] = d
for y in xrange(height):
print ''.join(str(digit.get((x, y), ' ')) for x in xrange(width))
prg.add('data', [], ''.join(data()))
prg.ground([('base', []), ('data', [])])
prg.solve_async(on_model=on_model).wait()
#end.
#program base.
1 {loc(P, S) : prime(P, L)} 1 :- space(S, L).
:- prime(P, L), 2 {loc(P, S) : space(S, L)}.
digit(C, D) :- loc(P, S), cell(S, J, C), primedigit(P, J, D).
:- cell(S, J, C), 2 {digit(C, D) : D=0..9}.
#show digit/2.
Warning: at ×ばつ60, this takes about 127 minutes and uses 63 GB of memory. You may want to start with smaller sizes if you just want to see it running.
Output (×ばつ60, 16001689 primes):
433 349 53 151 223 2939127 541281 743257 7243 499 163 653 239 6133 457 151 461 271 8275147 359229 661157 419
953 24733 71
99311657 3826111597 9051122063 121011283 1529924989 7276719843 308296947 9019110589 3038915607 138795903 3863921221 9008918869 111496 769
18287 318179 1258920611 368773851 7009927529 7804110601 118871583 1495128649 9547129927 173936529 7164711261 7095115907 999612351 112739 22709 449
90499 1214915629 112875939 1014129567 1885926959 312196569 7045726729 7013928921 180893529 1549721859 7843725253 909179421 9277924859 10781 2383
3559710133 710697949 9146319919 9562128181 175978053 3735722027 9204128871 186373559 7234120663 1852123671 138411069 9757716451 9526113567 18371
14369 74027 7388311119 7984322283 711476229 9268325189 1399720233 952391531 9169120353 7962718047 179815407 7050110009 9416922193 179112039 701571
2393203 726792237 3502315467 1224128687 317519479 9235329573 1300721377 153737549 7228711827 1011110453 972598087 7640311897 9217915383 94796163 1
8011193 344716359 1045310487 9149911699 315433343 9952915121 1915724847 309773677 9628914503 3184911399 751094783 7737710949 9985915727 9676011 137
33461 346733 741895519 1117315299 9388729867 946037649 1875715923 1255328151 928635581 9349713729 9958122993 775693067 1378125037 122669 9043 951311049
4 107294051 3139329423 9655716229 391814139 1026724133 7063918131 923774751 9548319373 9641928759 393433251 9007327299 9123710273 3863 21397
29683 9183712343 9124920441 721395477 3496316993 1177712007 327711307 7761118089 9845927793 302237607 1607322639 9810121929 136131777 7998710139
4801 312473 1234718839 313212647 7167124359 7816712239 979876761 7628321481 9791920089 982517349 1895925693 9836928087 775633631 9853322973 91961
269353 9918112569 3976116661 938512357 3072710457 9208323561 793012803 9691117851 3091111279 732434133 9695915271 7579324671 763792593 304122567 1
2837 902477 7999913649 7793312911 398831487 1475313537 1186721019 967974729 9894725237 3080328097 712578581 7495921169 7747725561 1735839 14323 277
98639 18461 1240917551 9163917239 310133049 9037310391 1969915307 955079907 9000111471 1113117957 989931217 7295924181 7644115131 12953 13967 700512503
3417118313 7724918061 931397817 7535329819 7166316319 351534007 7099915031 9822729759 104338669 7207710663 9904115823 392093187 22279 25343
16553 7675313693 926279803 7780111383 3002926723 994971543 1314722229 1359719009 793497103 7425711971 9907920147 965871637 9955911411 9003126731
4349 319793 141071783 3018715083 1440718041 754376961 7249313883 3379727431 963296277 7000927337 9108113337 346674591 3789720507 3318128219 17989
829941 7084923251 962818243 1085918911 9695313751 348414091 1428110529 9613726981 960017411 1347722259 9190918257 359339397 1676320681 990123447 6
5407 135379 1326726209 781736599 1731712511 3427320107 778939277 7600121739 3166716183 762531063 9979328751 7933714207 133397927 71910789 26479 373
92647 98057 3431314897 965172897 9736916223 7101117203 307138719 9847910211 1757913469 798615419 7502929347 7522719211 703098147 329587 13417 951776619
7961321997 165475669 1718913183 7399928879 343032081 9592315991 3897125121 713396883 3350325741 3299912347 129597591 1086127059 15787 26591
28753 952192459 7196328019 3030712611 984731031 7688317029 7000325673 900673389 1207322189 9245922093 383331933 3862910651 3599315359 121192969 9
2213 37547 9469321491 1189712577 305093889 9382726711 3094926417 761596211 7599713619 3034722571 320895417 3498124481 7778319993 336413301 363431
199709 395112441 7057116759 1000927749 970017369 9562910987 9677916069 729771151 7267318097 7222712253 347036299 9607926839 1444724251 72378543 173
8237 954291 960978539 7084119751 1090926321 309371721 1944723629 9350315649 148691499 1297910039 1453727943 102233583 3029310141 10924239 3727 1949
139038 186918693 9877317837 1097326821 379936199 3211923741 7588325931 988695437 9990719759 1573126227 781575021 9425318353 3580121557 77537 25447
9483725771 1097925667 1772911941 346071693 9098917401 1837922303 321417529 7954917539 9415113691 178819181 9136717393 3119327851 113931277 24029
3847 23227 9745927799 126896703 7147326459 9850726371 391074409 7148320443 3256329989 122395059 7957911779 3955114771 398279901 9508919571 34949
347 720023 9064121503 356173347 3019714249 3719921347 195015077 7437720743 1396322481 909972203 7255914549 3151321661 117013877 9154112197 168312
521 953273 7413118517 1124327061 785777717 7419714159 1237318199 798237727 7273327073 7987313331 744136271 9648728559 9005919213 10139619 23873 269
4813 94079 1999318947 7483114969 187974567 7819116453 7699118229 326939431 3683316141 9971921283 189114099 3328719141 1438914051 7091907 28643 2069
78497 9052318149 3597721817 163399787 9880728901 3250710321 367393457 1379922777 7751324071 175511087 3607310513 9218912107 955318167 310181 29297
3115125999 7209117659 132914159 9481927481 9825712109 157371901 9373910889 3484728069 787971019 7783914389 3736121179 960594507 9196723087 16369
6971 11981 109939209 1567923831 7132728607 975838123 9821317923 9264114449 362297459 9527925031 1207123053 312679829 9872925913 3302929191 90437
313 113241 385673163 1770710709 7634329473 779835483 9618115101 3751719763 383299551 7920121617 9444127527 954617211 9455916411 1060128309 134112
281 386999 1456118731 779993001 3370322273 9241917191 356036361 7883924763 1740116193 717612029 9446318181 9871724097 924892557 325911813 26017 479
3019 32839 7880312919 700395749 9973328283 9646122943 156479227 3234128697 9001918553 748212293 3278311933 3115318719 303675647 16715061 20129 6113
34687 9447714411 341412861 3484311807 1000729287 194272063 7648720627 1177928517 397035171 1204319739 1599116381 363077001 1472313681 915217 21211
1789119709 162314799 7625927653 1180720173 377815813 1613922159 7933317729 993972663 1132919073 9472318401 978619539 3214318367 1966111519 7307 9
7219 1567 7997911549 3872914177 369131213 1487922067 3619112211 950638081 9061912377 3241127127 955271879 9703929231 7663127479 311392801 917812
967 16247 9007112097 3247929209 738237109 9712717431 3339112613 714293019 7540116189 7045123321 936377457 1942925229 3302313397 945612729 37309127
419 397997 108472887 7657910639 1622924421 707938779 9585720707 1692125841 915775717 7694316607 9271710151 137093079 9360126953 915719181 9719 1433
83634 919512269 1125729101 7797719963 747615851 9255711159 1687121727 944478311 3699722031 9064727551 785714451 3010325219 3081714447 3314943 28099
7190923063 1913928591 3172328513 968478297 3258729611 7918114533 961795189 7577313463 9340711329 794931667 9804125799 3304912143 150738821 113121
7487 1871319753 1036923339 902812333 7475929881 7270118329 720739791 9499910313 7825927581 922976857 9371917657 3484922091 302118369 3922917839
307 17099 9797315647 737092917 9163129443 7108110831 770417481 9031323993 1580914519 317697841 7264910739 9523320347 710599281 7626122783 300472
17 3348713163 7017725981 163198867 3185916651 3625128597 788239433 7807922739 3491322907 902397027 7723729033 3870719501 197091187 1030313997 283
439 92467 3067716883 9840719037 702235333 1436922343 7923119139 110833499 7702921499 9854324019 730138513 1276323167 1809715331 92936397 19961 2473
6691 3535314939 7068720359 160976977 9678713931 3941923041 137519679 7879124749 9721325747 785178209 9987712781 3098323633 798298329 12714327 11317
3125921341 9388922483 753071427 7518125579 3205112113 153291523 3751127631 1142312899 967315527 1955318413 1309314009 908414657 9211927239 111821
9883 9784919069 352796373 9173327409 3580916657 345016343 3335925997 3298321379 338515501 1117727739 7468712241 756294283 7550327283 9012718169
367 27733 752392003 3346924109 1106923021 989299767 7887718661 1674124203 720192521 9838926203 1154914929 181498231 7061919801 1107115773 352813
37 9239919469 925675101 7401723801 9465113781 334132437 9931714551 3425927617 923331931 9124313001 9899929879 111975821 1626723609 9355927541 883
821 90217 3891723911 308591237 9913719289 1393319927 174193967 7338725147 3958110433 933379151 9464910627 1996321493 178377669 393122129 13859 7121
2017 7513312547 905331697 7726117609 9012125801 189792477 9738116747 7969720021 752231627 1078122271 1003924001 980171951 3876729129 31114561 29633
3559119577 923475227 7452125969 3179323689 106878291 7136316699 1291110067 362419133 7840112757 9240118913 754317069 9961116447 3867120693 37901 7
4937 998179391 9619916139 9199724379 734535011 3106317981 1178925303 962212011 9442713399 1564920543 172315393 7111919727 9674910993 353395387 9
811 719835791 1250320389 7795113171 913817951 7484316477 3308311197 372013461 1447910891 1342115373 942619697 9355319819 3859320509 150912207 503
137 317 19113 7391973 937 977 311 733 3917 719 397 113 193 1733 179317 373191 3133331 773 937 139 131 3931 71
Clingo with Python, 1600 primes
Specifically I fix the positions of the digits in a pattern designed for maximal packing of 5 digit numbers (with occasional 2, 3, and 4 digit numbers on the boundary), and add constraints to say that each number is a distinct prime.
The pattern I chose has the property that most initial digits are also final digits, so we can restrict the list of 5 digit primes to those beginning in 1, 3, 7, or 9. Mirror-reflecting the pattern would let me relax this restriction, but the reduced list of primes significantly improves memory usage and speed anyway, so I left it this way.
#script (python)
import re
def primesto(n):
p = [False]*2 + [True]*(n - 2)
for k in xrange(n):
if p[k]:
yield k
p[k*k::k] = [False]*(-(-n//k) - k)
def main(prg):
width = prg.get_const('width')
height = prg.get_const('height')
primes = [p for p in primesto(10**5) if p//10000 in [0, 1, 3, 7, 9]]
img = [[False]*width for y in xrange(height)]
for o in xrange(max(width, height)//6*-6, max(width, height)//6*6 + 1, 6):
x0 = max(0, o)
y0 = max(0, -o)
s = min(width - x0, height - y0) for k in xrange(s):
x2 = x0 + max(0, k - 2)
x3 = x0 + min(s, k + 3) img[y0 + k][x2:x3] = [True]*(x3 - x2)
for x, y in [(x0 - 1, y0 + 1), (x0 + 1, y0 - 1), (x0 + s, y0 + s - 2), (x0 + s - 2, y0 + s)]:
if 0 <= x < width and 0 <= y < height:
img[y][x] = True
def get_spaces():
for y, row in enumerate(img):
for m in re.finditer('#{2,}', ''.join(' #'[cell] for cell in row)):
yield [(x, y) for x in range(m.start(), m.end())]
for x, col in enumerate(zip(*img)):
for m in re.finditer('#{2,}', ''.join(' #'[cell] for cell in col)):
yield [(x, y) for y in range(m.start(), m.end())]
spaces = list(get_spaces())
def data():
for p in primes:
yield 'prime({}, {}).\n'.format(p, len(str(p)))
for j, d in enumerate(str(p)):
yield 'primedigit({}, {}, {}).\n'.format(p, j, int(d))
for i, space in enumerate(spaces):
yield 'space({}, {}).\n'.format(i, len(space))
for j, c in enumerate(space):
yield 'cell({}, {}, {}).\n'.format(i, j, c)
def on_model(model):
digit = {}
for atom in model.atoms():
if atom.name() == 'digit':
c, d = atom.args()
digit[c] = d
for y in xrange(height):
print ''.join(str(digit.get((x, y), ' ')) for x in xrange(width))
prg.add('data', [], ''.join(data()))
prg.ground([('base', []), ('data', [])])
prg.solve_async(on_model=on_model).wait()
#end.
#program base.
1 {loc(P, S) : prime(P, L)} 1 :- space(S, L).
:- prime(P, L), 2 {loc(P, S) : space(S, L)}.
digit(C, D) :- loc(P, S), cell(S, J, C), primedigit(P, J, D).
:- cell(S, J, C), 2 {digit(C, D) : D=0..9}.
#show digit/2.
Warning: at ×ばつ60, this takes about 12 minutes and uses 6 GB of memory. You may want to start with smaller sizes if you just want to see it running.
Output (×ばつ60, 1600 primes):
433 349 151 293 541 743 499 653 457 461 827 359 661 71
9931 38261 90511 12101 15299 72767 30829 90191 30389 13879 38639 90089 11149 769
18287 31817 12589 36877 70099 78041 11887 14951 95471 17393 71647 70951 99961 1
90499 12149 11287 10141 18859 31219 70457 70139 18089 15497 78437 90917 92779
35597 71069 91463 95621 17597 37357 92041 18637 72341 18521 13841 97577 95261
7 73883 79843 71147 92683 13997 95239 91691 79627 17981 70501 94169 17911 70157
239 72679 35023 12241 31751 92353 13007 15373 72287 10111 97259 76403 92179 9479
8011 34471 10453 91499 31543 99529 19157 30977 96289 31849 75109 77377 99859 967
33461 34673 74189 11173 93887 94603 18757 12553 92863 93497 99581 77569 13781 1 95131 10729 31393 96557 39181 10267 70639 92377 95483 96419 39343 90073 91237
91837 91249 72139 34963 11777 32771 77611 98459 30223 16073 98101 13613 79987
3 12347 31321 71671 78167 97987 76283 97919 98251 18959 98369 77563 98533 91961
269 99181 39761 93851 30727 92083 79301 96911 30911 73243 96959 75793 76379 3041
2837 90247 79999 77933 39883 14753 11867 96797 98947 30803 71257 74959 77477 173
98639 18461 12409 91639 31013 90373 19699 95507 90001 11131 98993 72959 76441 1 70051 34171 77249 93139 75353 71663 35153 70999 98227 10433 72077 99041 39209
76753 92627 77801 30029 99497 13147 13597 79349 74257 99079 96587 99559 90031
3 14107 30187 14407 75437 72493 33797 96329 70009 91081 34667 37897 33181 17989
829 70849 96281 10859 96953 34841 14281 96137 96001 13477 91909 35933 16763 9901
5407 13537 13267 78173 17317 34273 77893 76001 31667 76253 99793 79337 13339 719
92647 98057 34313 96517 97369 71011 30713 98479 17579 79861 75029 75227 70309 3 95177 79613 16547 17189 73999 34303 95923 38971 71339 33503 32999 12959 10861
95219 71963 30307 98473 76883 70003 90067 12073 92459 38333 38629 35993 12119
3 94693 11897 30509 93827 30949 76159 75997 30347 32089 34981 77783 33641 36343
199 39511 70571 10009 97001 95629 96779 72977 72673 72227 34703 96079 14447 7237
8237 95429 96097 70841 10909 30937 19447 93503 14869 12979 14537 10223 30293 109
13903 18691 98773 10973 37993 32119 75883 98869 99907 15731 78157 94253 35801 7
94837 10979 17729 34607 90989 18379 32141 79549 94151 17881 91367 31193 11393
97459 12689 71473 98507 39107 71483 32563 12239 79579 39551 39827 95089 34949
7 90641 35617 30197 37199 19501 74377 13963 90997 72559 31513 11701 91541 16831
521 95327 74131 11243 78577 74197 12373 79823 72733 79873 74413 96487 90059 1013
4813 94079 19993 74831 18797 78191 76991 32693 36833 99719 18911 33287 14389 709
78497 90523 35977 16339 98807 32507 36739 13799 77513 17551 36073 92189 95531 3
31151 72091 13291 94819 98257 15737 93739 34847 78797 77839 37361 96059 91967
10993 15679 71327 97583 98213 92641 36229 95279 12071 31267 98729 33029 90437
1 38567 17707 76343 77983 96181 37517 38329 79201 94441 95461 94559 10601 13411
281 38699 14561 77999 33703 92419 35603 78839 17401 71761 94463 98717 92489 3259
3019 32839 78803 70039 99733 96461 15647 32341 90019 74821 32783 31153 30367 167
34687 94477 34141 34843 10007 19427 76487 11779 39703 12043 15991 36307 14723 9
17891 16231 76259 11807 37781 16139 79333 99397 11329 94723 97861 32143 19661
79979 38729 36913 14879 36191 95063 90619 32411 95527 97039 76631 31139 91781
1 90071 32479 73823 97127 33391 71429 75401 70451 93637 19429 33023 94561 37309
419 39799 10847 76579 16229 70793 95857 16921 91577 76943 92717 13709 93601 9157
8363 91951 11257 77977 74761 92557 16871 94447 36997 90647 78571 30103 30817 331
71909 19139 31723 96847 32587 79181 96179 75773 93407 79493 98041 33049 15073 1
18713 10369 90281 74759 72701 72073 94999 78259 92297 93719 34849 30211 39229
97973 73709 91631 71081 77041 90313 15809 31769 72649 95233 71059 76261 30047
1 33487 70177 16319 31859 36251 78823 78079 34913 90239 77237 38707 19709 10303
439 92467 30677 98407 70223 14369 79231 11083 77029 98543 73013 12763 18097 9293
6691 35353 70687 16097 96787 39419 13751 78791 97213 78517 99877 30983 79829 127
31259 93889 75307 75181 32051 15329 37511 11423 96731 19553 13093 90841 92119 1
97849 35279 91733 35809 34501 33359 32983 33851 11177 74687 75629 75503 90127
75239 33469 11069 98929 78877 16741 72019 98389 11549 18149 70619 11071 35281
3 92399 92567 74017 94651 33413 99317 34259 92333 91243 98999 11197 16267 93559
821 90217 38917 30859 99137 13933 17419 73387 39581 93337 94649 19963 17837 3931
2017 75133 90533 77261 90121 18979 97381 79697 75223 10781 10039 98017 38767 311
35591 92347 74521 31793 10687 71363 12911 36241 78401 92401 75431 99611 38671 3
99817 96199 91997 73453 31063 11789 96221 94427 15649 17231 71119 96749 35339
71983 12503 77951 91381 74843 33083 37201 14479 13421 94261 93553 38593 15091
137 317 191 739 977 733 397 193 179 373 313 937 131
Clingo with Python, (削除) 1600 (削除ここまで) 1689 primes
Specifically, I fix the positions of the digits in a pattern designed for close packing of 4 and 5 digit numbers, with occasional 2 and 3 digit numbers on the boundary, and add constraints saying that each number is a distinct prime. The current packing uses a little more than half of all 4 digit primes (557 of 1061) and of all 5 digit primes starting with 1 or 2 (1047 of 2016).
#script (python)
import re
def primesto(n):
p = [False]*2 + [True]*(n - 2)
for k in xrange(n):
if p[k]:
yield k
p[k*k::k] = [False]*(-(-n//k) - k)
def main(prg):
width = prg.get_const('width')
height = prg.get_const('height')
primes = [p for p in primesto(30000)]
def get_img(x, y):
r = (x + y)%17
return \
((x == 0 or y == 0) and r in (0, 1, 5, 6, 7, 9, 10, 11, 12, 16)) or \
((x == width - 1 or y == height - 1) and r in (1, 2, 3, 7, 8, 9, 13, 14, 15, 16)) or \
(0 < x < width - 1 and 0 < y < height - 1 and r in (0, 1, 2, 3, 5, 6, 7, 8, 9, 11, 12, 13, 14, 16))
img = [[get_img(x, y) for x in xrange(width)] for y in xrange(height)]
def get_spaces():
for y, row in enumerate(img):
for m in re.finditer('#{2,}', ''.join(' #'[cell] for cell in row)):
yield [(x, y) for x in range(m.start(), m.end())]
for x, col in enumerate(zip(*img)):
for m in re.finditer('#{2,}', ''.join(' #'[cell] for cell in col)):
yield [(x, y) for y in range(m.start(), m.end())]
spaces = list(get_spaces())
def data():
for p in primes:
yield 'prime({}, {}).\n'.format(p, len(str(p)))
for j, d in enumerate(str(p)):
yield 'primedigit({}, {}, {}).\n'.format(p, j, int(d))
for i, space in enumerate(spaces):
yield 'space({}, {}).\n'.format(i, len(space))
for j, c in enumerate(space):
yield 'cell({}, {}, {}).\n'.format(i, j, c)
def on_model(model):
digit = {}
for atom in model.atoms():
if atom.name() == 'digit':
c, d = atom.args()
digit[c] = d
for y in xrange(height):
print ''.join(str(digit.get((x, y), ' ')) for x in xrange(width))
prg.add('data', [], ''.join(data()))
prg.ground([('base', []), ('data', [])])
prg.solve_async(on_model=on_model).wait()
#end.
#program base.
1 {loc(P, S) : prime(P, L)} 1 :- space(S, L).
:- prime(P, L), 2 {loc(P, S) : space(S, L)}.
digit(C, D) :- loc(P, S), cell(S, J, C), primedigit(P, J, D).
:- cell(S, J, C), 2 {digit(C, D) : D=0..9}.
#show digit/2.
Warning: at ×ばつ60, this takes about 7 minutes and uses 3 GB of memory. You may want to start with smaller sizes if you just want to see it running.
Output (×ばつ60, 1689 primes):
53 223 9127 281 257 7243 163 239 6133 151 271 5147 229 157 419
953 24733 1657 11597 22063 1283 24989 19843 6947 10589 15607 5903 21221 18869 6
9 20611 3851 27529 10601 1583 28649 29927 6529 11261 15907 2351 12739 22709 449
15629 5939 29567 26959 6569 26729 28921 3529 21859 25253 9421 24859 10781 2383
10133 7949 19919 28181 8053 22027 28871 3559 20663 23671 1069 16451 13567 18371
14369 4027 11119 22283 6229 25189 20233 1531 20353 18047 5407 10009 22193 2039 1
3203 2237 15467 28687 9479 29573 21377 7549 11827 10453 8087 11897 15383 6163 1
193 6359 10487 11699 3343 15121 24847 3677 14503 11399 4783 10949 15727 6011 137
3 5519 15299 29867 7649 15923 28151 5581 13729 22993 3067 25037 22669 9043 1049
4 4051 29423 16229 4139 24133 18131 4751 19373 28759 3251 27299 10273 3863 21397
29683 12343 20441 5477 16993 12007 1307 18089 27793 7607 22639 21929 1777 10139
4801 12473 18839 2647 24359 12239 6761 21481 20089 7349 25693 28087 3631 22973
353 12569 16661 2357 10457 23561 2803 17851 11279 4133 15271 24671 2593 22567 1
7 13649 12911 1487 13537 21019 4729 25237 28097 8581 21169 25561 5839 14323 277
17551 17239 3049 10391 15307 9907 11471 17957 1217 24181 15131 2953 13967 2503
18313 18061 7817 29819 16319 4007 15031 29759 8669 10663 15823 3187 22279 25343
16553 13693 9803 11383 26723 1543 22229 19009 7103 11971 20147 1637 11411 26731
4349 19793 1783 15083 18041 6961 13883 27431 6277 27337 13337 4591 20507 28219
941 23251 8243 18911 13751 4091 10529 26981 7411 22259 18257 9397 20681 23447 6
9 26209 6599 12511 20107 9277 21739 16183 1063 28751 14207 7927 10789 26479 373
14897 2897 16223 17203 8719 10211 13469 5419 29347 19211 8147 29587 13417 6619
21997 5669 13183 28879 2081 15991 25121 6883 25741 12347 7591 27059 15787 26591
28753 2459 28019 12611 1031 17029 25673 3389 22189 22093 1933 10651 15359 2969 9
2213 7547 21491 12577 3889 26711 26417 6211 13619 22571 5417 24481 19993 3301 1
709 2441 16759 27749 7369 10987 16069 1151 18097 12253 6299 26839 24251 8543 173
1 8539 19751 26321 1721 23629 15649 1499 10039 27943 3583 10141 24239 3727 1949
8 8693 17837 26821 6199 23741 25931 5437 19759 26227 5021 18353 21557 7537 25447
25771 25667 11941 1693 17401 22303 7529 17539 13691 9181 17393 27851 1277 24029
3847 23227 27799 6703 26459 26371 4409 20443 29989 5059 11779 14771 9901 19571
347 20023 21503 3347 14249 21347 5077 20743 22481 2203 14549 21661 3877 12197 2
3 18517 27061 7717 14159 18199 7727 27073 13331 6271 28559 19213 9619 23873 269
18947 14969 4567 16453 18229 9431 16141 21283 4099 19141 14051 1907 28643 2069
18149 21817 9787 28901 10321 3457 22777 24071 1087 10513 12107 8167 10181 29297
25999 17659 4159 27481 12109 1901 10889 28069 1019 14389 21179 4507 23087 16369
6971 11981 9209 23831 28607 8123 17923 14449 7459 25031 23053 9829 25913 29191
313 13241 3163 10709 29473 5483 15101 19763 9551 21617 27527 7211 16411 28309 2
9 18731 3001 22273 17191 6361 24763 16193 2029 18181 24097 2557 11813 26017 479
12919 5749 28283 22943 9227 28697 18553 2293 11933 18719 5647 15061 20129 6113
14411 2861 11807 29287 2063 20627 28517 5171 19739 16381 7001 13681 15217 21211
19709 4799 27653 20173 5813 22159 17729 2663 19073 18401 9539 18367 11519 7307 9
7219 1567 11549 14177 1213 22067 12211 8081 12377 27127 1879 29231 27479 2801 2
967 6247 12097 29209 7109 17431 12613 3019 16189 23321 7457 25229 13397 2729 127
7 2887 10639 24421 8779 20707 25841 5717 16607 10151 3079 26953 19181 9719 1433
4 2269 29101 19963 5851 11159 21727 8311 22031 27551 4451 25219 14447 4943 28099
23063 28591 28513 8297 29611 14533 5189 13463 11329 1667 25799 12143 8821 13121
7487 19753 23339 2333 29881 18329 9791 10313 27581 6857 17657 22091 8369 17839
307 17099 15647 2917 29443 10831 7481 23993 14519 7841 10739 20347 9281 22783 2
7 13163 25981 8867 16651 28597 9433 22739 22907 7027 29033 19501 1187 13997 283
16883 19037 5333 22343 19139 3499 21499 24019 8513 23167 15331 6397 19961 2473
14939 20359 6977 13931 23041 9679 24749 25747 8209 12781 23633 8329 14327 11317
21341 22483 1427 25579 12113 1523 27631 12899 5527 18413 14009 4657 27239 11821
9883 19069 6373 27409 16657 6343 25997 21379 5501 27739 12241 4283 27283 18169
367 27733 2003 24109 23021 9767 18661 24203 2521 26203 14929 8231 19801 15773 3
7 19469 5101 23801 13781 2437 14551 27617 1931 13001 29879 5821 23609 27541 883
23911 1237 19289 19927 3967 25147 10433 9151 10627 21493 7669 22129 13859 7121
12547 1697 17609 25801 2477 16747 20021 1627 22271 24001 1951 29129 14561 29633
19577 5227 25969 23689 8291 16699 10067 9133 12757 18913 7069 16447 20693 7901 7
4937 9391 16139 24379 5011 17981 25303 2011 13399 20543 5393 19727 10993 5387 9
811 5791 20389 13171 7951 16477 11197 3461 10891 15373 9697 19819 20509 2207 503
13 1973 937 311 3917 719 113 1733 317 191 3331 773 139 3931 71
Approach
I generate a giant constraint satisfaction problem and solve it using an industrial strength satisfiability solver. Lots of magic has gone into making satisfiability solvers relatively fast (even though the problem is NP-complete in general), but fortunately I don’t have to worry about that part.
Specifically I fix the positions of the digits in a pattern designed for maximal packing of 5 digit numbers (with occasional 2, 3, and 4 digit numbers on the boundary), and add constraints to say that each number is a distinct prime.
The pattern I chose has the property that most initial digits are also final digits, so we can restrict the list of 5 digit primes to those beginning in 1, 3, 7, or 9. Mirror-reflecting the pattern would let me relax this restriction, but the reduced list of primes significantly improves memory usage and speed anyway, so I left it this way.
Code
Approach
I generate a giant constraint satisfaction problem and solve it using an industrial strength satisfiability solver. Lots of magic has gone into making satisfiability solvers relatively fast (even though the problem is NP-complete in general), but fortunately I don’t have to worry about that part.
Specifically I fix the positions of the digits in a pattern designed for maximal packing of 5 digit numbers (with occasional 2, 3, and 4 digit numbers on the boundary), and add constraints to say that each number is a distinct prime.
The pattern I chose has the property that most initial digits are also final digits, so we can restrict the list of 5 digit primes to those beginning in 1, 3, 7, or 9. Mirror-reflecting the pattern would let me relax this restriction, but the reduced list of primes significantly improves memory usage and speed anyway, so I left it this way.