Write an algorithm to display the first 10 multiples of 7.
Answers
Answered by
0
An efficient solution can solve the above problem in O(1) time. The idea is to count multiples of 3 and add multiples of 7, then subtract multiples of 21 because these are counted twice.
count = n/3 + n/7 - n/21
// A better C++ program to find count of all
// numbers that multiples
#include<iostream>
using namespace std;
// Returns count of all numbers smaller than
// or equal to n and multples of 3 or 7 or both
int countMultiples(int n)
{
return n/3 + n/7 -n/21;
}
// Driver code
int main()
{
cout << "Count = " << countMultiples(25);
}
Similar questions