# Re: Number Spiral (#109)

**URL:** <https://www.ruby-forum.com/t/re-number-spiral-109/82516>\
**Category:** Ruby\
**Created:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/re-number-spiral-109/82516 "2007-01-19T16:31:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/re-number-spiral-109/82516/1 "2007-01-19T16:31:04Z")

</div>

My answers assumes an odd numbered spirals (I inferred it from “The  
number zero represents the center of the spiral”).

Sorry for my beginners ruby (are there some standard min(x,y)/max(x,y,)  
functions?)

The approach is to work out a standard equation for the value in any  
cell (I ended up with two, for the top left and bottom right) and then  
to iterate through each cell and calculate the value. The algorithm is  
stateless.

class SpiralMaker  
def make\_spiral(size)  
# only allow odd numbered squares (as zero is centre)  
if (size.modulo(2) == 0)  
exit(1)  
end

```
#step along row
(1..size).each do |y|
  # step down columns
  (1..size).each do |x|
    # are we in top left or bottom right half of spiral?
    if (y+x <= size)
      # top left - calculate value
      sn = size - (2 * (min(x,y) - 1))
      val = (sn*sn) - (3*sn) + 2 - y + x
    else
      # bottom right - calculate value
      sn = size - (2 * (size - max(x,y)))
      val = (sn*sn) - sn + y - x
    end
    # Print value
    STDOUT.printf "%03d ", val
  end
  # Next line
  STDOUT.print "\n"
end

```

end

def min(a,b)  
(a \<= b) ? a : b  
end

def max(a,b)  
(a \>= b) ? a : b  
end  
end

maker = SpiralMaker.new  
maker.make\_spiral 21

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [September 25, 2007, 10:38pm UTC](https://www.ruby-forum.com/t/re-number-spiral-109/82516/2 "2007-09-25T22:38:33Z")

</div>

I think the standard idiom is to use the min/max functions of an array:

a = 5  
b = 10  
[a, b].max #=\> 10

You can also give max a block, similar to sort:

a = “Hello”  
b = “Hi”  
[a, b].max {|x, y| x.length \<=\> y.length}

Or you can write a method so it works more like sort\_by (the interface,  
not the implementation):

class Array  
def max\_by &blk  
max {|a, b| blk.call(a) \<=\> blk.call(b)}  
end  
end

a = “Hello”  
b = “Hi”  
[a, b].max {|x| x.length}

And then, in Ruby 1.9, you should be able to do this (using max\_by from  
above):

a = “Hello”  
b = “Hi”  
[a, b].max(&:length) # Not sure if the syntax is 100%

And if you still want your max() function:

def min(\*args)  
args.min  
end

Everything also applies to minimums using the min function.

Dan

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [September 25, 2007, 10:44pm UTC](https://www.ruby-forum.com/t/re-number-spiral-109/82516/3 "2007-09-25T22:44:36Z")

</div>

This one works for even and odd spirals (obvious really)

class SpiralMaker  
def make\_spiral(square\_size)  
# allow for even numbered squares by missing off the last row and  
column  
size = square\_size  
if (square\_size.modulo(2) == 0)  
square\_size = square\_size+1  
end

```
#step along row
(1..size).each do |y|
  # step down columns
  (1..size).each do |x|
    # are we in top left or bottom right half of spiral?
    if (y+x <= square_size)
      # top left - calculate value
      sn = square_size - (2 * (min(x,y) - 1))
      val = (sn*sn) - (3*sn) + 2 - y + x
    else
      # bottom right - calculate value
      sn = square_size - (2 * (square_size - max(x,y)))
      val = (sn*sn) - sn + y - x
    end
    # Print value
    STDOUT.printf "%03d ", val
  end
  # Next line
  STDOUT.print "\n"
end

```

end

def min(a,b)  
(a \<= b) ? a : b  
end

def max(a,b)  
(a \>= b) ? a : b  
end  
end

maker = SpiralMaker.new  
maker.make\_spiral 3

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [September 25, 2007, 10:44pm UTC](https://www.ruby-forum.com/t/re-number-spiral-109/82516/4 "2007-09-25T22:44:52Z")

</div>

On Jan 14, 2007, at 1:46 PM, Tom A. wrote:

> My answers assumes an odd numbered spirals (I inferred it from “The  
> number zero represents the center of the spiral”).

But the quiz example is an even spiral. 😉

James Edward G. II
