JOJ 2042 is a program comprehension question, which provides a wonderful program for calculating x ^ 3. The principle is as follows:
It can be seen from the above that the power function increases by 6 for the first time, and its initial value is 1. Therefore, you can write the program to calculate the Cube as follows:
Int f (x ){
Int c = 0, d = 1, e = 6;
While (x --){
C + = d;
D + = e;
E + = 6;
}
Reutnrn c;
}
Similarly, based on this idea, you can write similar programs for square calculation:
X2-(x-1) 2 = 2 * X-1, the addition of the square function, the initial value is 1
2 * X-1-[2 * (x-1)-1] = 2, the increase of the square function, is constant 2
So the following program can calculate the square of the input positive integer:
# Include <stdio. h> </P> <p> int F (int x) {<br/> int C = 0, D = 1; <br/> while (X --) {<br/> C + = D; <br/> D + = 2; <br/>}< br/> return C; <br/>}</P> <p> int main () {<br/> int N; <br/> while (scanf ("% d ", & N), n) {<br/> printf ("% d/N", F (n); <br/>}< br/> return 0; <br/>}</P> <p>
There is a formula for this factorization: An-BN = (a-B) (An-1 + an-2b +... + Abn-2 + bn-1), theoretically should be able to calculate.