For UVa problem 10783 (Odd Sum), one just has to sum up all the odd numbers between two numbers, a and b. Things can get slow if for each number between a and b, one checked if that number were odd or even, like in the following code:

for (i = a, sum = 0; i <= b; i += 1) {

__j = i mod 2;

__if (j == 1) {

____sum += i;

__}

}

To hasten the computation, we can start at an odd number in the for-loop and add 2 to the iterator index for each iteration.

First we check if a is odd or even. If it is even, we increment it to make it odd. Then we go to the for-loop:

for (j = a, sum = 0; j <= b; j += 2) {

__sum += j;

}

## No comments:

## Post a Comment