 What's up guys, my name is Michael and welcome to my YouTube channel today I normally don't upload two videos in a day, but might as well upload another video because the first video is pretty short And also, I don't feel like you guys really really care that much about prefix sums So I'm gonna go over another video one of the guys asked me a comment to go over project Euler problem one and This is the problem. I normally don't do project Euler questions mainly because they're mostly with number theory and Number theory is not that much with algorithms But it does help with solving some competitive programming questions Like if you could figure out a mathematical equation a lot of the times that that's way faster than trying to come up with an algorithm You know, you know, I mean like because a mathematical equations like O of one like O of one constant time It just takes one computation to solve it. So anyway, we're gonna go over this problem. So basically the problem is find the sum of all multiples of Three or five below one thousand. Okay, so what does this mean? So pretty much you what you could do is you could go through all the You could create a for loop and start from int i equals zero, right? Not in i equals zero, okay so you could start a for loop for Starting from one and go up to a thousand and then every time you have a number That's divisible by three or five. You could sum it up Alright, so you could do like sum plus equal to whatever that number is But the problem is that that you technically don't have to do that So we're gonna find one mathematical equation to solve this problem So we don't have to use a for loop to loop through all the numbers and then just count Trying to find them out. Okay. So first of all before I start this Problem we have to look at this. We need to find all sums We need to find the sum of all multiples of three or five or five. This is a five by the way. Sorry. This is a five I hope you guys can see it. It's a three or five below a thousand. Okay, so But whenever I see a problem with or I Know that I have to use this formula to do things. Okay So I don't know what the specifics is called, but let me search search it up real quick Whenever you see an or we need to use something called the inclusion exclusion principle. Okay, so before I start with this I have to explain what that is. So let's say I have this set A and B, okay, and I have some group numbers in a and In B, right and the intersection between them are all the numbers that are in both A and B. Okay So this is a Venn diagram This a is just a set of all group of numbers and B is a set another set of group of numbers Okay, and this is the intersection. This inside is the intersection. Okay Inside is the intersection of A and B. So we write that normally in probability as this right intersection A and B So this is all the group of numbers that are in A and in B Okay, now you might be thinking Um, what does this have to do with the Or okay with the find sum of all the multiples of three or five below a thousand Well, first of all Has a lot of thing to do with it. Okay, so Whenever we see a Group of numbers right like a set A and B like a set A and then a set B Uh, let's say I want to find the group of numbers that are in A or B So it could be in either A or B. So what does that mean? That means that This uh, so okay, let me let me draw this again So this this was the Venn diagram of A A Intersection B, right? So that's the group of numbers in A and in B. Well, what about the group of numbers in A or B? So in A or B So what does that mean? That means I could be in All the group of numbers in A or I could be in all group of numbers of B So that basically means that I'm gonna Shade in the all the numbers of it. So that that's this group of numbers in A Or the group of numbers in B Okay And we the we show that in this formulation by calling it this A union B So this is a group of numbers in A and or the group of numbers in B Now you might be thinking well How do I calculate A or B right the group of numbers A or in group of numbers B? Well, how you would do it is that you would actually have to add up the group of numbers in A And then add up B. Then you have to subtract by the intersection Of this, okay, and I'll show you why so like A union B is actually going to equal to A plus B minus A um A minus A intersection B, okay And the reason why is because You're double counting essentially So I'll show you guys Let's say I let's say I have intersection of just A Just a okay, and if I were to This is this is all the group of numbers of A Okay It's like that. Okay. That's all the group of numbers of A now if I add up the group of numbers in B I'm adding this Okay So you could see that I'm double counting here You already could see I'm already double counting because I'm adding up Group of numbers in B and I'm double counting here already There are some group of numbers in A that overlap with B So I have to subtract at least one overlap to get My normal A union B, okay So so I have to do A or B. It was equal to A plus B Then minus A Intersection B and that's I have to subtract the values that I overlap Okay, so I hope you guys understand the picture I was drawing so If you add up B You're overlapping the values of B and then this this intersection got counted twice and we don't need that intersection So we have to subtract by at least one intersection of that. Okay. So now now that we have this under control I have to think about this I now I explain the inclusion exclusion principle to you guys We need to find the sum of all multiples of of three or five below a thousand. So what does that mean? that means multiples of three or five below a thousand So what am I going to do? I'm going to say, okay A is going to equal to the multiples of three below a thousand So A is going to equal to multiples of three And then B is going to equal to multiples of five over a thousand. Oh, I I accidentally draw this twice. Sorry guys So A is going to equal to multiples of three below a thousand and then B is going to equal to multiples of five over a thousand So the intersection of the multiples of three or five below a thousand is going to be a or b. So that's going to be the multiples of three or five over a thousand. So let's call this three or five over over or below a thousand below a thousand my bad three or five below a thousand below one thousand okay so that's what this is three or five below a thousand that's going to be a union b okay and then that's going to equal to a plus b minus a intersection b so what's a intersection b a intersection b is going to be multiples of three and five whoops and five below one thousand okay so once I just I'm just gonna get this out of the way okay so we basically what we have to do is we have to add up the multiples of three below a thousand with the multiples of five below a thousand and then subtract by the multiples of three and five over a thousand okay below a thousand right so that's what we have to do that's I'm just explaining that that we have to do okay so how do we find the multiples of three that are below a thousand so let's just sum it up okay so we're gonna write the summation like this okay so a is going to equal to multiples of three the sum of multiples of three sum of multiples of three below a thousand right let's just let's just let's just write it out we're gonna do three plus six plus nine what's three times four three times four is 12 I think yeah three times four is 12 then three times five is 15 and we're gonna go up to okay so what are the numbers what's the last number that is below a thousand that's a multiple of three that's 999 okay so that's 999 okay all right now I'm gonna write the multiples of five below a thousand so what is that multiples of five five below a thousand I'll just write it quickly okay right multiples of five below a thousand that's gonna equal to five plus 10 plus 15 plus 20 and up to what's the last number below a thousand it's 995 right so that's what this is doing okay so I hope I'm showing you guys how this works okay so now that we know that let's let's think of let's come up with an equation okay so what do I see on here number of three multiples of three below a thousand well three I have three plus six plus nine plus 12 plus 15 plus up to 999 well all these good values could be I could distribute a three out I could factor three out so I'm gonna do equal three times one plus two plus three plus four and then plus five and it's gonna go up to 333 I think yeah yeah yeah 333 and for five I'm gonna do the same thing I'm going to factor out of five so I'm gonna be one plus two plus three plus four plus five yeah up to 995 what's 995 divided by five that's my trusty calculated 199 okay so that I basically all I did was just factor out the five and the three on both of these okay so now what do we do there is a way to sum up the values of 1 to n okay there's a mathematical equation that helps you sum up the values from 1 to n and I'm going to show you guys I think there's a let's see let's see to do that we are actually going to take a my trusty I think it's called I think it's n times sum of sum of arithmetic series okay there's a sum of arithmetic series formula that adds up the values of 1 to n I think let's see this one a1 plus a of n over 2 times n um a1 plus a of n over 2 so yeah this formula is the sum of arithmetic series is a1 plus a n over 2 times by n which is n is the what is n and as the number of terms I think wait some more terms okay wait hold up let me see sum up values sum up sum up grad sequence up from 1 to I don't want to use the actual thing I want to go over the actual term there's like a formula that lets you do it yeah it's n times n plus 1 over 2 and that sums up all the the numbers from 1 to n I believe so that's is going to be from what are the numbers from 1 to 333 it's going to be summing up from 1 to 333 is going to be n times n plus 1 over 2 yeah and times n plus 1 over 2 so what is our n so the mathematical equation that lets you sum up from the numbers from 1 to n is going to be n times n plus 1 over 2 and what is that our n is going to be 333 so that's going to be 333 times 333 plus 1 which is 4 334 over 2 okay and that's going to that's going to be our multiples of 3 below a thousand and we're gonna do the same thing for 5 so what's the sum of the numbers from 1 to not 199 n times n plus 1 over 2 so 199 times 199 plus 1 which is 200 and then divided by 2 okay and that's that's that's it that's pretty much it hold up what am I doing 199 that's pretty much it yeah so now you have both of these values now we have to subtract by the the difference which is going to be yeah so now we have to subtract by the the multiples of 3 and 5 below a thousand okay so I'm gonna get another sheet of paper okay so basically guys I just rewrote the exact same thing of what I wrote on the other sheet of paper this is a this is the equation that that gives you the sum of 1 to n and that's equal to n times n plus 1 over 2 and I basically use that at the same values of the same sequence that we're going through 333 times 234 over 2 and then we multiply by 3 and then we do the same thing over here 5 is equal to 199 times 200 over 2 times 5 okay so that's that's the sums that's a and b now we have to think about the a intersection b which is going to be the sum of 5 and 3 below a thousand so some of the multiples of 5 and 3 well what are the multiples of 5 and 3 multiples of 5 and 3 are 15 okay because 3 times 5 is 15 and that's what the multiples of 5 and 3 are so this is equal to some of multiples of 15 below a thousand okay and once we know that we just have to sum up all the numbers of 15 below a thousand and it's pretty much the exact same thing of 3 and 5 5 right alright guys so what do I have to do I pretty much have to write a mathematical function that given a value n it's going to sum up all the multiples of n below a thousand so all I have to do is I just have to plug in the multiples of 3 of a thousand plus the multiples of 5 over a thousand then subtract by multiples of 15 over a thousand so how do we we have to look back at how do we figure out a function to figure out for a and b okay so let's figure out what do we do we had first of all we had to find a number that's below a thousand okay so first of all we have to find out the last ending value that we have to end on below a thousand and in this case there was a 999 so what do we do we took 999 then divided by 3 and then we got 333 and then we actually took the whole sequence and divide by 3 so we had factor out of 3 and then we realized we could sum up the values from 1 to this last number and that will be pretty much the sum of the values of 1 to this last number and then that would be that we take that number and we plug that into this equation for the n times n plus 1 over 2 and that gave us our answer so what do we have to do so basically our mathematical function it's going to be this is going to be what it was this is going to be function some multiples below n okay below n okay some multiples below n alright this is going to be a pseudo code okay what are we going to do we're first going to take our the last number that we yet we need to find the last number so this is going to be int last num and what are we going to do we need to find the last number so what did we take we took the so we took the largest possible number that's actually possible that's below a thousand which is 999 so we're going to take 999 divided by our number n here okay so last number is going to equal to 999 divide by n then what are we going to do so then once we have this last number we have to plug this last number into our math equation of n times n plus 1 over 2 so that'll be last num times last num plus 1 and then we divided by 2 okay and then what do we do after that after we plugged in this last number into our math equation right here so 333 times 334 over 2 that's what we did that's what we did here we have to multiply the result by our multiple that we had that we divided it by so in our multiple in this case is going to be n because we're writing a function that gen that pretty much it's a generic value it's not just three but it's going to be n so we're going to multiply this by n and yeah that's pretty much it we just have to return this return n times last num multiply last num plus 1 divided by 2 and that's pretty much it so then now to to calculate the sums of multiples of three of three or five below a thousand it's going to be some multiples below we're going to call this function pass in three then plus sums below the exact same thing some multiple below pass in five then subtract some multiples below and then pass in 15 and that'll be your answer plug this pseudo code code this up guys I think you guys code this up it's not that it's not that difficult just take 999 divided by n get your last number then you sum up last number return n times last number times last number plus 1 over 2 and that's pretty much it and then once you have this function just plug this to do like a C out or something of multiple not some multiples below 3 plus some multiples of 5 minus some multiples below 15 that's all you have to do that's how you do this problem for some multiples of 3 or 5 below a thousand I could actually just coded this up with you guys I could just code this up just for in case if you want me to so I'm going to code it up right now alright guys so how do we do this we're going to basically just do the exact same thing so I'm going to create a function called int sum multiples below and then what am I going to do I'm going to take the last number find the last number in our sequence so it's last num is an equal to 999 divided by n okay so some num so when I say some multiples below this I should write a thousand thousand okay okay so I'm gonna do that and then what I'm going to do I'm going to turn n times last number times last number plus 1 over 2 so return n times last number times last number plus 1 then divide by 2 and yeah that's basically it that's how you do this problem so then now how are we going to do this let me just let me just like comment this whole thing out comment my old code I should learn short cuts comment but whatever okay now I'm gonna just do see out some multiples of a thousand plus some multiples below 5 plus some multiples below 15 and L okay now I just have to do this I'm gonna do this just for readability sake 3 5 and minus 15 okay now we just have to print it should just give us a number I already have I already know what the number is so 3 3 3 1 6 8 let's see is that the same number as what I got 3 yeah 2 3 3 1 6 8 2 3 3 1 6 8 yeah okay so that's that's the right answer of the problem one on project Euler I hope you guys enjoyed this video this was this was a lot of talking but yeah rate com subscribe I'll check you guys later peace