Ulam spiral (for primes): Difference between revisions

→‎{{header|Ruby}}: A form of improvement
(Tcl implementation added)
(→‎{{header|Ruby}}: A form of improvement)
Line 1,801:
 
=={{header|Ruby}}==
It finds the number from the position ( the coordinates ).
{{trans|Python}}
<lang ruby>require 'prime'
Line 1,900 ⟶ 1,901:
 
===Another Version===
computes the next spiral position.
 
<lang ruby>require 'prime'
 
Line 1,921 ⟶ 1,922:
h = Hash.new(0)
position = spiral_generator
(start ... start+n*n-1).each do |i|
pos = position.next
h[pos] = 1 if i.prime?
Line 1,927 ⟶ 1,928:
chr = [[' ', '▄'], ['▀', '█']]
(xmin, xmax), (ymin, ymax) = h.keys.transpose.map(&:first).minmax)
ymin, ymax = h.keys.map(&:last).minmax
(xmin..xmax).step(2).each do |x|
puts (ymin..ymax).map{|y| chr[h[[x,y]]][h[[x+1,y]]]}.join
Line 1,934:
end
 
[911, 120122].each do |n|
puts "\nN : #{n}"
ulam_spiral(n)
Line 1,941:
{{out}}
<pre style="font-size:50%">
N : 911
▀▄
▀▄▀▄ ▄▀ ▀
▀ ▀▄ ▄▀▄
▄▀ █▄▀▄
▄▀▄ ▀▀ ▀
▀▄▀ ▀▄ ▀▄
▀▄▀ ▀▄
 
