[Tutor] or better ...
Gregor Lingl
glingl@aon.at
Tue, 10 Apr 2001 00:16:03 +0200
--------------BEA9CD40CEA971B83AF0BCEF
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, -6, -1):
for x in range(-5, 6):
print "%5d" % primelist[getNum(x, y)],
print
which ouputs all the first 121 primes: (Note the changes in the ranges above,
sorry)
547 541 523 521 509 503 499 491 487 479 467
557 313 311 307 293 283 281 277 271 269 463
563 317 157 151 149 139 137 131 127 263 461
569 331 163 59 53 47 43 41 113 257 457
571 337 167 61 11 7 5 37 109 251 449
577 347 173 67 13 2 3 31 107 241 443
587 349 179 71 17 19 23 29 103 239 439
593 353 181 73 79 83 89 97 101 233 433
599 359 191 193 197 199 211 223 227 229 431
601 367 373 379 383 389 397 401 409 419 421
607 613 617 619 631 641 643 647 653 659 661
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.
--------------BEA9CD40CEA971B83AF0BCEF
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:<tt></tt>
<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, -6, -1):</tt>
<br><tt> for x in range(-5, 6):</tt>
<br><tt> print "%5d" % primelist[getNum(x,
y)],</tt>
<br><tt> print</tt>
<p>which ouputs all the first 121 primes: (Note the changes in the ranges
above, sorry)
<p><tt> 547 541 523 521
509 503 499 491 487
479 467</tt>
<br><tt> 557 313 311 307
293 283 281 277 271
269 463</tt>
<br><tt> 563 317 157 151
149 139 137 131 127
263 461</tt>
<br><tt> 569 331 163 59
53 47 43 41
113 257 457</tt>
<br><tt> 571 337 167 61
11 7 5
37 109 251 449</tt>
<br><tt> 577 347 173 67
13 2 3
31 107 241 443</tt>
<br><tt> 587 349 179 71
17 19 23 29
103 239 439</tt>
<br><tt> 593 353 181 73
79 83 89 97
101 233 433</tt>
<br><tt> 599 359 191 193
197 199 211 223 227
229 431</tt>
<br><tt> 601 367 373 379
383 389 397 401 409
419 421</tt>
<br><tt> 607 613 617 619
631 641 643 647 653
659 661</tt>
<br>
<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>
--------------BEA9CD40CEA971B83AF0BCEF--