 What's up guys I just ran two miles, so I'm really tired I don't have the energy to make this video, but I'm gonna make a video anyway So bear with me even though I might sound like I don't know what the hell I'm talking about But I do know what I'm talking about. Anyway, let's get into this problem. All right can't on count on can't on So there's a proof in mathematics, which is the set of rational numbers is countable. So the proof works by basically just Labeling the set of rational numbers as of the diagram here So what do you have here you have 1 over 1 1 over 2 1 over 3 1 over 4 1 over 5 yada yada yada Right and then you have here we have 2 over 1 2 over 2 2 over 3 2 over 4 2 Over 5 yada yard Yada, then we have 3 over 1 through over 2 3 over 3 3 over 4 3 over 5 yada yada 4 over 1 4 over 2 for the 3 4 or 4 forward or five and 5 are over 1 5 over 2 5 or 3 5 over 4 5 over 5 Okay So in the diagram, the first term is one over one. So it's this, this term, the first term, right? One over one, do you see this? Yeah, right here. Okay. My mouse is on here. Okay. The second term is one over two, which is this. One over two. Okay. And the third term is two over one. So it's this two over one. And the fourth term is three over one. Which is this. Bottom, three over one. And the sixth term, fourth term is two over one. Yeah. Fifth term is two over two. So two over two here. And so on and so forth. Now basically our job is to write a program that basically given the number n, whatever number it is, it'll tell us the whatever term it is for the next term. Right, so in this case, our input was three. So what is the third term? Well if we go through the list value here, okay one over one is this, one over two is this, one over two is the second term, two over one is the third term, right? So the answer is two over one. The 14th term is, yeah, okay, so the 14th term we'll go over that later, but let's look at the seventh term. So the seventh term, how this works is it goes one over one, it goes right, one over two, then it goes diagonal, two over one, it goes down, three over one, so here one, two, three, four, then it goes upright, two over two, then it goes upright, one over three, so here one, two, three, four, five, six, and then it goes one right, which is one over four, which is the seventh term. The seventh term is one over four. So in order to solve this problem, you have to look at the pattern, so this is an ad hoc problem. It's not really like a, it's not a math problem or anything, but I'll show you the pattern by just drawing out the numbers. Okay so here, let's look at this. Remember one over one, one over two, one over three, one over four, one over five, okay? And then now we have two over one, okay I'll just keep drawing here, hold up. I'll just keep drawing, I keep drawing, keep drawing. Oh whoops, I just keep drawing. And then we're gonna move this up here, okay? And then yeah, let's keep drawing. I don't feel like I did anything in this video because I'm really lazy. But two over one, two over two, two over three, two over four, two over five, three over one, three over two, three over three, three over four, three over five, four over one, four over two, four over three, four over four, four over five, five over one, okay? Five over two, five over three, five over four, five over five, all right. So how does this pattern work? Well how it works is that you start on the top left, one over one. And so this is the first term, right? Then we go right one, so then now we have one over two. So this is the second term. Then we go diagonal down. So this is two over one, so this is the third. And then let's see, what was it? Two diagonal down, oh yeah, then we go one over, then we go down one. So now we go down one, so here, so that was the fourth term, three over one, okay? And now we're gonna go diagonal to the right, up. So we're gonna go here. So this is the fifth term, okay? And then we go diagonal up again, so here. This is the sixth term. Then we go one over the right, over four. So this is the seventh term. Diagonal down, two over third, eighth term. Diagonal down, three over second, ninth term. Diagonal down, four over one, tenth term. Then we go down, five over one, this is the eleventh term. Then we go diagonal up, so this is the 12th term. 13th, 14th, 15th, so this is 15th. So I hope you guys understand the pattern right now. So how the pattern works, if you wanna get the nth term, this is how it works. You start on the top left of one over one, you go right, then you go down, so you go right. You go right one spot, right? Then you go diagonal down, okay? Then from here, you go down one, then you go diagonal up, okay? So from here, you have three over one, so now we go diagonal up, so now we're gonna keep going diagonal up. So the sixth term is gonna be here. Once we add this back to the first line, right, again, we're gonna go one over to the right. So going one over right, that's our seventh term. So that's one over four. Then after that, we're gonna go diagonal down again. So we're gonna go to two thirds, three halves, four over one. After that, when we're at four over one, we're gonna go down one, okay? And then we're gonna go diagonal up. Once again, go all the way up diagonal up, up to one over five. Then after that, when we're on the top left, we're gonna go down again, down one. So then we have this is the 16th. So basically to get the nth term, what you do is you go right, one, diagonal down, go down one, diagonal up, go right one, diagonal down, go down one, diagonal up, right? And then once you get to the top one, you go down one, okay? So essentially is, is that you have, you have a few moves you have to do, okay? So here I'm gonna just minimize this real quick. Okay, so there's a few moves you have to do. So for one, you have to go right. So we're gonna go right, this is right. Then after that, you gotta go, this is one to the right, by the way, one to the right, one right. Okay, then you have to go diagonal down. So this is diagonal, okay? Then you gotta go one down, okay? Then you have to go diagonal upwards. So this is diagonal right. Okay, so then how do you do, how do you basically do these operations, okay? So we're just basically gonna keep repeating these operations over and over again until we get to our nth term, okay? So how do you do this? Okay, for starters, to go one to the right, all you have to do is if you look at the first term, is one over one, right? So if you go one to the right, what you do is you just add one to the denominator. So for that, you just basically just add one to the denominator, so that makes it one to the right. So that's really easy, right? You just add one to the denominator, then that's one over right, one to the right. Another thing is easy is going one down. How do you go one down? Well, if you go from the third term to the fourth term to go one down, you basically just add one to the numerator. So in this case, we have two over one. Now we add one to the numerator, so that goes down one term, so that now it's three over one. Okay? So that's basically, that's easy also. Okay, now here's a part, where's the hardest part? Harder ones. Okay, so how do you go diagonal? Let's go over diagonal downwards first. So how do you go diagonal down? Okay, so if you look at the seventh term, one over four, right? When I'm going diagonal down, what pattern do you see? Well, first off, it goes one over four, then it goes two over three, three over two, and four over one. Okay? So basically what it's doing is to go diagonal down, all it's doing is just adding one to the numerator, then subtracting one from the denominator, over and over again. That's what it's doing. So the seventh term, one over four, eighth term is two over third, ninth term is three over two, tenth term is four over one. Right? And it's adding one to the numerator, so the numerator is one, it becomes two, becomes three, becomes four, and then the denominator is four, now it becomes three, becomes two, becomes one. So to go diagonal down, all you have to do is do like a while loop, while the denominator is not equal to one. Right? Or our denominator is not equal to one. What we're gonna do is we're gonna subtract one from the denominator, over and over again, and then we're gonna add one to the numerator, over and over again. And yeah, that's basically how you go diagonal down. Now how do you go diagonal upwards? That's our last term, okay? Now to go diagonal upwards, all you have to do, it's basically the same thing. Instead, if we look at the 11th term, five over one, five over one, right? Five over one. Now to go diagonal up, what it's doing, it goes five over one, becomes four over two, three over three, two over four, one over five. So now for diagonal upwards, it's different. Instead of the denominator going down to one, this time it's the numerator going down to one. So for a diagonal going up, we're just gonna do a while loop, while our numerator is not equal to one, we're just gonna keep decreasing the numerator by one and then increasing our denominator by one. So we're gonna have five over one becomes, it goes from five, four, three, two, one, and then our denominator was one, it goes one, two, three, four, five, okay? So that's basically the gist of this problem. We're gonna have two variables, numerator and denominator, which is gonna repeat these operations, okay? We're gonna add one, to go right, we're gonna add one to the denominator, to go diagonal, we're gonna use that while loop, to go downwards for the down one, we're gonna add one to the numerator. Diagonal up, we're gonna use another while loop. And we're gonna keep repeating this until our value of our current counter, for the current counter of the number of terms is gonna equal to n, right? Because we want to find, let's say we want to find the 18th term. So what we're gonna do is we're gonna repeat this operations so and so number of times until we reach the 18th term. And then at that time, when we reach the 18th term, we break and then we print out our answer, okay? So I'm gonna explain my code now because I don't feel like editing this video because I'm really tired. And also, yeah, I'm really tired. So let's just go to my, how the hell do I go back? Oh, okay, yeah, my status. Okay, so yeah, I think it's edit. Okay, so to do this problem, let's just scroll down real quick. Okay, so main method, just read a number of test cases while our number of test cases is t minus minus. So number of test cases is t, while t minus minus, we're gonna read an n and then we're gonna call solved. So n is like the current, the nth term that we want, and then we call solved, okay? So what do I do here? Okay, so our case just represents like the number of times the current k represents the current term we're on, right? So the first term we're on, where's one, right? So we want to keep going, do a while loop until we reach the nth term, right? Cause we want to find the nth term. Like if I give you the 17th term, I want to get to the 17th term, right? So what I'm gonna do is I'm gonna keep incrementing k by one every time I go to the next term until I reach the 17th term. So that's what our k is, okay? Now I have a numerator and a denominator and I both set that to equal to one. And the reason why I set that to equal to one is cause the first value, the first term in our cases here is one over one, right? So the numerator is always one and the denominator is one. So that's the reason why we do that, okay? Okay, all right. So if the numerator is equal to one, this is gonna go, we're gonna go right. We're gonna go right by one. That's the time when we go right by one. Cause if our numerator is one, we go right by one. And to do that, we just add a denominator about plus one. So for that, here we just add denominator plus one and then we increase our term to the next term, right? So a k plus plus goes to the next term. Okay, I have this checked to check if we reached the nth term already. You don't, I did that because like, I know certain conditions like, what if you already reached the term? So yeah, you're gonna have to do that. So I do this check just in case of like, I reached other terms already, if I already reached the nth term. So if k is equal to n, I just break. So I'm already done, okay? Now, once we went one to the right, once we didn't went one to the right, now we have to go diagonal down. So how do I do that? Just to go diagonal down. What I do is while my denominator is greater than one, so while it's not really equal to one and our k is less than n, so k is the current term, right? It's just making sure that I didn't reach the nth term already, right? This did this condition. Basically, you just say while our denominator is equal to, is greater than one. So in this case, if we go back to our terms, going down we have, let's say we're at seventh term, one over four, then while our denominator is not equal to one, right? I'm gonna keep decreasing denominator by one. So it'll be four, three, two, one, until we reach four over one. So that's what this does. So that's what this while loop does. So while the denominator is greater than one, I'm gonna do numerator plus one, and then a denominator minus one, and then I increase k. Because the reason why I increase k is that I'm gonna, that makes sure that goes to the next term, right? So if I have one over four, that's the seventh term, then I go to eighth term, so that's plus one, the ninth term plus one, tenth term, okay? So that's basically the gist of it, okay? So that's how to go diagonal down. I have another case to check if I've already reached the nth term. If I already reached the nth term, k is equal to n, and I just break, okay? That's what this case is for. All right, now once I reached diagonal down, right? I have to go one down, and to go one down you just increase one to the numerator. So for that you just check if our denominator is already one, so in this case four over one, right? Our denominator is already one, so I just increase numerator by one. So for that, here if denominator is equal to one, then numerator plus one, and then I increase one, my counter of the kth term to the next term. So that's what I do, okay? And then if k is equal to n, we just break, okay? So this is checking if I already reached the next, the nth term already. So I do that every time because it's just in case if I already reached the nth term or not. So there's no reason to keep going again and then. But yeah, that's what this is for. So this if statement was for to go down, and then this if statement is just checking if again if I already reached the nth term. All right, now once we go down one, we also have to go diagonal up again. So for that, why don't I keep going visual studio? For that, all you do is while numerator is greater than one, so while numerator is not equal to one, so diagonal up. Remember what we do is we're gonna keep subtracting one from the numerator, while our numerator is not equal to one, we're just gonna keep subtracting one from our numerator and then we're gonna add one to our denominator over and over again. So that's what this is for, okay? So while numerator is greater than one, and we're not at the nth term yet, we're gonna subtract one from the numerator over and over again and we're gonna add one to the denominator and we're gonna increase our k so that we go to the next term over and over again. Now I have another statement to see if I already reached the nth term. And if I did, I'd just break, okay? After when you break out of this, you have to print out the term. So here you just do term, print out the term, print out the space, print out n, and you say is, and then you print out your numerator divided by denominator. And that's pretty much the gist of this code. Hope you guys enjoyed this video. Rate, com, subscribe. I'm really tired. I'm gonna go to bed because I need to fix my sleep schedule. But yeah, rate, com, subscribe. Check you guys later, peace.