# Modulus

(Difference between revisions)
 Revision as of 08:27, 24 December 2014 (view source)← Older edit Revision as of 05:45, 27 December 2014 (view source)Newer edit → Line 1: Line 1: - Modulus is an extremely powerful operator that is useful in many common algorithms. The operator returns the remainder after division, so for instance if 10 were divided by 3 the result would be 9 with a remainder of 1, 10 % 9 would be 1. + Modulus is an arithmetic operator that is useful in many common algorithms. The modulus expression [code]a % b[/code] gives the remainder after performing the division [code]a / b[/code]. More technically, it returns [code]c[/code] such that [code]a = K*b + c[/code], where [code]K[/code] is an integer. As an example, [code]10 % 3[/code] returns [code]1[/code], as [code]10 / 3[/code] is [code]9[/code] with a remainder of [code]1[/code]. - For instance if it is 11:00 and you want to know what hour it will be in 5 hours on a 12 hour clock you can do the following. + An algorithmic example of the use of modulus is computing the time a certain number of hours after a given time. Take, for example, computing the time five hours in the future. If the current time is 11:00, adding five hours yields 4:00. + show_message("5 hours from 11:00 is " + string((11 + 5) % 12) + ":00"); show_message("5 hours from 11:00 is " + string((11 + 5) % 12) + ":00"); Line 9: Line 10: - You may also want to get whether a number is even or odd. + For this simple example, you could compute this using a simple conditional statement, but modulus allows doing it in the general case. For instance, if the time is 23:00, but you want the time in five hours on a twelve-hour clock, the modulus example will still work, while simply conditionally subtracting twelve would give you 16:00 (28 − 12 = 16, but 28 % 12 is 4). + + Another simple example is determining whether a number is even or odd. if (number % 2 == 0) { if (number % 2 == 0) { Line 18: Line 21: - It may also be useful to perform some operation every nth iteration of a loop. The following loop will display every 3rd number between 0 and 50 and ignore 0. + It may also be useful to perform some operation every nth iteration of a loop. The following loop will display every third number between zero and fifty: - for (i = 0; i < 50; i++) { + for (i = 1; i < 50; i++) { - if (i % 3 == 0 && i > 0) { + if (i % 3 == 0) { show_message(string(i)); show_message(string(i)); } } } }

## Revision as of 05:45, 27 December 2014

Modulus is an arithmetic operator that is useful in many common algorithms. The modulus expression [code]a % b[/code] gives the remainder after performing the division [code]a / b[/code]. More technically, it returns [code]c[/code] such that [code]a = K*b + c[/code], where [code]K[/code] is an integer. As an example, [code]10 % 3[/code] returns [code]1[/code], as [code]10 / 3[/code] is [code]9[/code] with a remainder of [code]1[/code].

An algorithmic example of the use of modulus is computing the time a certain number of hours after a given time. Take, for example, computing the time five hours in the future. If the current time is 11:00, adding five hours yields 4:00.

show_message("5 hours from 11:00 is " + string((11 + 5) % 12) + ":00");

// the following can be done for a 24 hour clock
show_message("5 hours from 22:00 is " + string((22 + 5) % 24) + ":00");

For this simple example, you could compute this using a simple conditional statement, but modulus allows doing it in the general case. For instance, if the time is 23:00, but you want the time in five hours on a twelve-hour clock, the modulus example will still work, while simply conditionally subtracting twelve would give you 16:00 (28 − 12 = 16, but 28 % 12 is 4).

Another simple example is determining whether a number is even or odd.

if (number % 2 == 0) {
// even
} else {
// odd
}

It may also be useful to perform some operation every nth iteration of a loop. The following loop will display every third number between zero and fifty:

for (i = 1; i < 50; i++) {
if (i % 3 == 0) {
show_message(string(i));
}
}