Modulo
Coding Skill Exercise #2
Modulo
Compute a % b without using % operator.
Knowledge Gap Finder
If you are unable to code the solution, answer the following questions and reply to this email to get customized lesson.
Was the problem statement clear to you when you read it? What did you think of doing first? Were you reminded of a construct in general or a general structure of solution that you thought would be useful? Have you previously seen problems that resemble this one? Did you feel stuck at any point while working on this problem? What did you choose as your test case? Do you think you’ve covered all possible scenarios with your tests? What program design techniques did you apply to solve this problem? Are there any constructs of the programming language that you find difficult or confusing to use? What issues make programming constructs difficult to use? For example, the keyword used, the syntax, the examples, the documentation for the construct, etc.
Feel free to forward this email to your friends so they can subscribe here https://codingskill.biz/.
layout: post title: Modulo Operation excerpt: This covers the basics of modulo operation and the problem based on it. tags: linearscan modulooperator forloop whileloop reductionofreturnvalue divisiblecheck reducingvaluebysubstraction counter chainedconditional accumulator
Modulus operator is an operator, denoted with a percent sign (%) in Ruby. It works on integers and returns the remainder when one number is divided by another.
The simplest thing that can be done with the modulo operator is checking if a number is divisble by another number.


Implement Modulo Operation
To get a better understanding of the modulo operation, implement the % operator without using the language builtin modulus operator. The implementation:


Using formula to implement modulo operation:


The modulo is the remainder after dividing one number by another. Let a = 19, b = 5. 19 cannot be divided exactly by 5. The closest we can get (without going over) is: 3 x 5 = 15, which is 4 less than 19. So the answer of 19 ÷ 5 is: 19 ÷ 5 = 3 R 4 Check it by multiplying: 5 × 3 + 4 = 19, r = a  b x ans
Refer mathisfun website for detailed explanation with illustrations.
Multiples of a Number
Write a function that takes a parameter n and returns the sum of numbers that are multiples of three or five, e.g. 3, 5, 6, 9, 10, 12, 15.


Fizz Buzz problem can be solved based on understanding of modulus operator. One of the building blocks in Fizz Buzz is the chained conditional. Chained conditional is a conditional statement with a series of alternative branches.
Sometimes there are more than two possibilities and it requires more than two branches. One way to express this kind of computation is called chained conditional.
Odd and Even Number
Write a method to check if a given number is even without using language builtin methods.
Since the modulo operator returns the remainder of division, we can use it in the implementation:


We can reuse the divisible method to make this method readable:


You can now write a method to check if a given number is odd without using language builtin methods. Modify the solution for even to implement the odd? method.
Problems that uses modulo operator as a basic building block:
 [Extract RightMost Digit]({% post_url 20210215subtractproductandsumofdigits %})
 [Cycle Through a Set of Values ]({% post_url 20210507excelsheetencode %})
Some problems require you to combine the modulo with division to solve the problem.
Primality Test
Prime numbers are the positive integers having only two factors, 1 and the integer itself. For example, factors of 3 are only 1 and 3, totally two. Hence, 3 is a prime number. 1 is neither prime nor composite.
Some prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97.


Equivalence Relation
Modulus operation can be used to define an equivalence relation. For the set of integers, use the modulus function to define a binary relation such that two numbers x and y are in the relation if and only if:
x mod m = y mod m
Thus, for m = 4,〈1,5〉is in the relation because 1 mod 4 = 5 mod 4.


We see that modulus used in this way defines an equivalence relation on the integers and this relation can be used to partition the integers into m equivalence classes.
Building Blocks
 Linear Scan
 Modulo Operator
 for Loop
 while Loop
 Reduction of Return Value
 Divisible Check
 Reducing Value by Substraction
 Accumulator