Ulam spiral (for primes): Difference between revisions

Added 11l
(Added Wren)
(Added 11l)
Line 113:
* MathWorld™ entry:   [http://mathworld.wolfram.com/PrimeSpiral.html Prime Spiral]
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<lang 11l>F cell(n, =x, =y, start = 1)
V d = 0
y = y - n I/ 2
x = x - (n - 1) I/ 2
V l = 2 * max(abs(x), abs(y))
d = I y >= x {(l * 3 + x + y)} E (l - x - y)
R (l - 1) ^ 2 + d + start - 1
 
F show_spiral(n, symbol = ‘# ’, start = 1, =space = ‘’)
V top = start + n * n + 1
V is_prime = [0B, 0B, 1B] [+] [1B, 0B] * (top I/ 2)
L(x) 3 .< 1 + Int(sqrt(top))
I !is_prime[x]
L.continue
L(i) (x * x .< top).step(x * 2)
is_prime[i] = 0B
 
(Int -> String) f = _ -> @symbol
 
I space == ‘’
space = ‘ ’ * symbol.len
 
I symbol.empty
V max_str = String(n * n + start - 1).len
I space == ‘’
space = ‘.’ * max_str‘ ’
f = x -> String(x).rjust(@max_str)‘ ’
 
V cell_str = x -> I @is_prime[x] {@f(x)} E @space
 
L(y) 0 .< n
print((0 .< n).map(x -> cell(@n, x, @y, @start)).map(v -> @cell_str(v)).join(‘’))
print()
 
show_spiral(10, symbol' ‘# ’, space' ‘ ’)
show_spiral(9, symbol' ‘’, space' ‘ - ’)</lang>
 
{{out}}
<pre>
#
# #
# # #
# # #
# # #
# # # #
# #
# #
# # #
# #
 
- - - - 61 - 59 - -
- 37 - - - - - 31 -
67 - 17 - - - 13 - -
- - - 5 - 3 - 29 -
- - 19 - - 2 11 - 53
- 41 - 7 - - - - -
71 - - - 23 - - - -
- 43 - - - 47 - - -
73 - - - - - 79 - -
</pre>
 
=={{header|360 Assembly}}==
1,480

edits