Spiral

My friend sent me this:

a bunch of hardcoded print statement which spiral a counter

It pretty much forced me to write spiral.php and spiral.sh to generate this:

print('1')

print('7 8 9')
print('6 1 2')
print('5 4 3')

print('21 22 23 24 25')
print('20  7  8  9 10')
print('19  6  1  2 11')
print('18  5  4  3 12')
print('17 16 15 14 13')

print('43 44 45 46 47 48 49')
print('42 21 22 23 24 25 26')
print('41 20  7  8  9 10 27')
print('40 19  6  1  2 11 28')
print('39 18  5  4  3 12 29')
print('38 17 16 15 14 13 30')
print('37 36 35 34 33 32 31')

print('73 74 75 76 77 78 79 80 81')
print('72 43 44 45 46 47 48 49 50')
print('71 42 21 22 23 24 25 26 51')
print('70 41 20  7  8  9 10 27 52')
print('69 40 19  6  1  2 11 28 53')
print('68 39 18  5  4  3 12 29 54')
print('67 38 17 16 15 14 13 30 55')
print('66 37 36 35 34 33 32 31 56')
print('65 64 63 62 61 60 59 58 57')

print('111 112 113 114 115 116 117 118 119 120 121')
print('110  73  74  75  76  77  78  79  80  81  82')
print('109  72  43  44  45  46  47  48  49  50  83')
print('108  71  42  21  22  23  24  25  26  51  84')
print('107  70  41  20   7   8   9  10  27  52  85')
print('106  69  40  19   6   1   2  11  28  53  86')
print('105  68  39  18   5   4   3  12  29  54  87')
print('104  67  38  17  16  15  14  13  30  55  88')
print('103  66  37  36  35  34  33  32  31  56  89')
print('102  65  64  63  62  61  60  59  58  57  90')
print('101 100  99  98  97  96  95  94  93  92  91')

print('157 158 159 160 161 162 163 164 165 166 167 168 169')
print('156 111 112 113 114 115 116 117 118 119 120 121 122')
print('155 110  73  74  75  76  77  78  79  80  81  82 123')
print('154 109  72  43  44  45  46  47  48  49  50  83 124')
print('153 108  71  42  21  22  23  24  25  26  51  84 125')
print('152 107  70  41  20   7   8   9  10  27  52  85 126')
print('151 106  69  40  19   6   1   2  11  28  53  86 127')
print('150 105  68  39  18   5   4   3  12  29  54  87 128')
print('149 104  67  38  17  16  15  14  13  30  55  88 129')
print('148 103  66  37  36  35  34  33  32  31  56  89 130')
print('147 102  65  64  63  62  61  60  59  58  57  90 131')
print('146 101 100  99  98  97  96  95  94  93  92  91 132')
print('145 144 143 142 141 140 139 138 137 136 135 134 133')

print('211 212 213 214 215 216 217 218 219 220 221 222 223 224 225')
print('210 157 158 159 160 161 162 163 164 165 166 167 168 169 170')
print('209 156 111 112 113 114 115 116 117 118 119 120 121 122 171')
print('208 155 110  73  74  75  76  77  78  79  80  81  82 123 172')
print('207 154 109  72  43  44  45  46  47  48  49  50  83 124 173')
print('206 153 108  71  42  21  22  23  24  25  26  51  84 125 174')
print('205 152 107  70  41  20   7   8   9  10  27  52  85 126 175')
print('204 151 106  69  40  19   6   1   2  11  28  53  86 127 176')
print('203 150 105  68  39  18   5   4   3  12  29  54  87 128 177')
print('202 149 104  67  38  17  16  15  14  13  30  55  88 129 178')
print('201 148 103  66  37  36  35  34  33  32  31  56  89 130 179')
print('200 147 102  65  64  63  62  61  60  59  58  57  90 131 180')
print('199 146 101 100  99  98  97  96  95  94  93  92  91 132 181')
print('198 145 144 143 142 141 140 139 138 137 136 135 134 133 182')
print('197 196 195 194 193 192 191 190 189 188 187 186 185 184 183')

6 thoughts on “Spiral

  1. Fun challenge: do this in a spreadsheet. It is actually a nice example of how the functional and imperative approaches can differ radically. The shift to functional is, IMO, the hard part about doing Clojure, not the language syntax.

  2. Hey. I’ve been thinking about this and I can’t figure it out. I have no idea how you would to this in a spreadsheet. I wouldn’t even know where to start. With my program above I started with the maximum number being input by the user. With a spreadsheet I don’t know how I would program in that number. Also, I assume I would have to put the whole program in a cell, and then copy that program into every cell which I might need in the output, which will vary based on the maximum number. So, yeah. No idea about this one. If you could show me how it’s done I would be very interested to know!

  3. I haven’t actually gone through and figured it out in detail (nor am I going to), but I’ve done this sort of thing enough to know the general shape.

    Basically, you need a mathematical formula from X and Y to a distance along the spiral. I suspect that is a four-way case statement, one for each side – top, bottom, left, right. I would call the center position 0,0 for simplicity.

  4. Another interesting way to do it would be to generate a lazy, infinite sequence of state values (current number, current layer, current direction) by making an function of the prior state and iterating. Then map over the first N values to get an array of numbers. Then print.

Leave a Reply