 So in this video, we're going to look at an example of a problem that we will solve using a looping strategy, in particular a Y loop. And then we will ask the question, could we actually write that in a for loop as well? Because in one of the previous videos, I told you that there are problems, so most problems can be solved with both a for and a Y loop, but there are problems that can only be solved with a Y loop that is more generic than the more specific for loop that can only work under certain circumstances. So let's look at the example, create a new notebook file and let's call it collards, named after a mathematician who has adjusted a game and then he asked a theoretically question about it. So let's go ahead and call the file collards conjecture. So conjecture in math world is always something that people think is correct, but they cannot prove it yet. So with high confidence, we know this is true, but we cannot prove it at using a mathematical proof concept. So let's do the following. Let's look at a game and the game goes like this. Given a positive integer, do the following. If the integer, if n or let's call it n, if given a positive integer, let's call it n. If n is even, then the next n is just half the previous n. So if n is 10, then the next n would just be 5. Otherwise, so if n is odd, then the next n is 3 times the old n plus 1. And the game ends, so game ends if n reaches 1. And now the question is, the big question is, does n always reach 1? So that is the game we're going to implement in code. So let's go ahead and do that. And of course, we are going ahead and we will do so in a while loop first. That is the whole point of this video because as we will see that this will be very hard to do with a for loop. So going ahead with a while loop, let's do the following. The pose n is, for example, 10. Let's write a while loop that checks a condition of when the game is done. And the condition is, once we reach the number 1. So in other words, while n is not equal to 1, this is the condition that is true as long as the game is still going on. So that is why we use a while loop here to determine if the game is still on or if we are already at the end. So let's go in the while loop. We now have to decide if the current n is either an even or an odd number. We know how to do that. So let's go ahead and say if n modulo divided by 2 has no rest. So if n percentage sign 2, so modulo operator has no rest, so division by 2 has no rest, then the new n is going to be the old n divided by 2. So now remember how when we looked into division using a single slash operator is the normal division operator. And this will always give us back a floating point number. However, n is supposed to be an integer at all times. So we start with an integer and we always get an integer just like this as well. So in other words, what we are going to do is we are going to use the double slash here. So n divided by 2 using the double slash will preserve the integer data type. And now if we see this here, if we see the new n is set to the old n divided by 2, what we could also do is we could simply use the shortcut version like this, double slash equals 2. So this is basically the shortcut version. And again, the double slash is only there to preserve the data type. Then we go ahead and say let's model the else clause. So in other words, if the number n is odd, then what we are going to do is the new n is going to be the old n times 3. So let's write 3 times n plus 1. And the second expression here cannot be simplified just like the first one here because the first one was a simple update, simply divide n by 2 and that's the new n. And here we have a second operand plus so we cannot make this any shorter. Okay, so this is basically it. And now let's also go ahead and in the first line in the y loop, let's also go ahead and simply write print n and let's print the current n on one line. Let's go ahead and the program outputs 10, 5, 16, 8, 4 and 2 and then 2 divided by 2 would be 1. So we would also have to go ahead and say print 1 here as well. And then we see the 1. Okay, the 1 is not shown because once n becomes 1, the y loop is basically over. So the condition becomes false and we are not going into the y loop again. So this print function called here will not happen once n has become 1. This is why we need to say print either n or print 1 down here. So print n also works, of course. Okay, and now we can play with it. So let's start. Let's say, for example, with 20 instead of 10 and let's see, 20 is even. So we divide it by 2, it becomes 10, 10 is even. So we divide it by 2, it becomes 5, 5 is odd. So we multiply it by 3 and add 1 to it, this becomes 16 and then so on. So now the question is, do we always get to the number 1? And it turns out that no one ever has proven it so far. And that is why we call this thing the collets conjecture and not the collets proof. If we could prove it, then probably this example would be called the collets proof in math terms, but it's not provenable. So we call it the collets conjecture. And now let's also get an intuition why this is the case. So let's start with 10 again and let's see that we have 1, 2, 3, 4, 5, 6, 7 numbers. If I go to, let's say, 100, I get more numbers, okay? So this already looks like if I start with a bigger N, then the series until I reach the number 1 must become longer. So let's go ahead and use 1,000. And 1,000 will still get longer, so it seems that our intuitive rules seems to hold. Let's go to 10,000 and all of a sudden the series becomes smaller again, okay? So depending on what the start value is, we either converge very fast or not so fast towards 1. But at the end of the day, we always reach 1 at least. Whenever I tried this function or this code here, I always reached 1 no matter what the start value was. So it seems like this statement is always true here, that do we always reach 1? The answer should be yes, but again, no one has proven it so far. And by the fact that this number, the series of numbers, until we reach 1 sometimes becomes longer as N increases and sometimes becomes smaller or shorter as N increases. This is already the indication or the intuition why this is mathematically a very hard problem to solve because there is no 1 to 1 correspondence between larger Ns and the length of the sequence we get. Okay? And if you want to write a function to do that, what we could do is we could highlight all the code. We indent everything to the left-hand side by one level. The first N, where it's set to 1 at 10,000 here, we don't need actually because we're going to write define the collet function. And the collet function will take N as the parameter. And now we have to go ahead and write some nice doc string. So let's do that. And let's go ahead and simply copy paste this here in here. And let's write here print a collet sequence in descending order, maybe. Let's have an empty line here. So now here we have a nice description of the game, given a positive integer N and so on. And then of course we should not forget to authorize arcs here. Then it's an integer and it must be a positive number. So let's write a positive number to start the collet sequence. And let me see if we have to change something here. No, we don't. So the idea of this function is, as it says in the subject line, print a collet sequence. Okay? So this function has a side effect. So it does not have a return value. That is why we don't have a return here. The function does not calculate something. It simply prints out the sequence because that is the task here. And if I execute this, I now have a function. And now I can go ahead and call the function a couple of times. For example, with the number 10. And let's do this. Make it 100. Then make it 1,000. We see it becomes longer. And then, as we just saw, if we make this 10,000, then the series all of a sudden becomes smaller again. Okay? So this is now in one picture how we could write that, how we could write a nice function doing that. It's nicely documented. And that is it. So why can we not use the for loop here to solve this problem? That is a theoretical question. And remember that in previous videos, when I introduced a couple of problems that were solved using a for loop, then I always said that we could always, before the code actually ran, tell how often we had to loop. Okay? So we could predict the number of iterations in the loop. The number of times that a certain piece of code was repeated was deterministic. We could calculate it. Okay? For factorial of three, for example, we know we have to do three iterations. For the Fibonacci number, it was not the same as the index of the Fibonacci number we wanted to calculate, but it was some constant smaller than this. Okay? So we could predict the number of iterations here, however, as we see, we cannot predict how long, how many numbers it takes, how many iterations it takes. That is the indication. That is the intuitive reason why we cannot use the for loop here. Okay? So in other words, the why loop can always be used whenever a for loop also works, but the for loop is usually a simplification. But then there are examples just like this example here, the collets conjecture, where we have to specify some end condition. And we are not sure when this is reached or if at all. Okay? That is why this example is also theoretical nature a bit to get a point across that not all problems can be solved using for loop. And in particular, in the next video, we are also going to look at how we can model interactive games, so games where a program takes input from a user, a guessing game, for example, like guessing a coin cost or something like that. And then the user, it depends on the user when the game is done. So also for interactive games or for whenever you write a program where a computer has to wait for manual input by some user, then also oftentimes you can implement that using a why loop and not in a for loop usually. Okay? So these are applications of the why loop. This is it for this video. And in the next video, we are going to see how we how else we can use the why loop. See you then.