```On Fri, 10 Oct 2008 10:13:59 -0500, Matthew Moss wrote:

> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> The three rules of Ruby Quiz 2:
>
> 1.  Please do not post any solutions or spoiler discussion for this quiz
> until 48 hours have passed from the time on this message.
>
> 2.  Support Ruby Quiz 2 by submitting ideas as often as you can! (A
> permanent, new website is in the works for Ruby Quiz 2. Until then,
> please visit the temporary website at
>
>      <http://splatbang.com/rubyquiz/>.
>
> 3.  Enjoy!
>
> Suggestion:  A [QUIZ] in the subject of emails about the problem helps
> original quiz message, if you can.
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> ## Modular Arithmetic (#179)
>
> _Quiz idea provided by Jakub Kuma_
>
> [Modular] [arithmetic] is integer-based arithmetic in which the
> operands and results are constrained to a limited range, from zero to
> one less than the modulus. Take, for example, the 24-hour clock. Hours
> on the clock start at 0 and increase up to (and include) 23. But above
> that, we must use the appropriate congruent value modulo 24. For
> example, if I wanted to know what time it would be seven hours after
> 10pm, I would add 22 and 7 to get 29. As that is outside the range `[0,
> 24)`, I take the congruent value mod 24, which is 5.  So seven hours
> after 10pm is 5am.
>
> Your task this week is to write a class `Modulo` that behaves in almost
> all ways like an `Integer`. It should support all basic operations:
> addition, subtraction, multiplication, exponentiation, conversion to
> string, etc. The significant difference, of course, is that an instance
> of `Modulo` should act modularly.
>
> For example:
>
>     # The default modulus is 26.
>     > a = Modulo.new(15)
>     > b = Modulo.new(19)
>     > puts (a + b)
>     8
>     > puts (a - b)
>     22
>     > puts (b * 3)
>     5

Just a preview of my solution (inspired by TZFile):

Mod26=ModularBase.new(26)
Mod8=ModularBase.new(8)

def test_incompat_bases
assert_raises(ArgumentError) do
Mod26.new(1)+Mod8.new(1)
end
assert_nothing_raised do
Mod26.new(1)+Mod8.new(1).to_i
end
end

That said, please clarify on the behavior of a couple tests:

#new
a = Mod26.new(15)
assert_equal(???, 15 + a)
end

#new
def test_sub_reverse
a = Mod26.new(15)
assert_equal(???, 1-a)
end

def test_mul_int_reverse
a = Mod8.new(15)
assert_equal(77, 11 * a)
end

Clearly a Modulo +/*/- an Integer should give a Modulo
Should the reverse relation promote the Modulo to an Integer, or promote
the Integer to a Modulo (of the same base)?

--Ken

--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

```