N : 120122
▀▄▀▄▀ ▄▀ ▄▀ ▀▄ ▄▀ ▀ ▀ ▄ ▄▀
▄▀ ▄▀ ▀▄▀ ▄▀ ▄ ▄ ▄▀ ▄▀ ▀▄
▀▄▀ ▀▄ ▀▄▄▀ ▄▀ ▄▀ ▀▄▄▀ ▄▀ ▄▀ ▀▄ ▀▄
▄▀ ▄ ▄ ▄▀ ▄▀▄
▄▀▄ ▀▄▀ ▀▄ ▄▀ ▀▄▀ ▀▄ ▀▄ ▄▀
▄▀ ▄▀ ▀ ▄ ▄▀ ▀▄ ▄▀▄▀ ▀▄ ▀▄▀ ▄▀▄
▄▀▄ ▄▀ ▄▀ ▄▀▄ ▀▄ ▀▄ ▄▀ ▄▀ ▄▀ ▀▄ ▀▄ ▀▄ ▀▄
▀▄ ▄▀ ▄▀ ▀▄ ▄▀ ▄▀ ▀▄ ▀▄ ▀▄ ▄▀ ▀▄
▄▀▄▀ ▀▄ ▄▀ ▀▄ ▀▄ ▄▀ ▀▄ ▀▄ ▀▄ ▀▄
▀▄ ▀▄ ▄▀ ▄▀ ▄▀ ▀▄ ▄▀▄ ▄▀ ▀▄
▄▀ ▀▄ ▀▄▀ ▄▀▀▄▀ ▄▀ ▀▄ ▀▄ ▄▀ ▀▄▀ ▀▄ ▀▄ ▄▀ ▀▄
▄▀ ▄▀▄ ▀▄ ▀▄▀▄ ▄▀ ▀▄ ▄▀ ▄▀▄ ▄▀ ▄▀
▄▀ ▄▀▄ ▄▀ ▀▄ ▄▀ ▄▀ ▄▀▄▀ ▄▀ ▀▄▀ ▄ ▄ ▄▀ ▀▄▀ ▄▀
▄▀ ▄▀ ▄▀
▀▄ ▀▄ ▀▄▀▄▀ ▄▀ ▀▄▀ ▄▀▄ ▀▄ ▀▄ ▀▄
▀▄ ▄▀▄▀▄ ▀▄ ▄▀ ▀▄▀ ▄ ▄▀ ▀▄▀ ▀▄ ▄▀ ▄▀ ▀▄▀ ▄ ▄
▀▄ ▄▀▄▀▄ ▀▄ ▀▄ ▄▀ ▄▀
▄▀ ▄▀ ▄▀ ▄▀ ▄▀ ▀▄ ▀▄ ▀▄ ▄▀ ▀▄ ▀▄▀ ▀▄ ▄▀▄ ▀▄
▄ ▄ ▀▄ ▄▀▄ ▄ ▄ ▄▀ ▀▄ ▄▀▄ ▀▄▄▀ ▀▄ ▀▄ ▄▀ ▄▀▄▀ ▀▄ ▀▄▀▄ ▄
▄▀ ▀▄▀▄ ▀▄ ▀▄ ▀▄ ▀▄ ▄▀ ▄▀▄
▄▀ ▄▀▄ ▀▄ ▄▀ ▄▀ ▀▄▀ ▀▄▀▀▄ ▀▄▀▄▀
▄▀ ▀▄ ▄▀ ▄▀ ▄▀ ▄▀▄▀ ▀▄ ▄▀ ▀▄ ▄▀ ▄▀▄ ▀▄▀ ▀▄▀ ▀▄ ▄▀
▀▄▀ ▀▄ ▀▄▀ ▀▄▀▄ ▄▀ ▀▄ ▀▄ ▄▀ ▀▄ ▀▄ ▀▄ ▄▀ ▄▀
▀▄ ▄▀ ▀▄▀ ▀▄ ▄▀ ▀▄ ▄▀ ▀▄ ▄ ▄ ▀▄ ▄▀▄▀
▄▀ ▀▄ ▄▀▀▄ ▀▄▀▄▄▀▄ ▄▀▄ ▀▄▀ ▄▀ ▄▀ ▄▀ ▄▀
▀▄ ▀▄ ▀▄ ▄▀▄ ▀▄ ▄▀ ▀▄ ▄ ▀▄ ▄▀ ▀ ▀
▀▄ ▀▄▀▄▄▀ ▀▄ ▄▀▄ ▄▀ ▀ ▀ ▄▀▄▀ ▀▄ ▀▄▀ ▄▀ ▀▄ ▄▀
▄▀ ▄▀▀▄ ▄▀▄ ▀▄▀ ▀▄ ▀▄▄▀ ▀▄ ▄ ▄ ▄▀▄ ▄▀ ▀▄
▀ ▀▄ ▀▄▄▀ ▄▀▄ ▀▄ ▄▀▀▄ ▀ ▀
▀ ▀ ▄▀▄▀▀▄ ▀▄ ▀ ▀ ▀ ▀▄ ▀▄ ▀▄ ▀ ▀ ▀ ▀ ▀ ▀▄ ▄▀▄ ▄▀▄▀▄ ▄▀ ▀ ▀ ▀▄ ▄▀ ▄▀ ▀▄▀ ▀
▀▄▀ ▄▀ ▄▀▄▀ ▄▀ ▀▄ ▄▀▄ ▀▀ ▄▀ █▄▀▄ ▄▀▄ ▄▀▄ ▄▀ ▄ ▄ ▄
▄▀ ▀ ▀▄▀ ▀▄ ▄▀▄ ▀▄
▀▄ ▄▀ ▄▀ ▄▀ ▀▄ ▀▄▀ ▀▄ ▀▄ ▀▄▀▄▀ ▄▀▀▄ ▄▀▀▄ ▄▀ ▄▀ ▀ ▀ ▀▄▀ ▄▀ ▀▄
▄▀▄▀▄ ▄▀▄ ▀▄ ▀▄▀▄ ▀▄▀ ▀▄ ▀▄▄▀ ▄ ▄ ▀▄ ▄▀ ▄▀ ▄ ▀
▀▄ ▀▄ ▄▀ ▀▄ ▀▄ ▄▀▄ ▀▄▀
▀▄ ▀▄ ▄▀ ▀▄ ▀▄▀ ▀▄ ▄▀▄ ▀▄ ▄▀ ▄▀▄▀ ▀▄ ▄▀ ▀ ▀ ▀▄▀ ▀ ▀
▄ ▄▀▄▀ ▄▀▄ ▄▀▄ ▀▄ ▄▀ ▄▀ ▀▄▀▄ ▄▀ ▄▀ ▀▄▀ ▀▄
▄▀ ▀▄ ▀▄ ▀▄ ▄▄▀ ▀▄▀ ▀▄▀ ▀▄ ▀▄ ▄▀▄ ▀▄ ▄▀ ▄▀
▄▀ ▄▀ ▀▄ ▀ ▀▄▀ ▀▄ ▀▄ ▄▀ ▄▀ ▀▄ ▀▄▀ ▀▄▀ ▄▀ ▄▀
▄ ▄ ▄▀▄ ▄▀ ▀▄ ▄▀ ▀▄ ▄▀ ▀ ▄ ▀▄ ▄▀
▄▀▄ ▄▀ ▄▀ ▄▀ ▄▀ ▄▀ ▀▄ ▄▀ ▄▀ ▀▄ ▄▀ ▀▄▀ ▄▀▄ ▄▀ ▀▄
▀ ▄ ▀▄▀ ▀▄▀ ▄▀ ▀▄ ▄▀ ▄▀▄ ▀▄ ▀▄ ▄▀
▄ ▀ ▀ ▄ ▀▄ ▀▄ ▄ ▄ ▄▀ ▀▄ ▄▀▄ ▄ ▄ ▀▄▀▄▀ ▀▄ ▀▄▀
▄▀ ▀▄▄▀ ▄ ▄ ▄▀ ▀▄ ▄▀ ▀▄
▄▀ ▄▀▄▀▄ ▀ ▀ ▄▀ ▀▄ ▄▀ ▀ ▄ ▀▄
▄▀▄▄▀▄▀ ▄▀ ▄▀▄ ▄▀ ▄▀ ▀▄ ▀▄▀▀▄ ▀▄ ▀▄ ▄▀ ▄▀▄ ▀▄
▀▄ ▄▀▄▀▄▀ ▄▀ ▀▄ ▄▀ ▄▀ ▀▄▀▄ ▀▄▀ ▄▀▄▀ ▀▄ ▀▄
▀▄ ▄▀ ▀▄ ▄▀▄ ▀▄ ▄▀ ▄▀ ▄▀ ▀▄ ▄▀ ▀▄▀ ▀▄ ▀▄▀
▀▄ ▀▄ ▄▀ ▄▀▄ ▄▀ ▀▄▀ ▀▄▀
▀▄ ▀▄ ▀▄ ▀▄ ▄ ▄ ▀ ▄ ▀▄ ▀ ▀
▀▄ ▀▄ ▀▄ ▄▀ ▄▀ ▄▀ ▀▄ ▄▀ ▄▀ ▀ ▀ ▄ ▀ ▀▄▀▄ ▀▄
▄▀▄ ▀▄ ▀▄ ▄▀ ▀▄ ▀▄ ▄▀▀▄ ▄▀ ▀▄
▀ ▀ ▀▄ ▄▀ ▄▀▄ ▀▄▀ ▄▀ ▀▄ ▄▀ ▀▄ ▄ ▀ ▀▄
▀▄ ▄▀ ▄▀ ▄▀▄▀▄▀ ▀▄ ▄▀ ▀ ▄ ▄ ▄▀ ▀▄
▄ ▄ ▄▀ ▀▄▀ ▄▀ ▄▀▄ ▄▀ ▀▄ ▀ ▀▄ ▄▀ ▄▀
▀▄ ▀▄ ▄▀ ▀▄ ▀▄ ▀▄▀▄ ▀▄
▄▀ ▀▄ ▄▀
▀▄▀ ▄▀ ▄▀▄ ▀▄ ▄▀▄ ▄▀ ▄ ▀ ▄▀ ▀▄ ▄ ▄▀ ▄▀ ▄▀ ▄ ▄ ▀▄
▀▄ ▀▄ ▀▄ ▄▀ ▄▀ ▀▄ ▀ ▄ ▀▄ ▄▀▄
▀▄ ▄▀ ▄▀ ▀▄ ▄▀ ▄▀ ▄▀ ▀▄ ▄▀▄▀ ▀▄ ▄ ▀
▀ ▄ ▀▄ ▄ ▄▀▄ ▄ ▀▄▀▄ ▀ ▀ ▄ ▄ ▄▀ ▄▀▄ ▀▄ ▀▄
</pre>
The method of presentation of the result consulted " REXX ".
Anonymous user