We knew this was trying to do squaring, so intellectually we know we can square -4, it ought to be 16, but what happens here?
我们知道程序是用来求平方数的,那么按理说我们可以来求-4的平方,也就是16,但是程序结果是怎么样的呢?
And then in the even case I've got to do a square and the divide.
如果是偶数情况的话,我还要再做一次除法和求平方。
We know it's going around in a circle because if I find the length of this vector, which is the x-square part, plus the y-square part, I just get r square at all times, because sine square plus cosine square is one.
我们之所以知道它做圆周运动,是因为我求出了这个矢量的模长,也就是 x 的平方加上 y 的平方,我就得到了它在任意时刻的模长平方,因为正弦平方加余弦平方始终等于1
Right, It's just squaring an integer, is what it's doing.
对了,这个程序就是,用来求一个整数的平方的。
If it's not greater than x, take 3. Square it.
那么就跳到2,求它的平方。
I'm given an integer that's a perfect square, and I want to write a little piece of code that's going to find the square root of it. All right so I'm cheating a little, I know it's a perfect square, somebody's given it to me, we'll come back in a second to generalizing it, so what would the steps be that I'd use to walk through it?
完美平方数的整数,我想写一段代码来求这个数的平方根,好,我这儿有点儿作弊了,我知道这是一个完美的平方数了,他们给我的,我们后面会讲怎么产生这个数的,那么我想解决这个问题,需要什么步骤呢?
Pardon? I could square x but maybe I should just do something pretty simple here. Suppose-- whoops.
对不起我没听清,我可以求x的平方,但是好像这里我做点简单的,改动就可以解决问题了。
I take the difference in the x-values, squared, the difference in the y-values, squared, add them up, take the square root of that.
好,是毕达哥拉斯定力对不对?,求x坐标的差,然后平方,求y坐标的差,然后平方,把它们加起来,开平方。
If it's not greater than x, take 2. Square it.
求它的平方,如果1的平方小于等于x的话。
And if I were to use that again, I'd just put it on your handout, I could go back and rewrite that thing that I had previously for finding the square roots of the perfect squares, just using the FOR loop. OK. What I want to do, though, is go on to-- or, sorry, go back to - my divisor example.
它可以是任意的集合,如果我又要去用这个方法的话,我会把它放在你们的课堂手册上的,我可以回过头去用FOR循环,重新写我们那个求平方数的程序,我想要做的是,是继续-哦抱歉,回到-我的除数那个例子。
And then from that start point, get the x-value. Same thing, from that instance, get the endpoint, from that end point get the x-value, square.
取得结束点,然后取得结束点的y坐标,然后求差开平方,然后同样求的y坐标。
Got a simple square procedure,.
其实是一个很简单的求一个数的平方的过程。
And in the even case, I'd square, divide by half, call this again: in the odd case, I go b minus 1 and then multiply by a.
就直接求a的平方的二分之b次方,如果是奇数的话,就去求a的b-1次方乘以a,以此递归下去。
应用推荐