 What's up guys? Mike the coder here. Today we are going to go over Euler-toiler function. This is another problem on spodge basically that you're given The Euler-toiler function is just like the toiler of a positive integer n is Defined to be the number of positive integers less than or equal to n that are co-prime to n When we say co-prime that just means the values of the GCD of the integers Between it and to n is equal to 1. That's what co-prime means like the GCD of the values for it So how would you do this problem? basically, it's You just have to loop through it and I'll just show you the code because if you could try doing it the normal way I'll loop through looping through it like how I did it. So here I just did Actually, let's let's let's go back to the first one that I tried Okay here what I try to do is I just loop through from 1 to n Okay, so I read a number test cases Read an n and I just have to count to equal to 0 and then I just looped through from 1 to n and I just see if the GCD of the current value of the number i and The value of n is equal to 1 that I just increment count by 1 now This is actually we'll get you TLE because I don't know if they're they're really They're really strict about the time. So what we're what are we gonna do? Well, one thing we could do is that we could actually instead of going from 1 to n We could loop from 1 to the square root of n so that's why I try doing that and I When I try doing that, I still got TLE. So here I show you we're looking from 1 to the square root of n Here's a trick you could use you could do if I'm looping from i is equal to 1 and i times i is less than i times i is less than or equal to n This actually loops you from 1 to the square root of n without having to use the square root function So this is a little bit faster, but it's still not as fast So I'll just show you guys what the code is for the actual solution that gets accepted Okay, so it's the same thing out of loop is it gonna loop from Actually, we're gonna start from 2 instead of 1 we're gonna start from 2 and we're gonna loop to the square root of n Right, so this is out of loop. Alright, what are we what are we gonna do now? Okay, so we're gonna have this this Result result is gonna store the current value of n and what we're gonna do is that every single time that it's divisible by or Temporary variable of result is divisible by the current value of i what we're gonna do is keep dividing by So we're just gonna keep dividing by it over and over again So in this case while n mod by i is equal to equal to zero. So while it's divisible by it We're just gonna keep dividing our Our n by our i. Okay, so we're just gonna keep dividing it over and over again. Okay Then what are we gonna do is that we are going to take the result and then we are going to subtract result minus equal result over i And why did we do this because if we're gonna keep dividing by n? divided by equal to i basically what we're doing is that We are just subtracting the values. They're already divisible by I so then what that does is it enables us to basically have the end result to be the values that are Co-prime that are basically the gcd is equal to one So there's actually a better explanation here. I'll just show you guys it. What things it oiler to oiler function oiler to oiler function I Think it was on yeah right here oiler to oiler oilers toward tolent function. There's one of geeks for geeks and They have a good job explaining this so If you go on geeks for geeks, here's what they do This actually not a good formula This they're doing here Anyway, I digress Yeah, this part. Okay, so basically what they're doing is that they're going to I kind of got this from geeks for geeks Actually, because like this is a good implementation, but basically what we're gonna do is that if? If if the current value is divisible by it, we're gonna subtract all multiples from it So all multiples from one to n and all multiples of p will have a gcd of more than one, right? So like if if if if we subtract all the multiples that that that have that are Multiple of p from one to n those are the values that have gcd more than one So what we're gonna have is we're gonna have the values of n that are going to be Only have gcd of one right greatest common in a divisor being one So if we subtract all the values that are divisible by it then in the end We're gonna have all the values that are just only divisible Whose gcd is only one. Okay, so like here There's a better way to do it. So basically We didn't initialize as n and what we're gonna do is we're gonna check if it's this this checks If it's a prime factor by the way, so if it's a prime factor then Yeah, if it's if p is a prime factor So p is a prime factor then what we're gonna do is we're kind of keep dividing The end by the prime factor over and over again So in the end what we're gonna do is we're going to subtract result divided by the prime factor of p and The reason why we do this is if it's yes, then we're gonna update and end results So then in the end is that Basically our result is going to have all the values of whose gcd is not One right if we subtract all the values whose multiples of it So if a result divided by p is just takes all the values that are multiples of p That's what it's doing. Okay, so then we subtract by that and the reason why we do n divided by Equal p is that this what this is doing is it's gonna keep Keep dividing n by the prime number of p So remember p is a prime number right prime factors and then where if we keep dividing n then it updates our n so then that means our Our final update of n that we're looping to gets Minimized over and over again. So that basically changes the Factors of it that we're going through because here is we're going through the prime factors, right? so if we keep dividing by p that's gonna change the the The top part of our n. Okay, so then if n it has a prime factor greater than square root of n Then we have to subtract The current value of n from our result because that means that n is also a prime factor and Result we need to get rid of the prime factor of current value Yeah, we have to get rid of that current value multiple that's also Visible by result and then we just return result. So that's basically just the code. It's not much to it Basically I'll just reiterate again We're just gonna go through every every prime factor here and if it divides by we're gonna subtract all the multiples from 1 to n Then what that has is that in the end and we're gonna have the GCD of only the values that have value 1 Okay, then we're gonna update n by repeatedly dividing it by p and that basically just reduces the problem Okay, so from the current value of n we're gonna reduce that So then the yeah, if the reduced n is more than 1 then move all multiples of n from the result So that's what we do here down there But yeah, that's basically basically just a problem. I hope you guys enjoy this video right come subscribe I'll check you guys later. Peace