Given a number n, count all multiples of 3 and/or 5 in set {1, 2, 3, ... n} - GeeksQuiz
Given a number n, count all multiples of 3 and/or 5 in set of numbers from 1 to n.
The value of n/3 gives us number of multiples of 3, the value of n/5 gives us number of multiples of 5. But the important point is there are may be some common multiples which are multiples of both 3 and 5. We can get such multiples by using n/15.
Read full article from Given a number n, count all multiples of 3 and/or 5 in set {1, 2, 3, ... n} - GeeksQuiz
Given a number n, count all multiples of 3 and/or 5 in set of numbers from 1 to n.
The value of n/3 gives us number of multiples of 3, the value of n/5 gives us number of multiples of 5. But the important point is there are may be some common multiples which are multiples of both 3 and 5. We can get such multiples by using n/15.
unsigned countOfMultiples(unsigned n)
{
// Add multiples of 3 and 5. Since common multples are
// counted twice in n/3 + n/15, subtract common multiples
return
(n/3 + n/5 - n/15);
}