Stuffing primes in a box Your task is write a program or function that can fill a given rectangle with
prime numbers. The width and height of the rectangle will be the input. The
output must be a list of height strings consisting of width digits and
spaces. Each horizontal (left to right) and vertical (top to bottom) digit
sequence (delimited by space or rectangle borders) of length 2 or greater must
be a prime number. Each prime number may be used only once. Leading zeros are
not allowed. A trailing new line is optional on the output.

Example:

With input (5, 3) a valid output would be:
11 13
7  0 
 173 

which scores 11, 13, 173, 17, 103 for a total of 5 points.


Score:

The rectangle size for scoring will be 80, 60.
Each horizontal or vertical prime number of length 2 or more in the rectangle
scores one point. The answer with the most points wins. In the event of a tie, 
the earliest answer will win.

Rules:


Standard loopholes are prohibited.
Your program must not be designed for the 80, 60 size. If I suspect an
answer is optimised for this size, I reserve the right to change the rectangle
size up to 100, 100.
The primes used must be real primes (not probabilistic or pseudoprimes). Your program may compute or check numbers at run time, or have them hard coded. The method of finding the primes is not an important part of the challenge.
Your answer should include the output text and your code. If your program is too large, you may just include some core algorithm code, with a little explanation.


Edit: Clarified that real primes needed. Added maximum rectangle size.

Edit 2: Clarified what code needs to be posted.
Smalltalk, 1098 primes

First off, nice hard question - as evidenced by the lack of non-trivial responses thus far.

I had a solution cooking at work, but had to wait for the long weekend to have time to clean it up a bit. Even so, it's quite "heavy", so excuse the length of this answer - thankfully this isn't a code-golf question. There were plenty of little gotchas along the way, but I'm pretty sure it's bug-free now, though there is plenty of room for improvement and fine-tuning.

Language

My language of choice for this sort of thing is Smalltalk - the superior debugging and the ability to test-as-you-code beats anything else I know. Plus, even non-coders can read it and understand the basics of what's going on. I've limited the code below to the interesting part, because I thought the approach and results would be more interesting than the boilerplate code, but a file-out of the full code is pasted here if anyone wants to try it out (just save as a *.st and file it in from a file browser in Pharo). I used Pharo 4.0, which is FOSS and can be downloaded from pharo.org for Win/Mac/Linux. Also happy to paste the full code here, but I took the "should include the output text and your code" as a suggestion - let me know if I'm wrong.

Approach

So, my thinking was, what if you could start in the middle, stuff as many long primes as you could in the box, and work your way outwards from there? Let's start with a Box object, containing a matrix of characters, and then use a Stuffer object to try to fill it up by finding the best Insertion (another object) for each point in the matrix and for each prime (starting with the long ones). Pharo has nice Matrix and Point classes with a bunch of useful methods (though the matrix way of using row-major notation versus using x-y-coordinates of points can get a little confusing if you're not careful).

The basic steps of my algorithm are:


Create a Stuffer instance for given dimensions.
Have the stuffer initialise its box to size 5x5 (unless dimensions
are smaller).
Ask the Stuffer to stuff its box.


Iterate over a list of primes from largest to smallest.
Iterate over all points in the matrix.
Find the best spot to insert the prime, and insert if possible.

While the box can "grow" (until the target dimensions are reached),
increase it by one row/column on each side and copy the old contents
into the middle of the bigger one, then go to step 3.


Configurable:


The dimensions
The largest prime to start from
The "rating" of insertions (when choosing from multiple possible places to insert a prime)


For the matrix below, I used 80x60, primes below 20,000 (since the combined length of those is just under 10,000 characters, i.e. the maximum for a stuffed, though invalid, 100x100 box), and, after some experimentation, the following rating for each insertion:

(each accidentalPrimes size * 1) + (each numberOfOverlaps * 2)


