program optimization

Solutions on MaxInterview for program optimization by the best coders in the world

showing results for - "program optimization"
Sara
09 Jun 2018
1//Computational tasks can be performed in several different ways with 
2//varying efficiency. 
3//A more efficient version with equivalent functionality is known as a strength reduction. 
4//For example, consider the following C code snippet whose intention is to 
5//obtain the sum of all integers from 1 to N:
6int i, sum = 0;
7for (i = 1; i <= N; ++i) {
8  sum += i;
9}
10printf("sum: %d\n", sum);
11//This code can (assuming no arithmetic overflow) be rewritten using a mathematical formula like:
12int sum = N * (1 + N) / 2;
13printf("sum: %d\n", sum);