One obvious approach for finding multiples of three and five is to loop through the numbers between 3 and the maximum value, check each number to see if it is a multiple of 3 or 5, and add the multiples to get a running total. The following method takes this approach into account:
private long Method1(long max){ long total = 0; checked { for (long i = 3; i <= max; i++) if ((i % 3 == 0) || (i % 5 == 0)) total += i; } return total;}
This is straightforward but not very efficient. If the max parameter is large, this method can take a while.
A better approach is to use a variable to keep track of the next multiple of five and then loop through the multiples of 3. If a multiple of 3 is greater than the next multiple of 5, ...