 What's up guys, my name is Michael and welcome to my YouTube channel. Today, we're going to go over the next problem on spod list of classical algorithm problems. Basically, what I did was I just literally just searched up spod classical problems and I just sorted by users, users that solved it. Yeah, we are going to do the next problem, which is prime generator and I'll put a link in the description down below. Basically, you're given two numbers M and N, we know that N is greater than or equal to M. Your task is just to generate all the prime numbers between. So yeah, and you have t test cases. So let's do this t lines. To do this problem, basically, you have to run this algorithm called C of R S Theos. And I hope that I pronounced that right. So basically, C of error is basically a way to tell the fine prime numbers lesson are equal to a number N. So how this algorithm works is that it starts, it goes from two to the square root of N. And the reason why we go square root of N is because that's the last bound that you have to check how this works is basically that basically you what it's doing is it's looping from two to the square root of N. And then for each multiple of the square of two, three, four, five, six, all the way up to the square root of N, it marks those values as not prime, right? Because it's not prime. Like it's divisible by two square, three square, four square. We could just show you an example right now here. Here we have an example of N equal 50. You want to print all the prime numbers less than or equal to 50. So what they did was here is they just first we create a list of numbers two to 50. And then what the algorithm does is that it takes the number first, it picks two first, and then it marks all the ones that are divisible by two. They're greater than or equal to the square of it. So two square, three square, four square, two square, two to the third, two to the fourth, two to the fifth, all those values are divisible by two. So we mark them as not prime. Then we go to three, three square, three to the third, three to the fourth, three to the fifth, so on and so forth. Mark those as not prime because they're divisible by three. And then we go to four and then five. So they skip four, but let's go to five, okay? The next one is five, and then we mark all the multiples of five that are greater than or equal to the square of it. So five, five square, five to the third, stuff like that. All the multiples of five are not prime, right? So we mark those as not prime. And then we keep going on so on so forth. In the end, we are just the values that are unmarked, right? Are going to be the ones that are prime. So like two, three, five, seven, 11, 13, 17, 19, 23, 29, 31, 37, 41. This is a code in C++. So this Boolean is here used to represent, to mark all the ones that are prime or not prime. If it's prime, it'll mark it as true. If it's not prime, it's false. And this memset just sets them all to true in the beginning, right? And then here we're going to start from two and go up to the square root of n. That's what this is, p times p, so that's when you go to n, right? If it's true, so in the beginning, they're all true, right? Then we're going to update the values from the multiples of it. So in this starting case, it's going to start from two. So i is equal to p times p. p times c, so remember, p is starting from two and goes into the square root of n. So here, this second loop is going to mark all the values that are the multiple of it, multiple of it, right? So it's going to say, it's going to mark two times. So i is going to start from p times p. So in this case, p is equal to two. So i is going to go to equal to four, right? And then while i is also going to equal to n, right? The last value there. We're going to keep adding p and mark those as false, because those are the multiples of it, right? So this is going to be four, four is not prime. Then it's going to say six is not prime. It's going to say eight is not prime. Ten is not prime, yada, yada, yada, so on and so forth. Then when it comes back to the top of this, it's going to start with three. Three was prime. Then we're going to set all the multiple values of three. The multiples of three, or we're going to set them as false. And then it's going to go so on and so forth until we get to the square root of n, okay? So that's basically the gist of c of s. So now we're going to actually use this algorithm to code up the problem. Basically, we are going to do something similar to the c, except that instead of pre-computing all the values in an array, we just print them out, all right? So first of all, what I did was I had a t, which is the number of test cases we have to read in, so I read that in. And then while t minus minus, that's the number of test cases left, right? We're going to subtract one every time we have a test case. We're going to read in m and n, right? Remember, m is the left bound, n is the right bound. And we want to print out the each prime that's between m and n. So over here, I have a for loop. I'm going to start i is equal to m, and I go up to less than or equal to n. And then I keep adding one. And then I have a function that checks if it's prime, then I just print it out, okay? So now in the function is prime, I do something similar to exactly what the c does. So we know that if something is prime, if it's less than two, it's not prime, right? If a number is less than two, it's not prime, because zero and one is not prime, right? If it's equal to two, we know two is prime, so we're going to return true for that. If it's divisible by two, we know it's not prime, right? So it's going to return false. So over here, I do something similar to the c, I start from three instead of two, because, you know, we know this is just a function that returns if it's prime or not. So I just start from three, and I go up to the square root of n. So three times three is less than or equal to square root n. And I'm add by two every time, because I know that if you increment by two every time, that's like three, then five, then seven, then nine, nine, then 11, so on and so forth. We know these are the numbers that we have to check. These are the odd numbers that we have to check over and over again, right? And then I check if the number is divisible by these odd numbers, because if it is divisible by these odd numbers, then I know it's not prime, right? So then I return false. So I do that over and over again until I get to the end, and that's basically the code. And then if it is prime, in the end, if all these cases are not true, then it's going to return true, and that means it's prime. And then, yeah, then if it's prime in my for loop, then I just print out the number i. Okay, and that's pretty much it. I already submitted this, so I know that it's got accepted. But yeah, that's pretty much the gist of this code. Hope you guys understand this. Hope you guys understood this code. Raycoms subscribe. It's basically a similar to the sieve, and I explained the sieve to you guys already. And this is basically the exact, pretty similar to it. But yeah, instead of pre-computing, we just print out the prime, if it's prime or not. But yeah, that's pretty much this whole code. Raycoms subscribe, and I'm going to check you guys later. Peace.