Sunday, December 19, 2010

Cycle fun


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


I've forgotten where I saw this---perhaps it was a Project Euler problem. In any case, you can see the pattern. It's a nice (fairly simple) exercise in Python to construct a list of lists and the functions to modify it. The great thing is that once you have the inner 3 x 3 circle correct, the rest is automatic. The code for this is in cycle.py.

The real reason I'm posting about it is that it makes a fun plotting project for matplotlib, especially in combination with the prime number generator that we worked up the other day (in primes.py).

The graphic at the top of the post was obtained by modifying the array to hold n % 10 for each value, and using the matplotlib color map called "winter." I think for that one I had n = 50, so that's a 101 x 101 grid.

The prime example :) has n = 40 and uses the 'autumn' colormap. That's the one that script.py is set up to generate at the moment. Is there a pattern or is it just random?

[UPDATE: I did a bit of testing. In tracing the results for the prime plot using a small n, I discovered that matplotlib has shown the matrix in a different way than I printed it at the top of the post. Their "heatmap" goes vertically for the first step and cycles clockwise; it is both a mirror image and rotated 180 degrees from the way I did it. Sorry. But, now that I'm done, I think I can just leave it that way... ]

Zipped project files on Dropbox (here).