Modulus/division


#1

hey guys, i am reading that “learning to program” by Chris P. 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


#2

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.


#3

On 2/17/07, Derek T. removed_email_address@domain.invalid 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…

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.


#4

Derek T. wrote:

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

m_mod = input%1000

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

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)

c_mod = input%100

2 last digits.

l_mod = input%50

2 last digits minus 50 if necessary.

x_mod = input%10

Last digit.

v_mod = input%5

Last digit minus 5 if necessary.

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.

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.

c_div = d_mod/100

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

l_div = c_mod/50
x_div = l_mod/10

v_div = x_mod/5
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 H.


#5

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

while input < 1 || input > 3999
puts ‘Please enter a number between 1 and 3999’
input = gets.chomp.to_i
end

m_mod = input%1000
d_mod = input%500
c_mod = input%100
l_mod = input%50
x_mod = input%10
v_mod = input%5

m_div = input/1000
d_div = m_mod/500
c_div = d_mod/100
l_div = c_mod/50
x_div = l_mod/10
v_div = x_mod/5
i_div = v_mod/1

m = ‘M’ * m_div
d = ‘D’ * d_div
c = ‘C’ * c_div
l = ‘L’ * l_div
x = ‘X’ * x_div
v = ‘V’ * v_div
i = ‘I’ * i_div

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…


#6

Derek T. 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 H.


#7

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?


#8

Derek T. 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.


#9

Timothy H. wrote:

Derek T. 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

while input < 1 || input > 3999
puts ‘Please enter a number between 1 and 3999’
input = gets.chomp.to_i
end

m_mod = input%1000 4/1000 means 1000 goes into 4 zero times so 01000
= 0 … so 4-0= 4
d_mod = input%500 4/500 means 500 goes into 4 zero times 0
500 = 0
… so 4-0=4
c_mod = input%100 4/100 means 100 goes into 4 zero times 0100 =0 …
so 4-0=4
l_mod = input%50 4/50 means 50 goes into 4 zero times 0
50 = 0 …
so 4-0=4
x_mod = input%10 4/10 means 10 goes into 4 zero times 010 = 0 …
so 4-0 = 4
v_mod = input%5 4/5 means 5 goes into 4 zero time 0
5 = 0 … so
4-0= 4

m_div = input/1000 = 4/1000 = 0
d_div = m_mod/500 = 4/500 = 0
c_div = d_mod/100 = 4/100 = 0
l_div = c_mod/50 = 4/50 = 0
x_div = l_mod/10 = 4/10 = 0
v_div = x_mod/5 = 4/5 = 0
i_div = v_mod/1 = 4/1=4

m = ‘M’ * m_div = 0
d = ‘D’ * d_div = 0
c = ‘C’ * c_div = 0
l = ‘L’ * l_div = 0
x = ‘X’ * x_div = 0
v = ‘V’ * v_div = 0
i = ‘I’ * i_div = 4 * I equals IIII

puts m + d + c + l + x + v + i

end

number = gets.chomp.to_i
old_roman_number(number)


#10

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

def remainder(num, divided_by)
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.