Don’t worry if you don’t know what the ***** I’m talking about, because by the end of this article, you’ll be a pro at it (or at least understand it better than before).
So let’s start with some basics. When we divide an integer by another integer, we get a quotient and a remainder. For example: 17 divided by 5 is 3 with a remainder of 2. The remainder class modulo 5 refers to the set of all numbers that have the same remainder when they are divided by 5. In other words, if you divide any number in this set by 5, you’ll get the same remainder as another number in the set.
Here’s an example: let’s say we want to find the remainder class modulo 5 for all numbers between -20 and 10 (inclusive). To do that, we need to figure out what remainders we can get when dividing by 5. The possible remainders are 0, 1, 2, 3, or 4. So let’s create a list of all the numbers between -20 and 10 (inclusive) with their corresponding remainder:
# Create an empty list to store the remainders
remainder_list = []
# Loop through all numbers between -20 and 10 (inclusive)
for num in range(-20, 11):
# Check if the number is negative
if num < 0:
# If so, add the absolute value of the number to the list and subtract it from 5 to get a positive remainder
remainder = abs(num) % 5 # Calculate the remainder of the absolute value of the number divided by 5
remainder = remainder - 5 # Subtract the remainder from 5 to get a positive remainder
else:
# If the number is non-negative, simply take the modulo of the number and 5 to get the remainder
remainder = num % 5 # Calculate the remainder of the number divided by 5
# Add the remainder and the number to the list as a tuple
remainder_list.append((remainder, num))
Now let’s sort this list by the first element (the remainder) so that we can see which numbers have the same remainder:
# Sorts the list by the first element (the remainder) using the sorted() function
# The key parameter specifies the function to be used for sorting, in this case a lambda function
# The lambda function takes in a single argument x and returns the first element of x, which is the remainder
# This ensures that the list is sorted based on the remainder value
sorted_list = sorted(remainder_list, key=lambda x: x[0])
# Prints the sorted list
print(sorted_list)
This will give us a list of tuples where each tuple contains both the remainder and the original number. Here’s what it looks like for our example range (-20 to 10):
# This script creates a list of tuples containing the remainder and original number for a given range.
# Create an empty list to store the tuples
tuples_list = []
# Loop through the range (-20 to 10)
for num in range(-20, 11):
# Calculate the remainder when dividing the number by 5
remainder = num % 5
# Create a tuple with the remainder and the original number
num_tuple = (remainder, num)
# Append the tuple to the list
tuples_list.append(num_tuple)
# Print the list of tuples
print(tuples_list)
# Output:
# [(0, -20), (1, -19), (2, -18), (3, -17), (4, -16), (0, -15), (1, -14), (2, -13), (3, -12), (4, -11), (0, -10), (1, -9), (2, -8), (3, -7), (4, -6), (0, -5), (1, -4), (2, -3), (3, -2), (4, -1), (0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (0, 5), (1, 6), (2, 7), (3, 8), (4, 9), (0, 10)]
# The script first creates an empty list to store the tuples.
# Then, it loops through the range (-20 to 10) and calculates the remainder when dividing each number by 5.
# A tuple is created with the remainder and the original number, and then appended to the list.
# Finally, the list of tuples is printed as the output.
As you can see, the numbers with a remainder of 0 are all multiples of 5. The numbers with a remainder of 1 or 4 have alternating signs because we added/subtracted the absolute value of negative numbers to get their corresponding remainders (which is why we used `abs(num)` in our code).
Remainder classes modulo 5. Not too bad, right? Just remember that when dealing with negative numbers, you need to add/subtract the absolute value of the number to get a positive remainder (unless you want to deal with negative remainders but who wants to do that?!).
Hope this helps! Let us know if you have any questions.