Remainders

On this page we will look at even and odd numbers. Before doing so, let's review the idea of "remainders" and they're relation to division. Recall that if we have a number \(a\) and divide it by \(b\) then we might get a remainder. For example, when we compute \(8 \div 3 \) we get 2 with a remainder of 2: \[ 8 = 3\cdot 2 + 2\] On your own, determine the remainder of 11 when divided by 4.

In math and programming, it's common to use the term "modulus" to refer to the remainder after division. Conveniently, in most programming languages there is a "modulo" operator that tells us the remainder after division. In JavaScript, the modulo operator is notated with % and we use it kind of like the division symbol. For example, we would write 8 % 3 to compute the remainder of 8 after division by 3. So 8 % 3 == 2. The code below shows a few examples of the modulo operator.

print(8 % 3);
print(11 % 4);

var x = 174, y = 23;
print(x % y);

Even and Odd Numbers

An even integer is an integer that is a multiple of 2. For example: 4, 6, and 10 are all even because \(2\cdot 2 = 4\), \(2\cdot 3 = 6\) and \(2\cdot 5 = 10\). On the other hand, an odd integer is an integer that is not even. We can think of an odd number as a number that is 1 more than an even number. We can then say that any odd number \(a\) looks like \[a = 2\cdot n + 1 \] where \(n\) is some integer.

By the way even and odd numbers are defined, we can use remainders as a way to test whether or not a number is even or odd - and this is a common thing to do in programming! Since even integers are always 2 times another number, if we divide an even number by 2 we should get an integer (and hence 0 as a remainder!). Think about it for a second. Pick any even number and divide it by 2. What's the remainder? It should be 0.

On the other hand, if we divide an odd number by 2 we should get 1 as a remainder. (Try it! Pick any odd number, divide it by 2, and notice how the remainder is 1). Therefore if n is an even number then n % 2 == 0 and if n is odd then n % 2 == 1. The code below shows this in action.

var n = 7;

if (n % 2 == 0) {
	print(`${n} is even!`);
} else {
	print(`${n} is odd!`);
}

In the code above, change the value of n to various values and run the code. This trick of using n % 2 == 0 to test whether or not n is even is a very useful trick in programming!

Activities

Activity: Defining an "even" function

In the editor below, define a function named isEven to test whether an integer n is even or not. The function should return true if n is even, and false otherwise. Then use this function to test a few numbers. We've added a little code to get you started - you need to edit what happens inside of the isEven function.

function isEven(n) {
	// YOUR CODE HERE
	return false;
}

print( isEven(3) );
print( isEven(4) );

Activity: Sorting numbers based on "parity"

"Parity" is a word sometimes used to describe whether a number is even or odd. So, if we have a list of numbers and we want to sort them based on "parity," we are just sorting them based on whether or not they are even.

An array a is defined behind the scenes, and it contains 10 random numbers between 0 and 100. Your job is to put all of the even numbers in the evens array and all of the odd numbers in the odds array. We've add a little code to get you started.

var evens = [], odds = [];

for (var i = 0; i < a.length; i++){
	// YOUR CODE HERE. DELETE THE LINE BELOW
	print(a[i]);
}

print(`Evens: ${evens}`);
print(`Odds: ${odds}`);

Here's a couple hints. Recall that we can add an element to an array using the .push() method of an array. So, evens.push(42) will add the number 42 to the end of the evens array. You will need to test whether a[i] is even or odd, and then add it to one of the appropriate array based on that.

Activity: Sum of odd numbers

In the editor below, determine the sum of the first 100 odd numbers.

print("Your code here.");