Modular Arithmetic in Python
Transcript
hi everyone in this video i'll show you how to conduct modular arithmetic using python the packages we will use include date time and pi tz let's start with defining what modular arithmetic is which is the process where the modulo operation returns the remainder when dividing the equation below outlines the operation the numbers within the operation go up to b which is known as the modulus which sets the range of outputs here we have the equation a which is the
number being divided or the dividend divided by b which is the divisor and the modulus and the result will output a quotient q which is the number of times that b can go into a will also have a remainder which is going to be the leftover number this will make more sense i think once we actually go through an example now let's review how we can conduct modular arithmetic within python and we do this using the percentage sign symbol this is a built-in modulo expression
that works on both integers and floating numbers in this video just for simplicity we'll focus only on integers you can also use different functions from packages such as fmod to conduct module operations there are differences between the different functions and i if you want to learn more about them i recommend you go to the sources here on the differences between the two let's move on to actually conducting a modulo operation our first example we have six modulo two or six mod two the
result of the modulo operation is zero and to explain how this works we have six and we divide it by two and that's equal to six and we know that two can go into six three times so we have two times three which is six minus six is going to get zero we have a quotient or the number of times that two can go into three two can go into six which is three and a remainder of zero our second example is when we have a number that or a divisor that doesn't go evenly into the dividend in this case we
have five mod two is equal to one and to explain how this works we have five divided by two we can do five minus two times two which is four and when we subtract five minus four we have a remainder of one so in this case two can go into five twice and once it does that we'll also have a remainder of one we can also check this by using the modulo function within python and i'll use that now and i'll also print out the results and we can see that our results that we were able to compute
here match up with the python output where 6 modulo 2 is equal to 0 and 5 modulo 2 is equal to 1. we can also take a look how these divide into each other again we have this laid out here but we can take a quick look just to get a better understanding 6 divided by 2 that goes into it 3 times and for 5 we have 5 divided by 2 we have a quotient of 2 but then we have this leftover and it should makes it should be clear now how this works but if it doesn't i highly recommend you play with
a few other numbers just to get a better idea of how this operation works we can also create a modulo function from scratch and the way that we can do this is we can use a division operator known as floor division and this is another built built-in math expression within python instead of returning the remainder what floor division does is it returns the quotient or the whole number that goes into the dividend now let's start building out our function the first thing that we need to do is we need to write out def to let python know that we're creating a function modulo and we'll take in two numbers i'll call this the divisor and then i'll call this the modulus or the number of times it goes around i'll just call it mod
then what we'll do is we are going to return the divisor and we're going to subtract it by the divisor and we do modulo the modulus and multiply it by the modulus as well and let's take a look at how that will work so what i'll do is i'll just put i'll do we'll take a look at 5 modulo 2 again in this case what we'll do is we're going to take 5 and we're going to use floor division and we get a quotient of 2 and that makes sense because that's what we had here is our quotient next what we have to do is we need to multiply by the modulus in this case we multiply by two and we have that four
then finally we're going to take this all and we're going to subtract it by the divisor or five and this will return the modulus and we we have an identity that can prove this out i'll include a link in the resources that explains this more in depth but this is how we get our modulus modulo function from scratch run this and what we could do is we can first of all check our first two examples to make sure it works again for the function it's going to take the divisor which is going to be 6 and 5 and then our modulus or dividend is going to be 2 for both of these they should be 0 and 1 respectively let's check out if our function works great it was able to print out the correct output for the modulus operation what we could do is we can also check against a multiple numbers what i'll do is i am going to
print out a nested for loop and i am going to go over a certain range let's say negative 10 to 10. and these are all going to be integers so going up going from negative 10 up to but not including 10 and then i'm also going to have a modulus i'll actually call this divisor and i'll call this well i'll call this div and i'll call this modulus
what we're going to do in this loop is we're going to print out the divisor the modulus then we're going to run them through our function which is the modulo function and we're going to check that it is equal to python's modulus function is equal to the function that we created let's run this great and if we went through all this they should all match up and if there was a false then that means that there be an issue with our function but it looks like we were able to create this correctly
and then we can just spot check this by going over the numbers on in our head 9 modulus nine that should equal zero because nine goes into nine once and there's a remainder of zero nine modulo eight that should be one because eight goes into nine once and then there's a remainder of one same thing for all of the other numbers which seem to match up then for our final function that we're going to create is we're actually going to cut out a clock using modular arithmetic
and what we'll do is we're going to program this clock to return an hour in military time and for military time we have it listed out here it goes from zero up to 2300 once it hits goes past 2300 it doesn't go to 2400 it goes to zero zero zero zero which is the way it's stylized and it keeps wrapping around the modulo is going to be 24 in this case and we'll see how this works what we'll do is we're not going to focus on the minutes we're just going to focus on the hour
what we're going to program is we want it to return either the current time or the future time we can also program out the specific time zone if you want to see time zones based in different parts such as asia or africa let's start with writing our function the first thing that i'm going to do is i am going to create our function call it clock and i'm actually going to put two defaults what i want to do is i want to we're going to add or subtract the current time
add or subtract to the current time and i'll explain that as we get more into the code but the default hour is going to be 0. we're also going to put in a time zone and for the time zone i'll use as the default is eastern standard time you can set the default to wherever you live i'll review the different options you have there the first thing that we need to define in our function is going to be a variable called currenttime and that is going to use the datetime function as
well as the pi tz function and what i'm going to do is i am going to get the date time to get the time now but i also want to get it for a specific time zone otherwise it will return universal time which i don't think many people use personally and the way that we're going to get the time zone is we're going to use the pi tz time zone and i have my tease my time zone already defined which is eastern standard time then what i want to do is we can get day month hour minute i want to get the hour
because that's what we're going to be running our modulus arithmetic on next is i'll call a variable called updated time and this variable is going to take the current time and then it's going to add the hour that we put in there and we're going to do this modulo 24 because again if we have this military time it goes from 0 up to 2300 once it hits 2400 it resets to zero and we'll see different examples of this but if we just take a look at this quickly if we do
2100 if the time is 2100 right now and we want to get the time five hours from now it won't be 2500 it will be zero 100 because it will go past the modulus of 24 and that's what we'll do so we'll take a look at that once we get into it then we have our updated time we also want to take a look at the minutes and the way that we'll do that is we're just going to take this date time paste it here and we're going to change this to minute and we won't change that we'll just add that into our string
value now that we have the times and we can project the time forward or backward or we can just get the current time by leaving the default hour at zero we also want to stylize it in this military time time format and we'll do that by converting the numbers into strings and we can see that for zero at three am and zero three hundred if we go out to nine pm standard time the way it's stylized for military time is twenty one hundred we'll do some format we'll change the integers here
into strings and the way that that will do that is if the length of the string in minutes is less than one so meaning that the minutes are less than ten it's zero nine we still want the zero in there and we'll say equal to one then we want to run this if else statement and if the minutes are equal to 1 we're going to set minutes equal to and we're going to add a 0 to the front we're going to concatenate and we're going to turn the string the minutes into string and we're going to override it
else what we'll do is we'll just turn minutes into string format so str minutes because this will all be double digits this will be 10 minutes up to 59 minutes we'll do the same thing for the hour and we'll say that if the length of the updated time which is going to be the hour is equal to 1 meaning that the time goes from 0 100 up to 0 up to 0 900 we want to put the 0 in front of it what we'll do is similarly we'll concatenate it and we'll do print and we'll also put in our else statement
in case that the time is already past 0 900 on to 100 up to 2300 and you'll also notice that in this function we're just going to print this out we're actually not going to return anything but that's fine this is just meant to be a learning and doesn't really need to output specific data that we need to work with okay let's run this function and let's try it out what i'll do is i'll run it and we already have our defaults in there so i don't have to define anything i could just run this
i could just say i want the current time which is the hour is equal to zero and i want it for eastern standard time meaning i can leave the parameters on filled because they're already filled by default let's run this great and we have our military time so currently it is 2320 in military in eastern standard time and what i could do is if i want to project an hour from now this is going to be 23 and i'm going to do one hour and what this will be is it will be 24 modulo 24
and we know from previous examples that this will return zero let's check if our clock is going to return that so it should return 000 and it will still give us the 20 minutes because we're just incrementing by hours we're not incrementing by minutes and let's run this great so one hour from now in eastern standard time it will be zero twenty zero hundred twenty one we can also go backwards as well we can put negative one so we want to know what the previous time was an hour before from now
it was 221. and what we could also do is if you are in a different time zone we can check that out as well i'll still keep it to the current hour and i'll put zero and let's say that i want to get the current time for addis ababa and i have to state that tz is equal to addis ababa let's run that and currently it is 0 7 22 over in addis ababa and if you want to get the time zone specific to where you are you can check out the list of time zones provided by pi tz and there's a
function called all time zones it's actually returns a list of time zones let's run this and you can find the specific location you are if you want to try this out wherever you are i hope that this video was helpful and before i leave it off i am also going to just review a few real world applications of modular arithmetic and it's very useful in cryptographic applications such as the diffie-hellman key exchange which usually utilizes modular arithmetic diffie-hellman
specifically allows two parties to securely exchange cryptographic keys over public channel this is just one example of modular arithmetic within cryptography it's very popular in the field and previous videos i made you utilize modular arithmetic in the cryptographic videos module arithmetic can also be used to track inventory within systems upc codes or barcodes on items such as cereal boxes and soup cans can help grocery stores track the items in a store
there are 10 digits that comprise a upc that are used to track it and there's a checksum that uses modular arithmetic to verify that the upcs are valid i included a source here by professor ruth berger that goes into this in detail very similar another real world process that uses modular arithmetic is the international standard book number or isbn and this is used to verify books and there are unique values that are made using module arithmetic again you can check out this article by
professor ruth berger that goes in depth on how that's done thank you everybody for watching i've listed a bunch of different resources that i've used and that you can also check out in order to go more in depth into modular arithmetic it's very useful in real world applications and again this article by ruth berger goes over almost a dozen applications where modular arithmetic is used here are a few websites that go into it into detail i also included python resources in case you want to check out the differences in how modular arithmetic is done and you can even see how other languages tackle it like c versus python thanks again for watching if you like the video feel free to like and subscribe you can also connect with me on linkedin twitter and github thank you everybody for watching and happy coding

Ask anything
We've read this whole study set, so questions about it get the best answers.