# [Tutor] Primes on a two-dimensional plane in the form of arectangular spiral

Gregor Lingl glingl@aon.at
Tue, 10 Apr 2001 00:04:56 +0200

```Dies ist eine mehrteilige Nachricht im MIME-Format.
--------------5EA0C941FE15032E318423AD
Content-Type: multipart/alternative;
boundary="------------B18CC9CDB79AB7DAD05C9D6F"

--------------B18CC9CDB79AB7DAD05C9D6F
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Very well done!
After all, we can have a script with 20 lines of code,
as Julieta wanted:

def a(n):
return 2 * n * (1+2*n)

def getNum(x, y):
if abs(x) <= abs(y): return a(-y) + abs(y-x)
else: return a(-x) - abs(y-x)

def primes(n):
primes = [2]
i = 3
while len(primes) < n:
for p in primes:
if i%p == 0 or p*p > i: break
if i%p <> 0:
primes.append(i)
i = i+2
return primes

primelist = primes(11*11)
for y in range(5, -5, -1):
for x in range(-5, 5):
print "%5d" % primelist[getNum(x, y)],
print

which ouputs:

547   541   523   521   509   503   499   491   487   479
557   313   311   307   293   283   281   277   271   269
563   317   157   151   149   139   137   131   127   263
569   331   163    59    53    47    43    41   113   257
571   337   167    61    11     7     5    37   109   251
577   347   173    67    13     2     3    31   107   241
587   349   179    71    17    19    23    29   103   239
593   353   181    73    79    83    89    97   101   233
599   359   191   193   197   199   211   223   227   229
601   367   373   379   383   389   397   401   409   419

Thanks for your ideas

Gregor L.

P.S.: concerning the math: post it!

Daniel Yoo schrieb:

> Here's one more experiment after I played around with the spiraling
> function.  Take a look!
>
> ###
> >>> def a(n): return 2 * n * (1+2*n)
> ...
> >>> def getNum(x, y):
> ...     if abs(x) <= abs(y): return a(-y) + abs(y-x)
> ...     else: return a(-x) - abs(y-x)
> ...
> >>> for y in range(5, -5, -1):
> ...     for x in range(-5, 5):
> ...         print "%5d" % getNum(x, y),
> ...     print
> ...
>   100    99    98    97    96    95    94    93    92    91
>   101    64    63    62    61    60    59    58    57    56
>   102    65    36    35    34    33    32    31    30    55
>   103    66    37    16    15    14    13    12    29    54
>   104    67    38    17     4     3     2    11    28    53
>   105    68    39    18     5     0     1    10    27    52
>   106    69    40    19     6     7     8     9    26    51
>   107    70    41    20    21    22    23    24    25    50
>   108    71    42    43    44    45    46    47    48    49
>   109    72    73    74    75    76    77    78    79    80
> ###
>
> There is a Mathgod, after all.  *grin*
>
> Actually, I finally did the generating function way of deriving the
> function a(n).  If anyone's interested, I'll be glad to post a .pdf to the
> math.
>
> Ok, this was overkill, but I had fun doing it.

--------------B18CC9CDB79AB7DAD05C9D6F
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Very well done!
<br>After all, we can have a script with 20 lines of code,
<br>as Julieta wanted:
<p><tt>def a(n):</tt>
<br><tt>&nbsp;&nbsp;&nbsp; return 2 * n * (1+2*n)</tt><tt></tt>
<p><tt>def getNum(x, y):</tt>
<br><tt>&nbsp;&nbsp;&nbsp; if abs(x) &lt;= abs(y): return a(-y) + abs(y-x)</tt>
<br><tt>&nbsp;&nbsp;&nbsp; else: return a(-x) - abs(y-x)</tt>
<br><tt>&nbsp;</tt>
<br><tt>def primes(n):</tt>
<br><tt>&nbsp;&nbsp;&nbsp; primes = [2]</tt>
<br><tt>&nbsp;&nbsp;&nbsp; i = 3</tt>
<br><tt>&nbsp;&nbsp;&nbsp; while len(primes) &lt; n:</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for p in primes:</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if i%p == 0 or p*p > i: break</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if i%p &lt;> 0:</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
primes.append(i)</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i = i+2</tt>
<br><tt>&nbsp;&nbsp;&nbsp; return primes</tt><tt></tt>
<p><tt>primelist = primes(11*11)</tt>
<br><tt>for y in range(5, -5, -1):</tt>
<br><tt>&nbsp;&nbsp;&nbsp; for x in range(-5, 5):</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "%5d" % primelist[getNum(x,
y)],</tt>
<br><tt>&nbsp;&nbsp;&nbsp; print</tt><tt></tt>
<p>which ouputs:<tt></tt>
<p><tt>&nbsp; 547&nbsp;&nbsp; 541&nbsp;&nbsp; 523&nbsp;&nbsp; 521&nbsp;&nbsp;
509&nbsp;&nbsp; 503&nbsp;&nbsp; 499&nbsp;&nbsp; 491&nbsp;&nbsp; 487&nbsp;&nbsp;
479</tt>
<br><tt>&nbsp; 557&nbsp;&nbsp; 313&nbsp;&nbsp; 311&nbsp;&nbsp; 307&nbsp;&nbsp;
293&nbsp;&nbsp; 283&nbsp;&nbsp; 281&nbsp;&nbsp; 277&nbsp;&nbsp; 271&nbsp;&nbsp;
269</tt>
<br><tt>&nbsp; 563&nbsp;&nbsp; 317&nbsp;&nbsp; 157&nbsp;&nbsp; 151&nbsp;&nbsp;
149&nbsp;&nbsp; 139&nbsp;&nbsp; 137&nbsp;&nbsp; 131&nbsp;&nbsp; 127&nbsp;&nbsp;
263</tt>
<br><tt>&nbsp; 569&nbsp;&nbsp; 331&nbsp;&nbsp; 163&nbsp;&nbsp;&nbsp; 59&nbsp;&nbsp;&nbsp;
53&nbsp;&nbsp;&nbsp; 47&nbsp;&nbsp;&nbsp; 43&nbsp;&nbsp;&nbsp; 41&nbsp;&nbsp;
113&nbsp;&nbsp; 257</tt>
<br><tt>&nbsp; 571&nbsp;&nbsp; 337&nbsp;&nbsp; 167&nbsp;&nbsp;&nbsp; 61&nbsp;&nbsp;&nbsp;
11&nbsp;&nbsp;&nbsp;&nbsp; 7&nbsp;&nbsp;&nbsp;&nbsp; 5&nbsp;&nbsp;&nbsp;
37&nbsp;&nbsp; 109&nbsp;&nbsp; 251</tt>
<br><tt>&nbsp; 577&nbsp;&nbsp; 347&nbsp;&nbsp; 173&nbsp;&nbsp;&nbsp; 67&nbsp;&nbsp;&nbsp;
13&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp;
31&nbsp;&nbsp; 107&nbsp;&nbsp; 241</tt>
<br><tt>&nbsp; 587&nbsp;&nbsp; 349&nbsp;&nbsp; 179&nbsp;&nbsp;&nbsp; 71&nbsp;&nbsp;&nbsp;
17&nbsp;&nbsp;&nbsp; 19&nbsp;&nbsp;&nbsp; 23&nbsp;&nbsp;&nbsp; 29&nbsp;&nbsp;
103&nbsp;&nbsp; 239</tt>
<br><tt>&nbsp; 593&nbsp;&nbsp; 353&nbsp;&nbsp; 181&nbsp;&nbsp;&nbsp; 73&nbsp;&nbsp;&nbsp;
79&nbsp;&nbsp;&nbsp; 83&nbsp;&nbsp;&nbsp; 89&nbsp;&nbsp;&nbsp; 97&nbsp;&nbsp;
101&nbsp;&nbsp; 233</tt>
<br><tt>&nbsp; 599&nbsp;&nbsp; 359&nbsp;&nbsp; 191&nbsp;&nbsp; 193&nbsp;&nbsp;
197&nbsp;&nbsp; 199&nbsp;&nbsp; 211&nbsp;&nbsp; 223&nbsp;&nbsp; 227&nbsp;&nbsp;
229</tt>
<br><tt>&nbsp; 601&nbsp;&nbsp; 367&nbsp;&nbsp; 373&nbsp;&nbsp; 379&nbsp;&nbsp;
383&nbsp;&nbsp; 389&nbsp;&nbsp; 397&nbsp;&nbsp; 401&nbsp;&nbsp; 409&nbsp;&nbsp;
419</tt><tt></tt>
<p>Thanks for your ideas
<p>Gregor L.<tt></tt>
<p>P.S.: concerning the math: post it!
<p>Daniel Yoo schrieb:
<blockquote TYPE=CITE>Here's one more experiment after I played around
with the spiraling
<br>function.&nbsp; Take a look!
<p>###
<br>>>> def a(n): return 2 * n * (1+2*n)
<br>...
<br>>>> def getNum(x, y):
<br>...&nbsp;&nbsp;&nbsp;&nbsp; if abs(x) &lt;= abs(y): return a(-y) +
abs(y-x)
<br>...&nbsp;&nbsp;&nbsp;&nbsp; else: return a(-x) - abs(y-x)
<br>...
<br>>>> for y in range(5, -5, -1):
<br>...&nbsp;&nbsp;&nbsp;&nbsp; for x in range(-5, 5):
<br>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "%5d" % getNum(x,
y),
<br>...&nbsp;&nbsp;&nbsp;&nbsp; print
<br>...
<br>&nbsp; 100&nbsp;&nbsp;&nbsp; 99&nbsp;&nbsp;&nbsp; 98&nbsp;&nbsp;&nbsp;
97&nbsp;&nbsp;&nbsp; 96&nbsp;&nbsp;&nbsp; 95&nbsp;&nbsp;&nbsp; 94&nbsp;&nbsp;&nbsp;
93&nbsp;&nbsp;&nbsp; 92&nbsp;&nbsp;&nbsp; 91
<br>&nbsp; 101&nbsp;&nbsp;&nbsp; 64&nbsp;&nbsp;&nbsp; 63&nbsp;&nbsp;&nbsp;
62&nbsp;&nbsp;&nbsp; 61&nbsp;&nbsp;&nbsp; 60&nbsp;&nbsp;&nbsp; 59&nbsp;&nbsp;&nbsp;
58&nbsp;&nbsp;&nbsp; 57&nbsp;&nbsp;&nbsp; 56
<br>&nbsp; 102&nbsp;&nbsp;&nbsp; 65&nbsp;&nbsp;&nbsp; 36&nbsp;&nbsp;&nbsp;
35&nbsp;&nbsp;&nbsp; 34&nbsp;&nbsp;&nbsp; 33&nbsp;&nbsp;&nbsp; 32&nbsp;&nbsp;&nbsp;
31&nbsp;&nbsp;&nbsp; 30&nbsp;&nbsp;&nbsp; 55
<br>&nbsp; 103&nbsp;&nbsp;&nbsp; 66&nbsp;&nbsp;&nbsp; 37&nbsp;&nbsp;&nbsp;
16&nbsp;&nbsp;&nbsp; 15&nbsp;&nbsp;&nbsp; 14&nbsp;&nbsp;&nbsp; 13&nbsp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sp;&nbsp;
47&nbsp;&nbsp;&nbsp; 48&nbsp;&nbsp;&nbsp; 49
<br>&nbsp; 109&nbsp;&nbsp;&nbsp; 72&nbsp;&nbsp;&nbsp; 73&nbsp;&nbsp;&nbsp;
74&nbsp;&nbsp;&nbsp; 75&nbsp;&nbsp;&nbsp; 76&nbsp;&nbsp;&nbsp; 77&nbsp;&nbsp;&nbsp;
78&nbsp;&nbsp;&nbsp; 79&nbsp;&nbsp;&nbsp; 80
<br>###
<p>There is a Mathgod, after all.&nbsp; *grin*
<p>Actually, I finally did the generating function way of deriving the
<br>function a(n).&nbsp; If anyone's interested, I'll be glad to post a
.pdf to the
<br>math.
<p>Ok, this was overkill, but I had fun doing it.</blockquote>
</html>

--------------B18CC9CDB79AB7DAD05C9D6F--

--------------5EA0C941FE15032E318423AD
Content-Type: text/plain; charset=us-ascii;
name="primespiral.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="primespiral.py"

def a(n):
return 2 * n * (1+2*n)

def getNum(x, y):
if abs(x) <= abs(y): return a(-y) + abs(y-x)
else: return a(-x) - abs(y-x)

def primes(n):
primes = [2]
i = 3
while len(primes) < n:
for p in primes:
if i%p == 0 or p*p > i: break
if i%p <> 0:
primes.append(i)
i = i+2
return primes

primelist = primes(11*11)
for y in range(5, -5, -1):
for x in range(-5, 5):
print "%5d" % primelist[getNum(x, y)],
print

--------------5EA0C941FE15032E318423AD--

```