 What's up guys, my name is Michael and welcome to my YouTube channel. Today, we're going to go over the problem small factorial, which is basically you're given n and you need to calculate n factorial. Okay. So the problem about this problem is that n is between one and 100. And this is a problem because at the last value of 100, I do 100 factorial, right? I'm going to get an overflow. So that's not a good idea. Right? If you get an overflow, this is a huge problem. So in Java, you there's a way to do it. We'll use big integer class, which is basically they convert into strings, and then they do the operation for you to print out large numbers in C++. You cannot do that. Right. So I'm actually going to explain a better way to do it in C++ is how to multiply numbers, avoid overflow using arrays. So the maximum possible value for an unsigned n is two to the 32 minus one. So you get an overflow from if you get past this. And the maximum value for 64 long n is two to the 64 minus one, a digit of 100 factorial is going to have at most probably 200 digits, any digits. So the best way to do this is actually create arrays, an array of 200 values, a size 200, and then multiply it by yourself. Say I have a number 120. Okay. So in I'm going to create an array of 200 with all the values and array of zero is going to have zero array of one is going to have value to an array of two is going to have value one. Okay, so this 120 is going to be represented as an array of size 200. And this is going to be the zeroth place one place and this is going to be the second place. Okay, that's what this is going to do. Now let's say we have a value of 45 and we want to multiply it with a value 37. It should lies 45. Starting with a zero is going to be five. And at one is going to be four. So this is going to represent each digits of a one zero in this array. Let's call m is going to be the number of digits of our number first number so number of digits of our first number and this is going to be equal to two, because that's how many digits we have, there's only two digits. Right, there's 45, there's two digits, four and five. Okay. So now, if we're going to multiply this number 45 by 37, what are we going to do? For starters, we're going to loop through this array from index zero up to one. Okay, so index zero up to one. Index zero up to one, right, because we only have two digits here. And we're going to actually multiply each number in each digit here by 37. Right, because that's how we that's how you integer, integer multiplication works right you if you were to do it normally. You seven times five, 35 carry the three, seven times four is 28 plus three is 31. Then we do three times five is 15. Carry the 112 plus one is going to be 13. Then we have five, six, six, one 1665 right. So we're going to basically do the same thing we're going to take five times 37. So to do five times 37, I'm going to first move this, we are going to get 185. Okay, so now if you're going to do this regular multiplication, you have to carry the one and do every single carry right when you when you do integer multiplication division right. So what am I going to do from here now, we're going to have a temporary variable called temp and initialize it to zero. Then at every single iteration, we are going to add, we're going to multiply our each our x value by every single value in this index and we're going to add it to temp. Okay, that's what we're going to do. So in this case, we had took 37 multiplied by five, which is going to give us 185 and we have to add it to temp. Okay, so temp is going to 185 plus temp of zero is going to give us 185 new value at the current index. Current index, a of index, so at the current index I'm looping through is going to equal to the last digit that we just added. So this last digit five, we're going to mod by 10, right to get this last digit of five. So this is going to still equal to five, right. Then temp, we're going to carry the rest of this value. So this value of temp is going to get added. So temp is going to actually now get added to the rest of the value, which is 18. It's going to just like carrying the leftover to the other side when you're doing regular multiplication. Now the next iteration of index one is equal to four, right, we're going to multiply this for value by the same number that we have 37. And now we're going to get and now what we're going to do is we're going to take this number and add it with 18. So our temporary value is 18. So we're going to have 148 plus 18. And we're going to have 6166. Now at 166, this current value of the ones place of this six, right, this current value of the ones place of six is now going to get replaced. So this is going to this digit a of one is going to actually going to equal to six, right, because we're getting the ones digit of six. And the rest of temp is going to get equal now to 16. Okay. Now we have 65 and 16 right 65 for this and 16. Now our number of digits is over because we're done iterating through all our digits, but we still have value in temp, right? So what we're going to do is that we are going to append it last digit of our temp, and we're going to append it to our array. So our array currently have is 56 right at the zeroth and ones place, right? Now we're going to append the last digit of temp, which is a six and append it here. So now this is at the pen, at the end of the value, then we just divide temp by 10 to get one, then we have one. Now we're going to append this value again, because right, because we divide by 10, now we're one, we're going to append this value again to the end of this by modding by 10. So now it's going to have one here. And then we divide by 10. So then we get we were done with our temp array. It's now equal to zero. So now we have 5661. Now our answer is going to be 5661. And then we just looped from the end of the array. So here we have 5661. We're going to start at the last digit of four, and we're going to loop to the from the beginning down to zero. So now we have four is going to print out 1665, which is going to be our answer in our in our end result. So that's basically how you do this problem. And I basically explain how you do it, you basically add multiply each digit by the value you're going to multiply by, and then you carry the remainder and continue doing this. So yeah, each time you append the value into your end array. And that's it. And that's it. I hope you guys enjoy this video. I'm going to show you the code now. And then yeah, I'll explain the code, explain the code. So first we read in the test cases, and then we read while t minus minus we do this. We're going to read in our value of n, which is the value we need to calculate n factorial of. We create our array of 200 digits, right? So a is going to have 200 digits. We initialize the first digit to one, because this is the first value that we have to multiply it with right we have to one times one, one times two, one times the one one times two times three times four times five, all the way up to whatever value of n is right. And we're going to create our initial m, which is the number of digits we have originally is going to equal to one. So now I'm going to bring start my for loop. And my for loop is going to start from one up less than or equal to n. And the reason why I do less than or equal to n because we want to multiply all the values from one up to n. And those are the values we want to include n, right? We're not just excluding n, we have to multiply all values from one to n. So if n is five, we have to multiply all values one, two, three, four, five, right? We create a temporary number, which is going to be the carry that we're going to pass in over to the next value that we're going to do. Okay, so now we're going to loop through all of our integers in our array and initial integers in our array. This is going to represent the multiplying every single digit in our initial value by the number I, right? So here our for loop I equals one to n, or less than or equal to n, this I we're going to multiply I by whatever our current number is for each of our digits of our current number. And we're going to keep track of that and update it every single time. So it's going to do multiply one by one, then it's going to multiply two by that value than three by that value four by five five five by that value all the way up to n. Okay, so that's why we have this index. This loop goes through all the number of digits that we're currently going up to. So it's multiplying every single digit. Okay, now we have their integer x is going to equal to the current I. So this I represents the current, all the numbers are adding up multiplying two, right? So we have one times two times two times four times five. This I represents the current number we're multiplying. So if we're multiplying like two by the number than three by the number four by number five by number, that's what this I represents, right? It's a value we're going to multiply by. AI index is going to represent the current index at the digit that we're multiplying by. So this actually represents where this is the value we're multiplying by every single digit, right? So remember our current number, we have that current number and we're going to have to multiply every single digit in our current number by I, right? So that's what we have to do. And this we're going to add by 10, because like I explained in our previous solution, that's what we have to do. After that, I set a at index is going to equal to the last digit of x that we multiply by over. And then I divide temporary or temporary variable is going to be the carry over and the carry over is whatever value that we had originally divided by 10. Okay, because that's going to give us the carry over. So this represents storing the last digit of our carry over and then this is going to store the carry, right? So like, yeah, that's what it's going to store. At the end, while my temporary, the while the leftover is greater than zero, what we're going to do is we are going to add whatever value appended to the end of our array. That's what we're going to do. So that's what this does. So at the number of digits is going to equal to temp mod by 10, right? So that appends it every single last digit to the end of the array. And while this is happening, I'm also increasing m plus plus. So this is going to add the number of digits by one after every single iteration afterwards. So after this is set, it's going to add m by one. Okay, that's what it is. This is post incrementation. That's what it is doing. And then I divide by temporary by 10 every single time, because that's I'm appending every single digit from our temp, our leftover carry to the end. Okay. At the end, we are going to start our counter is going to equal to m minus one, which is the number of digits minus one, because that's what we're storing the array from. We are indexing it from zero. So that's how we have to do that for that's what we have that for. And then while it's greater than equal to zero, I minus minus, and I print out every single digit. And then at the end, I just print out a new line. So that yeah, that's basically how you do this problem. This was a pretty, pretty cool trick that you learn about how to multiply numbers by doing integer multiplication using arrays. It's pretty fun, pretty cool number. But yeah, Ray Combs subscribe. Hope you guys like this video. I'll check you guys later.