where accidentalPrimes are the perpendicular primes "accidentally" created when inserting between neighbouring primes, and numberOfOverlaps is how many characters are overwritten (e.g. when adding a 3 at the end of 11 to insert 113). I initially had it weighted more towards accidentalPrimes, but got a few more primes in the box by doing it this way - not entirely sure why (feel free to test/tweak).

Result

| 1  3449 8    19489 8 7 9277 2 7 419  6   9533   19211    19373 4  79379 9 6 1 1|
|8861 1 7 26701 6   19219 6  97577 6 319211  84631   3 9649  8  19207 6 7 10399 9|
| 7 8 98669    19469 5 0  19301 2 6997 4   9787   19441  7 18899 9  2 47297 1 0 0|
|8839 8 7  1613 5 6  149837  0 97213 319289  6 8291  915991 431  909113 1 3382373|
| 7 7607 1 9 9 19543  1 8  3779   5743  6 19813  1946981    19423  139 9 9431 1 1|
|  6911 8933 2 9  9 1 4198013  9497   19447    1093 9 1194899 8  569 4 487749433 |
|  1 1 1 3 0 5653 19583 5  101429 4691  4 119813 9923  9 5 9  1811 47293 3533 1 1|
| 8849 8 86959 0 1  0 4 195071 7589 19429 5 1  4    3863 219763  9 2 1 9391 87833|
|  9 813613 7  719 19471   6 19727 809 7 19759 619867 6 988069 1 5 11 191 9 1 9 8|
|541 0 7 3547 1  70919 93715816451 19609 3  6  3 41  757219717 9803 9 9 9283  4 7|
|  3 1 9  1  691 6 4 177493124562997 7 1 719483  152993 8 9 6173 7 4871 0 7 15797|
|5171 373 9 1 4  318191 263 19843  19919  8 19889953 4  169639 9679 6 8887  9    |
| 8 8 1  5449 2 9 9399647333  7907  8 8 677   2 6 6 9967 101 8111 7 7 3 7 1907 1 |
| 7 67901 2 2 757 8 5 9 119759 7499 19853 19751 19913  59879 0  6469 171179 6 487|
|8747 0 9 78167 4 8 78839  6 99317  1 3 9 9 9   9  16993 7 8117 3373 9 9  8093 9 |
| 3 913457  31193 9 107 7 198439   9 19861 19697  99125471 1   16979 2819 0  9 1 |
|9 1951 0 1879 1 7 1 1 6569 853 18397  83 8 656269159547193329 97871 7 2  1 4337 |
|14831 9157 6449 0 9 9613 8 15619 148436196977821 19597 9 9 6907 8 383 16573 1  1|
|7 9 8   4518803 15647 9 941 19661 19963 149 19937  43913 2393 1433   1 1 1 9 9 8|
|3 1 3613719596827 8 2 6 5163998629 467 6271    13967 29399 51673 9 15973 87473 9|
| 9127 9  927191 19157 0 8 66938969 176597 19949 5779513 1319 6 92779 14717 3 7 1|
| 1  98849 8  9  9  8  3371771169929  169937 4 65519 79769 9 906473 58699 9 78173|
| 8    0 6 9907 682771709 9  9 3319763 947387669 479978309 8597 35393 369731 5   |
| 6211 1823 1 318691 96918839 9 19 918481 1997911733 58271 6 2 9 1  1  92219 661 |
| 7 8 7349  9 369739 7297 1 8641 4 12983    6 61991 619793 7 3 8297  15791 0 3   |
| 9 8 1 1 1 5  619 8 510589 396932999 7 5897537 997379 6   39313 4 439 69911   1 |
|1  64969 9 54139 7069 739 99639719 58693 4951 977   9 9151 8 9 6689 257 4 37199 |
|8  9 0 37529 9 4 91193  6 6 9 7  6 8 11117 3970331  11969963 661911661 727  9 3 |
|3613 0 8 5   3 2 8 9 1 499787 9137 71 19997 971939 13999 787 0 9 9  701 7 151 8 |
|7 8 6961 97607 1 15569 9 8 1 1 1171 99929 99733 1759 9817931 3 8941  193  968819|
|9 8 0 6    0 99767 8 769 88969 9859 979871393 1733 9377113 95111 5  8 2 94033  1|
| 9041 6 14519   1063 6 937 8 8 8999 92993171 11519 313 89417 7 317 5233 4 3  9 8|
|433  8179 6113 8971  3191 7559 17 63313 939793919 971 198769 5693 9 7 14657  6 7|
| 9  1  725189 1 7591139 9 93911 3919 9257 33739 9293 29 1973 2  9161 9511  17239|
|349 87151 99349 57529 7 36637 6359 81999119371933 4 98573 7549 6317 6619 8 9 3 3|
| 3  8 99371 8 8 374718269 1144935789717977  83177 9 233  1499 8 1571 4 8 6803 9 |
| 9 15641  3 1607  3 8 761 393388937 9    3967  9 9719 13997 1259 7 9639079 0 443|
|  1 9 2 14969 1109 8313931 8  198174119963 5 997 9 9 991 8 1217 52579  1 3911 1 |
|5281 277    1979 32909 9 986983 19991  61399991 127997 9 82939 907 571847 4 9 3 |
|  7 8  9 617 8 4 21613 2 95792383  919979 4951997317 93719 4 641379379 1 12487  |
|1 9 66959 8  8 4 3339313613 5119963  189967 359 337361 25933 83891   8 8191 7  1|
|8 7 8 61516883 1619 839 799859 2 7 19927 5 19777 6737 9769 37799 9 19079 0 9 1 9|
|57331 2 4 9 1 1  7691 9697 61 19577 6 9949 9 9 31979399 1 1 8737 509 1 49871 8 8|
|9  1 6397 109 99133 3 313888397 59419699  1986133  8387 9 988319 5 47237 7 8 9 0|
|3  62131 1  38629 8699 9 8599 953 397   736903  1991933 877 3 4799 1 1     77171|
| 1 8 8 9091 7 86491  627673 593 1  19853377 9781  8  3 76919  7  49739 1511  9  |
| 8 76991 0 938747 9 924629743 193873 2 699823 8719801 907871 19181   4 9 9 93371|
|6959 7 88867 6 7757 792383  8 9  0  1981337 3947  3 3917 2089979 9 92831 0 4 2 8|
| 4  5953 1 3739   69473 7 19709 99016787  379 9619759 196991 6 56393 9 3 82373 9|
|273613  1163  13883 919531 8  7433 6337 8893 6917   9 9 1693 8 0 7 709 93199 9 1|
|631 97159 8  181 9 6   0 0 7819753 673  9    9  9901 19699   18119 7  1 1 111779|
|9 8 0 9 0 2 7919 4419559 98179  6  9  19553 9851 4 9 9067 9857 4957 14939 9397  |
| 8753 0 3631 4 4 18973 9 7  89923 19793 1 8  3491954199497 53 2969  9 0 0 0 0 6 |
|1 9 1 5 1    5 24337 9  19603 7 9 3  4 195312373619977 9 119489 8 96293 643 0 4 |
|8 3   18461  7577 1 19541 8 3929 19463 6  3 49937 3 9 98297 71347 3 4 7 9 1 9 1 |
|87541   9 92707 9817  2 58237 1 4 6  919423 5    19403 77383 194179 9601 1 1 1 1|
|6 9 38923 4 261631 61937389 19387 390739 9 19319 6 495617 9 5  9  7  93979 8 8 8|
|9 449 5 4658147899073 7  3   60293 5  7 8231 2641930937 7 1950139511 6 0 26993 7|
|  0 0 2 34789739 1 3619501   61930959767 7 19237 7 9 9829   7    7   27773 5 7 5|
|823 7211 7 3 3 919231     9631 337 1  3    7 9 3      1 3943 19141 653 3 15971 7|


