# Modulus/division

**URL:** <https://rubytalk.org/t/modulus-division/35461>\
**Category:** ruby-talk\
**Created:** [18 February 2007 02:24 UTC](https://rubytalk.org/t/modulus-division/35461 "2007-02-18T02:24:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Derek\_Derek](https://avatars.discourse-cdn.com/v4/letter/d/6f9a4e/32.png) [@Derek\_Derek](https://rubytalk.org/u/Derek_Derek)\
**Post date:** [18 February 2007 02:24 UTC](https://rubytalk.org/t/modulus-division/35461/1 "2007-02-18T02:24:22Z")

</div>

hey guys, i am reading that "learning to program" by Chris Pine still  
and at the end of each lesson they give you a "give this a try" thing  
for you practice what you've learned... Anyway, at the end of Chapter 9  
they give you this to try. I sort of have an idea of what i should do,  
but i really just don't understand the modulus(%) stuff, i was wondering  
if you guys could help me with just understanding what modulus is and  
how it works, thank you!

BELOW IS THE PROBLEM THEY GIVE ...

Old-school Roman numerals. In the early days of Roman numerals,  
the Romans didnÃÕ bother with any of this new-fangled subtraction  
IX nonsense. No sir, it was straight addition, biggest to littlest -  
so 9 was written VIIII, and so on. Write a method that, when  
passed an integer between 1 and 3000 (or so), returns a string  
containing the proper old-school Roman numeral. In other words,  
old\_roman\_numeral 4 should return 'IIII'. Make sure to test  
your method on a bunch of different numbers. Hint: Use the integer  
division and modulus methods on page 36.  
For reference, these are the values of the letters used:  
I = 1 V = 5 X = 10 L = 50  
C = 100 D = 500 M = 1000

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Derek\_Perrault](https://avatars.discourse-cdn.com/v4/letter/d/f9ae1b/32.png) [@Derek\_Perrault](https://rubytalk.org/u/Derek_Perrault)\
**Post date:** [18 February 2007 02:49 UTC](https://rubytalk.org/t/modulus-division/35461/2 "2007-02-18T02:49:40Z")

</div>

Modulus division = Remainder division

Two goes into five two times, with one left over. 5 / 2 = 2 (R1).  
Modulus division only returns that remainder. 5 % 2 = 1

Note that, when divided by 2, odd numbers always return 1, while even  
numbers always return 0.

> **···**
>
> On 2/17/07, Derek Derek \<derek.teixeira@gmail.com\> wrote:
> 
> > hey guys, i am reading that "learning to program" by Chris Pine still  
> > and at the end of each lesson they give you a "give this a try" thing  
> > for you practice what you've learned... Anyway, at the end of Chapter 9  
> > they give you this to try. I sort of have an idea of what i should do,  
> > but i really just don't understand the modulus(%) stuff, i was wondering  
> > if you guys could help me with just understanding what modulus is and  
> > how it works, thank you!
> > 
> > BELOW IS THE PROBLEM THEY GIVE ...
> > 
> > Old-school Roman numerals. In the early days of Roman numerals,  
> > the Romans didnÃÕ bother with any of this new-fangled subtraction  
> > IX nonsense. No sir, it was straight addition, biggest to littlest -  
> > so 9 was written VIIII, and so on. Write a method that, when  
> > passed an integer between 1 and 3000 (or so), returns a string  
> > containing the proper old-school Roman numeral. In other words,  
> > old\_roman\_numeral 4 should return 'IIII'. Make sure to test  
> > your method on a bunch of different numbers. Hint: Use the integer  
> > division and modulus methods on page 36.  
> > For reference, these are the values of the letters used:  
> > I = 1 V = 5 X = 10 L = 50  
> > C = 100 D = 500 M = 1000
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Derek\_Derek](https://avatars.discourse-cdn.com/v4/letter/d/6f9a4e/32.png) [@Derek\_Derek](https://rubytalk.org/u/Derek_Derek)\
**Post date:** [18 February 2007 03:16 UTC](https://rubytalk.org/t/modulus-division/35461/3 "2007-02-18T03:16:28Z")

</div>

Derek Perrault wrote:

> Modulus division = Remainder division
> 
> Two goes into five two times, with one left over. 5 / 2 = 2 (R1).  
> Modulus division only returns that remainder. 5 % 2 = 1
> 
> Note that, when divided by 2, odd numbers always return 1, while even  
> numbers always return 0.

okay, cool. i found the answer to the question on a forum online ...

ANSWER:

def old\_roman\_number input

&nbsp;&nbsp;while input \< 1 || input \> 3999  
&nbsp;&nbsp;&nbsp;&nbsp;puts 'Please enter a number between 1 and 3999'  
&nbsp;&nbsp;&nbsp;&nbsp;input = gets.chomp.to\_i  
&nbsp;&nbsp;end

&nbsp;&nbsp;m\_mod = input%1000  
&nbsp;&nbsp;d\_mod = input%500  
&nbsp;&nbsp;c\_mod = input%100  
&nbsp;&nbsp;l\_mod = input%50  
&nbsp;&nbsp;x\_mod = input%10  
&nbsp;&nbsp;v\_mod = input%5

&nbsp;&nbsp;m\_div = input/1000  
&nbsp;&nbsp;d\_div = m\_mod/500  
&nbsp;&nbsp;c\_div = d\_mod/100  
&nbsp;&nbsp;l\_div = c\_mod/50  
&nbsp;&nbsp;x\_div = l\_mod/10  
&nbsp;&nbsp;v\_div = x\_mod/5  
&nbsp;&nbsp;i\_div = v\_mod/1

&nbsp;&nbsp;m = 'M' \* m\_div  
&nbsp;&nbsp;d = 'D' \* d\_div  
&nbsp;&nbsp;c = 'C' \* c\_div  
&nbsp;&nbsp;l = 'L' \* l\_div  
&nbsp;&nbsp;x = 'X' \* x\_div  
&nbsp;&nbsp;v = 'V' \* v\_div  
&nbsp;&nbsp;i = 'I' \* i\_div

&nbsp;&nbsp;puts m + d + c + l + x + v + i

end

number = gets.chomp.to\_i  
old\_roman\_number(number)

I understnad the first few lines .. the "while" stuff and and ending  
stuff too. do you think you explain to me why the % and / works here? i  
guess i really need to get better at math. i'm sorry if i'm being really  
stupid here, hah. i just want to really understand things, not just see  
and move on .. thank you again..

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jason\_Mayer](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jason\_Mayer](https://rubytalk.org/u/Jason_Mayer)\
**Post date:** [18 February 2007 05:00 UTC](https://rubytalk.org/t/modulus-division/35461/4 "2007-02-18T05:00:29Z")

</div>

I find that when I'm trying to understand what's going on, the best way to  
figure it out is to use IRB

E:\Documents and Settings\Jason\>irb  
irb(main):001:0\> input = 3123  
=\> 3123  
irb(main):002:0\> m\_mod = input%1000  
=\> 123  
irb(main):003:0\> m\_div = input/1000  
=\> 3  
irb(main):004:0\> m = 'M' \* m\_div  
=\> "MMM"  
irb(main):006:0\> puts m  
MMM  
=\> nil  
irb(main):007:0\>

You can probably figure out the rest from there. Hope that helps.

> **···**
>
> On 2/17/07, Derek Teixeira \<derek.teixeira@gmail.com\> wrote:
> 
> > I understnad the first few lines .. the "while" stuff and and ending  
> > stuff too. do you think you explain to me why the % and / works here? i  
> > guess i really need to get better at math. i'm sorry if i'm being really  
> > stupid here, hah. i just want to really understand things, not just see  
> > and move on .. thank you again..

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungerecke](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@Sebastian\_Hungerecke](https://rubytalk.org/u/Sebastian_Hungerecke)\
**Post date:** [18 February 2007 07:50 UTC](https://rubytalk.org/t/modulus-division/35461/5 "2007-02-18T07:50:07Z")

</div>

Derek Teixeira wrote:

> okay, cool. i found the answer to the question on a forum online ...

> &nbsp;&nbsp;m\_mod = input%1000

Puts the remainder of division by 1000 (i.e. the three last digits of the  
number) in m\_mod.

> &nbsp;&nbsp;d\_mod = input%500

Remainder of division by 500 (i.e. the three last digits or if that number  
would be higher than 500, the three last digits minus 500)

> &nbsp;&nbsp;c\_mod = input%100

2 last digits.

> &nbsp;&nbsp;l\_mod = input%50

2 last digits minus 50 if necessary.

> &nbsp;&nbsp;x\_mod = input%10

Last digit.

> &nbsp;&nbsp;v\_mod = input%5

Last digit minus 5 if necessary.

> &nbsp;&nbsp;m\_div = input/1000

Divides the number by 1000, cutting of everything after the point (because  
this is integer division). So basically this returns everything but the  
three last digits.

> &nbsp;&nbsp;d\_div = m\_mod/500

This is one if the third to last digit (i.e. the one who specifies the  
hundreds) is 5 or higher and zero otherwise.

> &nbsp;&nbsp;c\_div = d\_mod/100

The third to last digit minus 5 if it's 5 or higher.

> &nbsp;&nbsp;l\_div = c\_mod/50  
> &nbsp;&nbsp;x\_div = l\_mod/10

> &nbsp;&nbsp;v\_div = x\_mod/5  
> &nbsp;&nbsp;i\_div = v\_mod/1

The same thing for the second to last and last digit accordingly.

> do you think you explain to me why the % and / works here?

I hope this is what I just did and you understand it now.

HTH,  
Sebastian Hungerecker

> **···**
>
> --  
> Ist so, weil ist so  
> Bleibt so, weil war so

---

<div class="post-metadata">

**Author:** ![Derek\_Derek](https://avatars.discourse-cdn.com/v4/letter/d/6f9a4e/32.png) [@Derek\_Derek](https://rubytalk.org/u/Derek_Derek)\
**Post date:** [18 February 2007 13:46 UTC](https://rubytalk.org/t/modulus-division/35461/6 "2007-02-18T13:46:34Z")

</div>

i tried to get a better understanding of the % method, so i just set up  
a program to show me the results of the % of a number ...

so i tried out the number 4.

puts 4%1000  
puts 4%500  
puts 4%100  
puts 4%50  
puts 4%10  
puts 4%5

and for everyline i got "4" as the remainder.. but when i tried these  
with a calulator .. i only got 4 as a remainder for the 1000. when i did  
the 4/500 i got a .008 .. so shouldn't the % be an 8?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungerecke](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@Sebastian\_Hungerecke](https://rubytalk.org/u/Sebastian_Hungerecke)\
**Post date:** [18 February 2007 14:10 UTC](https://rubytalk.org/t/modulus-division/35461/7 "2007-02-18T14:10:10Z")

</div>

Derek Teixeira wrote:

> and for everyline i got "4" as the remainder.. but when i tried these  
> with a calulator .. i only got 4 as a remainder for the 1000. when i did  
> the 4/500 i got a .008 .. so shouldn't the % be an 8?

The remainder of divison has nothing to do with what would come after the  
decimal point.  
You can calculate x%y as follows: First you calculate x/y (cutting off  
everything after the decimal point). Then you take this number and  
substract it from x.  
For example: 421/100==4 4\*100==400 421%100==421-400==21  
If y\>x then x/y==0, so x%y==x.

HTH,  
Sebastian Hungerecker

> **···**
>
> --  
> NP: Nocte Obducta - Eins mit der Essenz der Nacht  
> Ist so, weil ist so  
> Bleibt so, weil war so

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter4](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Tim\_Hunter4](https://rubytalk.org/u/Tim_Hunter4)\
**Post date:** [18 February 2007 14:14 UTC](https://rubytalk.org/t/modulus-division/35461/8 "2007-02-18T14:14:31Z")

</div>

Derek Teixeira wrote:

> i tried to get a better understanding of the % method, so i just set up a program to show me the results of the % of a number ...
> 
> so i tried out the number 4.
> 
> puts 4%1000  
> puts 4%500  
> puts 4%100  
> puts 4%50  
> puts 4%10  
> puts 4%5
> 
> and for everyline i got "4" as the remainder.. but when i tried these with a calulator .. i only got 4 as a remainder for the 1000. when i did the 4/500 i got a .008 .. so shouldn't the % be an 8?

When I was in elementary school they taught us these words for division. Suppose you have 9 divided by 4. 4 is the "divisor". 9 is the "dividend". 4 goes into 9 2 times, so 2 is the "quotient", but since 4 times 2 is only 8, there is 1 left over. 1 is called the "remainder". The modulus operator returns the remainder after dividing two integers. That is, 9 % 4 = 1.

So what is 4 % 5? 5 goes into 4 0 times. 0 times 5 is 0, so there is 4 left over. 4 % 5 = 4. For 4 % 10, 10 goes into 4 0 times, with 4 left over. 4 % 10 = 4. Same for 4 % 50 and all your other examples.

For interesting experiments, use a divisor that is smaller than the dividend. 10 % 4 = 2. 12 % 3 = 0.

For more interesting experiments, use negative numbers.

---

<div class="post-metadata">

**Author:** ![Derek\_Derek](https://avatars.discourse-cdn.com/v4/letter/d/6f9a4e/32.png) [@Derek\_Derek](https://rubytalk.org/u/Derek_Derek)\
**Post date:** [18 February 2007 17:31 UTC](https://rubytalk.org/t/modulus-division/35461/9 "2007-02-18T17:31:14Z")

</div>

Timothy Hunter wrote:

> Derek Teixeira wrote:
> 
> > puts 4%5
> > 
> > and for everyline i got "4" as the remainder.. but when i tried these  
> > with a calulator .. i only got 4 as a remainder for the 1000. when i did  
> > the 4/500 i got a .008 .. so shouldn't the % be an 8?
> 
> When I was in elementary school they taught us these words for division.  
> Suppose you have 9 divided by 4. 4 is the "divisor". 9 is the  
> "dividend". 4 goes into 9 2 times, so 2 is the "quotient", but since 4  
> times 2 is only 8, there is 1 left over. 1 is called the "remainder".  
> The modulus operator returns the remainder after dividing two integers.  
> That is, 9 % 4 = 1.
> 
> So what is 4 % 5? 5 goes into 4 0 times. 0 times 5 is 0, so there is 4  
> left over. 4 % 5 = 4. For 4 % 10, 10 goes into 4 0 times, with 4 left  
> over. 4 % 10 = 4. Same for 4 % 50 and all your other examples.
> 
> For interesting experiments, use a divisor that is smaller than the  
> dividend. 10 % 4 = 2. 12 % 3 = 0.
> 
> For more interesting experiments, use negative numbers.

i think i've got it!

is this pretty mcuh what they are saying .. i put explanations next to  
each line ..  
def old\_roman\_number input

&nbsp;&nbsp;while input \< 1 || input \> 3999  
&nbsp;&nbsp;&nbsp;&nbsp;puts 'Please enter a number between 1 and 3999'  
&nbsp;&nbsp;&nbsp;&nbsp;input = gets.chomp.to\_i  
&nbsp;&nbsp;end

&nbsp;&nbsp;m\_mod = input%1000 4/1000 means 1000 goes into 4 zero times so 0\*1000  
= 0 .. so 4-0= 4  
&nbsp;&nbsp;d\_mod = input%500 4/500 means 500 goes into 4 zero times 0\* 500 = 0  
... so 4-0=4  
&nbsp;&nbsp;c\_mod = input%100 4/100 means 100 goes into 4 zero times 0\*100 =0 ..  
so 4-0=4  
&nbsp;&nbsp;l\_mod = input%50 4/50 means 50 goes into 4 zero times 0\*50 = 0 ..  
so 4-0=4  
&nbsp;&nbsp;x\_mod = input%10 4/10 means 10 goes into 4 zero times 0\*10 = 0 ..  
so 4-0 = 4  
&nbsp;&nbsp;v\_mod = input%5 4/5 means 5 goes into 4 zero time 0\*5 = 0 .. so  
4-0= 4

&nbsp;&nbsp;m\_div = input/1000 = 4/1000 = 0  
&nbsp;&nbsp;d\_div = m\_mod/500 = 4/500 = 0  
&nbsp;&nbsp;c\_div = d\_mod/100 = 4/100 = 0  
&nbsp;&nbsp;l\_div = c\_mod/50 = 4/50 = 0  
&nbsp;&nbsp;x\_div = l\_mod/10 = 4/10 = 0  
&nbsp;&nbsp;v\_div = x\_mod/5 = 4/5 = 0  
&nbsp;&nbsp;i\_div = v\_mod/1 = 4/1=4

&nbsp;&nbsp;m = 'M' \* m\_div = 0  
&nbsp;&nbsp;d = 'D' \* d\_div = 0  
&nbsp;&nbsp;c = 'C' \* c\_div = 0  
&nbsp;&nbsp;l = 'L' \* l\_div = 0  
&nbsp;&nbsp;x = 'X' \* x\_div = 0  
&nbsp;&nbsp;v = 'V' \* v\_div = 0  
&nbsp;&nbsp;i = 'I' \* i\_div = 4 \* I equals IIII

&nbsp;&nbsp;puts m + d + c + l + x + v + i

end

number = gets.chomp.to\_i  
old\_roman\_number(number)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Edwin\_Fine](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Edwin\_Fine](https://rubytalk.org/u/Edwin_Fine)\
**Post date:** [18 February 2007 18:38 UTC](https://rubytalk.org/t/modulus-division/35461/10 "2007-02-18T18:38:06Z")

</div>

You can calculate modulus more expensively (but easier to understand)  
like this:

def remainder(num, divided\_by)  
&nbsp;&nbsp;num - (num / divided\_by).floor \* divided\_by  
end

'floor' effectively gets rid of everything past the decimal point and  
returns an integer.

We know intuitively that if we divide 401 by 100, we get 4 remainder 1.

Using the calculation (let's assume for laughs we have floating point  
numbers),

401.0 - (401.0 / 100.0).floor \* 100.0 =  
401.0 - (4.01).floor \* 100.0 =  
401.0 - 4 \* 100.0 =  
401.0 - 400.0 = 1.0

remainder 401, 100 =\> 1  
remainder 40, 100 =\> 40  
remainder 123456, 87 =\> 3  
remainder 123.45, 100.0 =\> 23.45

These values agree with the results of using '%'.

Hope this helps.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
