📅  最后修改于: 2022-03-11 15:04:42.830000             🧑  作者: Mango
//Computational tasks can be performed in several different ways with
//varying efficiency.
//A more efficient version with equivalent functionality is known as a strength reduction.
//For example, consider the following C code snippet whose intention is to
//obtain the sum of all integers from 1 to N:
int i, sum = 0;
for (i = 1; i <= N; ++i) {
sum += i;
}
printf("sum: %d\n", sum);
//This code can (assuming no arithmetic overflow) be rewritten using a mathematical formula like:
int sum = N * (1 + N) / 2;
printf("sum: %d\n", sum);