Problem Statement : Problem 1
Algorithm:
This problem is pretty simple and requires basic set theory.
Algorithm:
This problem is pretty simple and requires basic set theory.
Explanation about the principle can be read here.
- Calculate sum of all multiples of 3 less than 1000
- Calculate sum of all multiples of 5 less than 1000
- Calculate sum of all multiples of 15 less than 1000
A linear time running algorithm to calculate this is written below:
public class problem1 {
public static void main(String args[]) {
int sum = 0;
for (int i = 1; i < 1000; i++) {
if (i % 3 == 0) {
sum += i;
continue;
}
if (i % 5 == 0)
sum += i;
}
System.out.println(sum);
}
}
No comments:
Post a Comment