This matrix contains 1098 primes, which is a "load" of about 70%.

These are the primes (box primesUsed asSortedCollection printString):

11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 107 109 113 127 131 137 139 149 151 163 167 173 179 181 191 193 197 199 233 257 263 269 271 277 283 293 307 311 313 317 331 337 347 349 359 367 373 379 383 389 397 419 431 433 439 443 449 467 479 487 491 499 509 541 547 557 563 569 571 577 587 593 599 601 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 1063 1093 1109 1117 1153 1163 1171 1181 1217 1259 1319 1361 1399 1433 1499 1511 1571 1607 1613 1619 1637 1663 1667 1693 1697 1699 1733 1759 1811 1823 1879 1907 1913 1933 1949 1951 1973 1979 1987 1993 1997 1999 2393 2459 2617 2819 2927 2939 2969 3191 3229 3343 3373 3389 3449 3533 3539 3547 3613 3617 3631 3643 3733 3739 3769 3779 3793 3797 3821 3863 3911 3917 3919 3929 3931 3943 3947 3967 3989 4337 4409 4493 4691 4799 4871 4951 4957 4967 4999 5039 5099 5101 5171 5233 5281 5347 5399 5449 5479 5653 5693 5743 5939 5953 6079 6113 6131 6173 6197 6199 6211 6271 6317 6337 6359 6397 6449 6469 6569 6571 6599 6607 6619 6653 6661 6689 6737 6779 6781 6793 6803 6823 6829 6899 6907 6911 6917 6949 6959 6961 6967 6977 6997 7069 7079 7109 7193 7211 7219 7229 7297 7307 7349 7393 7433 7499 7549 7559 7577 7589 7607 7621 7691 7727 7757 7883 7907 7919 7949 7951 8093 8111 8117 8171 8179 8191 8219 8231 8291 8293 8297 8387 8419 8537 8539 8563 8597 8599 8641 8647 8681 8689 8693 8699 8719 8737 8747 8753 8779 8831 8839 8849 8861 8863 8887 8893 8929 8933 8941 8951 8963 8969 8971 8999 9007 9041 9067 9091 9109 9127 9137 9151 9157 9161 9173 9181 9187 9199 9257 9277 9281 9283 9293 9371 9377 9391 9397 9413 9419 9421 9431 9437 9461 9463 9467 9473 9479 9491 9497 9511 9521 9533 9539 9547 9551 9587 9601 9613 9619 9623 9629 9631 9643 9649 9661 9677 9679 9689 9697 9719 9721 9733 9739 9743 9749 9767 9769 9781 9787 9791 9803 9811 9817 9829 9833 9839 9851 9857 9859 9871 9883 9887 9901 9907 9923 9929 9931 9941 9949 9967 9973 10399 10979 11117 11519 11939 11959 11981 12487 12983 13313 13339 13463 13499 13763 13877 13883 13907 13931 13963 13967 13997 13999 14519 14657 14717 14831 14939 14969 15091 15569 15619 15641 15647 15791 15797 15971 15973 16573 16699 16937 16979 16993 17239 17333 17519 17761 17891 18119 18371 18379 18397 18461 18593 18691 18719 18757 18773 18787 18803 18839 18859 18869 18899 18911 18913 18917 18919 18959 18973 18979 19001 19009 19013 19031 19037 19051 19069 19073 19079 19081 19087 19121 19139 19141 19157 19163 19181 19183 19207 19211 19213 19219 19231 19237 19249 19259 19267 19273 19289 19301 19309 19319 19333 19373 19379 19381 19387 19391 19403 19417 19421 19423 19427 19429 19433 19441 19447 19457 19463 19469 19471 19477 19483 19489 19501 19507 19531 19541 19543 19553 19559 19571 19577 19583 19597 19603 19609 19661 19681 19687 19697 19699 19709 19717 19727 19739 19751 19753 19759 19763 19777 19793 19801 19813 19819 19841 19843 19853 19861 19867 19889 19891 19913 19919 19927 19937 19949 19961 19963 19973 19979 19991 19993 19997 21613 24239 24337 25933 26449 26701 26959 26993 27773 27919 29399 31193 32909 33739 35393 36637 36761 37199 37529 37799 38333 38629 38839 38861 38923 39293 39313 39733 43913 43943 45281 47237 47293 47297 47933 47939 48311 48761 49123 49199 49739 49871 49937 49999 51193 51673 52379 52579 54139 55313 56393 56983 57173 57331 57529 58237 58271 58693 58699 59743 59879 60293 61979 61991 62131 62873 63313 64969 65519 66491 66959 67339 67901 67993 69337 69473 69623 69697 69911 69941 70919 71347 71777 71933 75347 76163 76733 76919 76991 77171 77213 77383 77591 78167 78173 78839 78941 79379 79613 79631 79769 79967 80933 81919 82373 82939 83177 83891 84631 86491 86959 87151 87473 87541 87833 88499 88867 88951 88969 89413 89417 89519 89923 91193 91813 91997 92219 92593 92647 92707 92779 92831 93199 93371 93719 93911 93913 93971 93979 94033 94397 94399 95111 96233 96293 96587 96911 96959 96979 97159 97213 97577 97607 97787 97871 97919 98179 98297 98299 98573 98669 98837 98849 99133 99233 99317 99349 99371 99497 99733 99767 99907 99929 101429 111149 111779 119489 119759 119813 127997 149837 152993 167747 168863 169639 169937 171179 176597 179099 179111 183797 187973 188693 189473 189967 190811 191837 193813 193873 194179 195071 195319 196817 196871 196991 197273 197599 198013 198017 198439 198673 198769 199373 199679 199739 219763 261631 273613 277793 316879 318191 318691 319031 319069 319211 319289 319427 319727 319973 337361 369731 369739 390739 391753 414347 419801 439883 495617 499787 510589 519031 571847 601189 615749 616367 618719 619543 619583 619753 619763 619793 619813 619867 627673 639631 669923 698339 699823 716987 719009 719441 719483 719801 719813 725189 736903 792383 796553 799859 809339 813613 815569 819373 819457 829733 906473 907871 909113 912797 913457 914813 915641 915991 917159 918481 918679 918793 919183 919231 919381 919421 919423 919531 919559 919913 919979 927191 935639 938747 939439 943913 962971 968819 971939 976777 986983 988069 988319 991733 997319 997379 1194899 1518379 1678739 1691189 1867993 1899907 1903703 1923151 1942771 1946981 1950149 1981337 1986133 1988999 1991933 2089979 2919031 3110651 3190871 3193913 3197633 3319763 3382373 3619501 3970331 4198013 4419559 4518803 5119963 5195077 5319289 5619139 5779513 5897537 6119801 6129587 6189137 6194891 6198139 6649871 7219427 7591139 7819753 7937939 7982969 8019727 8313931 8419381 8719801 8783371 9119909 9189797 9323173 9377113 9619759 9639079 9679799 9717317 9721301 9721991 9817931 9911789 9923369 9967759 11969963 15619867 16981331 19669931 19717099 19793479 19853377 19889953 21968773 27519469 31979399 34789739 40699733 59419699 61399991 61516883 61897937 61937389 63519427 66938969 69719801 74969767 91938191 92993171 93369931 95792383 96918839 99016787 99125471 99639719 126919421 179319979 199733981 199739717 313888397 342719753 374718269 393388937 396932999 439314797 479978309 487749433 577519913 613799299 631819381 641379379 661911661 682771709 689194217 757219717 924629743 936195479 939793919 947387669 979871393 991579637 1903149343 1921376857 1945706473 1950139511 1960317529 1997911733 2641930937 3339313613 4951997317 4952523041 5163998629 7017198679 7981707577 9013119991 9119993297 9399647333 9945519919 18919323977 19031318947 19919788793 31879359403 61930959767 61960309907 91734751987 92774649739 93715816451 95479219889 98371979359 99551395387 191830198139 198012194899 198017187931 198174119963 213963819763 419801418947 819853319867 1937327699987 3189735598139 3371771169929 3491954199497 3613719596827 4658147899073 81999119371933 148436196977821 177493124562997 195312373619977 1144935789717977 656269159547193329 735547997331319937 11572771955941937959


