A number, , is said to be a multiple of another number, , if the remainder of is . Similarly, a number, , will be a multiple of 3 and 5 if the remainder of and is equal to .
In order to find all the numbers from to that are multiples of 3 and 5, each number needs to be considered separately, and the remainders of both and should be compared to .
Since the range to is traversed only once, the time complexity of this algorithm is .
The following code implements this algorithm in C++, Java, and Python:
#include <iostream>using namespace std;void findMultiples(int n){for(int i = 0; i <= n; i++)if(i % 3 == 0 && i % 5 == 0)cout << i << endl;}int main() {findMultiples(120);return 0;}
Free Resources