 Okay, so let's do something that's a fairly standard strategy, which is we're going to take an algorithm, we'll see what will break it, and then we'll modify it so that we have fixed the problem. So let's take a look at this. I can try and factor n by finding when n plus k squared is a perfect square, and let's see what's going to make that easy and what's going to make it difficult. So for example, let's say n is 73 times 71. Well, the thing we notice here is that that means that n is going to be 72 minus 1 times 72 plus 1, and rearranging that, working our difference of perfect squares backwards. What that means is that this product, 73 times 71, 51, 83, is going to give us a factorization when we evaluate 51, 83 plus 1 squared, which will equal a perfect square. And there's our difference of perfect square, 72 squared minus 1 squared, which gives us the factorization, and that is an easy problem to solve. On the other hand, let's take a look at something like 17 times 47. Same sort of logic tells us that n is 32 minus 15 times 32 plus 15. And when I get this from the difference of squares method, I had to have gotten it from 799 plus 15 squared equals 32 squared. And so the thing to notice here is that if I apply this difference of squares method, I get a factorization of 51, 83 after trying just one perfect square add. On the other hand, for 799, I have to add 1 squared, 2 squared, 3 squared, and so on all the way up to 15 squared. I have to run this through 15 times. And so 799 is significantly harder to factor this way than 51, 83. So let's try and fix this problem. So let's consider this. So the hard one to factor is 17 times 47, because we had to go all the way up to adding 15 squared to get a factorization. And let's think about that for a minute. Now, 17 times 47, the reason that this was difficult to factor using the difference of perfect squares is that the two factors were fairly far apart. But the thing to notice is that if I multiply one factor by a number, how about three, what happens is that smaller factor multiplied by three gives me a factor that's actually much closer to the other one. Now, over on the left-hand side, I have 3 times 799. And over on the right-hand side, I have something that is the product of two numbers that are very close together. And so rearranging things a little bit, if I want to evaluate 799 plus k squared, I have to go up to k equals 15 before I can find a perfect square. But instead, if I evaluate 3 times 799, I only have to go as far as k equals 2 before I can find my perfect square. So how does that look like? Well, 3 times 799 plus 1 squared is 2398, plus 2 squared is 2401, which is a perfect square. And that gives me my factorization, 51 times 47. And I'll drop that factor 3 now and have my factorization of 799. So this suggests the following. Suppose I have p and q, which are prime numbers. And p is approximately equal to k times q for some whole numbers. p is 2 times q, 3 times q, 5 times q, whatever. It's close to a multiple of q. Then k times n can be factored very easily using the difference of perfect squares. Of course, the problem is if I want to factor n, I am supposing I don't actually know what its factors p and q are. And if I don't know what p and q are, how am I going to figure out what k is? So if I don't know the factors of n, how do I know what k we should use? And turns out there's a good answer to this. If we don't know which one to use, we'll use all of them. In particular, the difference of perfect squares factorization works because I can find the product k times n as a difference of a squared and b squared. Well, this should look familiar because this is just the definition of when two numbers are congruent mod n. So if k times n is the difference of perfect squares, then a squared and b squared are going to be congruent mod n, and I don't actually need to know what k is. So that suggests the following approach. If I want to factor n, I'm going to find two numbers a and b where they're not equal, where their squares are congruent mod n. And again, what that means is that k times n is the difference of perfect squares, so k times n factors, which means that the greatest common divisor of n and a minus b will also divide n. And likewise, I could also use the greatest common divisor of n and a plus b. It doesn't matter which one I use. And remember, finding the greatest common divisor is actually pretty easy in comparison with factoring. Well, let's see how that works. So let's try to factor 3431. And so my goal is I want to find two numbers a and b not equal to each other, where their squares are congruent mod n. Now initially, what we might think about here is I'm looking for a number that is also a square mod n. And one thing that's worth noting here, square root of 3431 is around 58.57. So it won't do any good to start with any number that's smaller than 58.57. Because when I square it, well, I'll obviously get a square, but it'll just be the square of the number I started with. On the other hand, if I start with a equals 59, when I square that, it's going to be more than 3431. And it leaves open the possibility it might be the square of a different number. So we'll try that 59 squared is congruent to 50 mod 3431, not obviously the square of a number. So we'll go on to the next. And oh, 169, that is obviously the square of a number, 13. And that tells me that 3431 divides the difference, 60 squared minus 13 squared, which is 47 times 73. So that tells me, then, that the greatest common divisor, 3431. And either one of these two is going to also be a factor of 3431. And we find that the greatest common divisor is 47. And through division, we can get the other factor, 73. And so that gives us our factorization, 3431 is 47 times 73. Well, we may have longer problems, so how about the number 1649? So again, it doesn't do us any good to start before a equals 41, so we might as well start with that. 41 squared, 32 mod 6429, and again, not obviously a perfect square. So we'll go on to the next, not obviously a perfect square, go on to the next, and so on. And eventually, we do come to something that is a perfect square, that is obviously a perfect square. And so 57 squared is 1649, is 40 squared, and finding our greatest common divisor of n, 1649, and the difference is going to give us one factor, which turns out to be 17. And the other factor we can find by division and get our factorization.