[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> return 2 * n * (1+2*n)</tt><tt></tt>
<p><tt>def getNum(x, y):</tt>
<br><tt> if abs(x) <= abs(y): return a(-y) + abs(y-x)</tt>
<br><tt> else: return a(-x) - abs(y-x)</tt>
<br><tt> </tt>
<br><tt>def primes(n):</tt>
<br><tt> primes = [2]</tt>
<br><tt> i = 3</tt>
<br><tt> while len(primes) < n:</tt>
<br><tt> for p in primes:</tt>
<br><tt>
if i%p == 0 or p*p > i: break</tt>
<br><tt> if i%p <> 0:</tt>
<br><tt>
primes.append(i)</tt>
<br><tt> i = i+2</tt>
<br><tt> return primes</tt><tt></tt>
<p><tt>primelist = primes(11*11)</tt>
<br><tt>for y in range(5, -5, -1):</tt>
<br><tt> for x in range(-5, 5):</tt>
<br><tt> print "%5d" % primelist[getNum(x,
y)],</tt>
<br><tt> print</tt><tt></tt>
<p>which ouputs:<tt></tt>
<p><tt> 547 541 523 521
509 503 499 491 487
479</tt>
<br><tt> 557 313 311 307
293 283 281 277 271
269</tt>
<br><tt> 563 317 157 151
149 139 137 131 127
263</tt>
<br><tt> 569 331 163 59
53 47 43 41
113 257</tt>
<br><tt> 571 337 167 61
11 7 5
37 109 251</tt>
<br><tt> 577 347 173 67
13 2 3
31 107 241</tt>
<br><tt> 587 349 179 71
17 19 23 29
103 239</tt>
<br><tt> 593 353 181 73
79 83 89 97
101 233</tt>
<br><tt> 599 359 191 193
197 199 211 223 227
229</tt>
<br><tt> 601 367 373 379
383 389 397 401 409
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. Take a look!
<p>###
<br>>>> def a(n): return 2 * n * (1+2*n)
<br>...
<br>>>> def getNum(x, y):
<br>... if abs(x) <= abs(y): return a(-y) +
abs(y-x)
<br>... else: return a(-x) - abs(y-x)
<br>...
<br>>>> for y in range(5, -5, -1):
<br>... for x in range(-5, 5):
<br>... print "%5d" % getNum(x,
y),
<br>... print
<br>...
<br> 100 99 98
97 96 95 94
93 92 91
<br> 101 64 63
62 61 60 59
58 57 56
<br> 102 65 36
35 34 33 32
31 30 55
<br> 103 66 37
16 15 14 13
12 29 54
<br> 104 67 38
17 4 3
2 11 28 53
<br> 105 68 39
18 5 0
1 10 27 52
<br> 106 69 40
19 6 7
8 9 26 51
<br> 107 70 41
20 21 22 23
24 25 50
<br> 108 71 42
43 44 45 46
47 48 49
<br> 109 72 73
74 75 76 77
78 79 80
<br>###
<p>There is a Mathgod, after all. *grin*
<p>Actually, I finally did the generating function way of deriving the
<br>function a(n). 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--