As you can see, some of the "accidental" primes get quite long... 20 digits for the longest one. :-)

Details

To understand how it works, here is the output from doing the first few steps - inserting 19997 first (if you look in the middle of the big matrix, you'll see 19997 there as well), then down the list where there's space in the 5x5 box (&gt; means insert going right, v means going down; anything in {} are accidentally added primes:

Insertion at: (1@1) &gt; {} of 19997
Box 5x5 (1 prime, load: 20%):
|19997|
|     |
|     |
|     |
|     |

Insertion at: (1@1) v {} of 19993
Box 5x5 (2 primes, load: 36%):
|19997|
|9    |
|9    |
|9    |
|3    |

Insertion at: (3@1) v {} of 9973
Box 5x5 (3 primes, load: 48%):
|19997|
|9 9  |
|9 7  |
|9 3  |
|3    |

Insertion at: (1@4) &gt; {} of 9931
Box 5x5 (4 primes, load: 56%):
|19997|
|9 9  |
|9 7  |
|9931 |
|3    |

Insertion at: (1@3) &gt; {89, 11} of 9871
Box 5x5 (7 primes, load: 64%):
|19997|
|9 9  |
|9871 |
|9931 |
|3    |

Insertion at: (5@1) v {98713, 99317} of 7937
Box 5x5 (10 primes, load: 76%):
|19997|
|9 9 9|
|98713|
|99317|
|3    |

Insertion at: (4@3) v {} of 113
Box 5x5 (11 primes, load: 80%):
|19997|
|9 9 9|
|98713|
|99317|
|3  3 |

11 old primes: 19997, 19993, 9973, 9931, 9871, 89, 11, 7937, 98713, 99317, 113
8 new primes: 19997, 98713, 99317, 19993, 89, 9973, 113, 7937
Insertion at: (4@1) v {} of 19973
Box 7x7 (9 primes, load: 42%):
|   1   |
| 19997 |
| 9 9 9 |
| 98713 |
| 99317 |
| 3  3  |
|       |


In the insertion immediately above, the box becomes a 7x7, and is filled with the 5x5 contents before the next insertion happens. I also re-calculate the primes used at this point, because some are "freed up" due to overwriting (11 was inserted, but then overwritten by 113).

Code

Here are the most relevant parts of the code:

Stuffer class

Class side

forDimensions: aPoint
    ^self new
        width: aPoint x;
        height: aPoint y;
        initializeBox;
        yourself


Instance side

initializeBox
    box := Box width: (width min: 5) height: (height min: 5)

stuffBox
    self stuffCurrentBox.
    [self canGrow] whileTrue: [
        box growMatrixByOneUpTo: width @ height.
        self stuffCurrentBox
        ]

canGrow
    ^width &gt; box width or: [height &gt; box height]

stuffCurrentBox
    self class primes reverseDo: [:eachPrime |
        | string insertion |
        (box primesUsed includes: eachPrime) ifFalse: [
            string := eachPrime asString.
            (insertion := box bestInsertionFor: string) ifNotNil: [
                Transcript crShow: insertion printString, ' of ', string.
                box doInsertion: insertion of: eachPrime.
                Transcript crShow: box
                ]
            ]
        ].
    box updatePrimesUsed.


Box class

Instance side

bestInsertionFor: aString
    | insertions |
    insertions := OrderedCollection new.
    self pointsDo: [:eachPoint |
        insertions
            addIfNotNil: (self insertionOf: aString at: eachPoint isHorizontal: true);
            addIfNotNil: (self insertionOf: aString at: eachPoint isHorizontal: false)
        ].
    insertions removeAllSuchThat: [:each |
        self isInsertion: each invalidWith: aString asInteger
        ].
    ^insertions detectMax: [:each |
        (each accidentalPrimes size * 1) + (each numberOfOverlaps * 2)
        ]

insertionOf: aString at: aPoint isHorizontal: isHorizontal
    | point insertion |
    point := aPoint.
    (self canFitEndsAt: point length: aString size isHorizontal: isHorizontal)
        ifFalse: [^nil].
    (self isAlreadyThere: aString at: aPoint isHorizontal: isHorizontal)
        ifTrue: [^nil].
    insertion := Insertion at: point isHorizontal: isHorizontal.
    1 to: aString size do: [:eachIndex |
        | eachCharacter |
        eachCharacter := aString at: eachIndex.
        (self canDoInsertion: insertion of: eachCharacter at: point)
            ifTrue: [
                (self at: point) == eachCharacter
                    ifTrue: [insertion increaseOverlaps].
                point := point + (self offset: isHorizontal)
                ]
            ifFalse: [^nil]
        ].
    ^insertion

isInsertion: anInsertion invalidWith: aPrime
    | otherPrimes |
    otherPrimes := anInsertion accidentalPrimes.
    ^(primesUsed includesAny: otherPrimes)
        or: [(otherPrimes includes: aPrime)
        or: [otherPrimes copy removeDuplicates size &lt; otherPrimes size]]

canDoInsertion: anInsertion of: aCharacter at: aPoint
    "Answer whether anInsertion of aCharacter is valid at aPoint. If so,
    also update anInsertion with any accidental insertions."

    | perpendicularSequence offset front back |
    (self at: aPoint) == aCharacter
        ifTrue: [^true].
    (self isFreeAt: aPoint)
        ifFalse: [^false].
    perpendicularSequence := OrderedCollection with: aCharacter.
    offset := self offset: anInsertion isHorizontal not.
    front := aPoint - offset.
    back := aPoint + offset.
    [self isFreeOrBorder: front] whileFalse: [
        perpendicularSequence addFirst: (self at: front).
        front := front - offset
        ].
    [self isFreeOrBorder: back] whileFalse: [
        perpendicularSequence addLast: (self at: back).
        back := back + offset
        ].
    ^perpendicularSequence size == 1
        or: [
            | string number |
            string := String withAll: perpendicularSequence.
            number := string asInteger.
            (string first ~~ $0 and: [number isPrime])
                ifTrue: [anInsertion addAccidentalPrime: number. true]
                ifFalse: [false]
            ]

doInsertion: anInsertion of: aPrime
    self
        insert: aPrime printString
        at: anInsertion point
        horizontalIf: anInsertion isHorizontal.
    primesUsed
        add: aPrime;
        addAll: anInsertion accidentalPrimes

growMatrixByOneUpTo: aDimension
    | smallMatrix rowOffset columnOffset |
    smallMatrix := matrix copy.
    matrix := Matrix
        rows: (self height + 2 min: aDimension y)
        columns: (self width + 2 min: aDimension x)
        element: Character space.
    rowOffset := (self height - smallMatrix numberOfRows) min: 1.
    columnOffset := (self width - smallMatrix numberOfColumns) min: 1.
    self copy: smallMatrix to: (1 + columnOffset) @ (1 + rowOffset)

copy: aMatrix to: aPoint
    matrix
        atRows: aPoint y
        to: aPoint y + aMatrix numberOfRows - 1
        columns: aPoint x
        to: aPoint x + aMatrix numberOfColumns - 1
        put: aMatrix


Collection

Oh, and I added one method to the instance side of Collection, just for convenience:

addIfNotNil: anElement
    anElement ifNotNil: [self add: anElement]


Running it

To run my code, all I need to do is select the following code in a workspace ("playground" in Pharo) or in any text pane, and "do it" from the context menu:

(Stuffer forDimensions: 80@60)
    stuffBox


The rest of the code is relatively boring.

As I said, room for improvement, but at 80x60, each run takes over 3 minutes on my machine. Clearly, performance hasn't been my concern, but there are a LOT of primes flying around. This sure has been an interesting challenge. :-)