Multiples of 3 or 7 - GeeksforGeeks

16 Aug, 2021
Given a positive integer n, find count of all multiples of 3 or 7 less than or equal to n.Examples : 
Input : n = 10
Output : Count = 4
The multiples are 3, 6, 7 and 9

Input : n = 25
Output : Count = 10
The multiples are 3, 6, 7, 9, 12, 14, 15, 18, 21 and 24
A Simple Solution is to iterate over all numbers from 1 to n and increment count whenever a number is a multiple of 3 or 7 or both. 
C++
Java
Python3
C#
PHP
Javascript
// A Simple 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 multiples of 3 or 7 or bothint countMultiples(int n){    int res = 0;    for (int i=1; i<=n; i++)       if (i%3==0 || i%7 == 0)           res++;     return res;} // Driver codeint main(){   cout << "Count = " << countMultiples(25);}
// A Simple Java program to// find count of all numbers// that multiplesimport java.io.*; class GFG{     // Returns count of all numbers// smaller than or equal to n// and multiples of 3 or 7 or bothstatic int countMultiples(int n){    int res = 0;    for (int i = 1; i <= n; i++)    if (i % 3 == 0 || i % 7 == 0)        res++;     return res;} // Driver Codepublic static void main (String[] args){    System.out.print("Count = ");    System.out.println(countMultiples(25));}} // This code is contributed by m_kit
# A Simple Python3 program to# find count of all numbers# that multiples # Returns count of all numbers# smaller than or equal to n# and multiples of 3 or 7 or bothdef countMultiples(n):    res = 0;    for i in range(1, n + 1):        if (i % 3 == 0 or i % 7 == 0):            res += 1;      return res; # Driver codeprint("Count =", countMultiples(25)); # This code is contributed by mits
// A Simple C# program to// find count of all numbers// that are multiples of 3 or 7using System; class GFG{     // Returns count of all // numbers smaller than// or equal to n  and// are multiples of 3 or// 7 or bothstatic int countMultiples(int n){    int res = 0;    for (int i = 1; i <= n; i++)    if (i % 3 == 0 || i % 7 == 0)        res++;     return res;} // Driver Codestatic public void Main (){    Console.Write("Count = ");    Console.WriteLine(countMultiples(25));}} // This code is contributed by ajit
<?php// A Simple PHP program to find count// of all numbers that multiples // Returns count of all numbers// smaller than or equal to n// and multiples of 3 or 7 or bothfunction countMultiples($n){    $res = 0;    for ($i = 1; $i <= $n; $i++)    if ($i % 3 == 0 || $i % 7 == 0)        $res++;     return $res;} // Driver codeecho "Count = " ,countMultiples(25); // This code is contributed by aj_36?>
<script> // A Simple JavaScript program to find count// of all numbers that multiples // Returns count of all numbers// smaller than or equal to n// and multiples of 3 or 7 or bothfunction countMultiples(n){    let res = 0;    for (let i = 1; i <= n; i++)    if (i % 3 == 0 || i % 7 == 0)        res++;     return res;} // Driver codedocument.write( "Count = " +countMultiples(25)); // This code is contributed by bobby </script>
Output : 
Count = 10
Time Complexity : O(n)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 
C++
Java
Python 3
C#
PHP
Javascript
// 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 multiples of 3 or 7 or bothint countMultiples(int n){   return n/3 + n/7 -n/21;} // Driver codeint main(){   cout << "Count = " << countMultiples(25);}
// A better Java program to// find count of all numbers// that multiplesimport java.io.*; class GFG{     // Returns count of all numbers// smaller than or equal to n// and multiples of 3 or 7 or bothstatic int countMultiples(int n){    return n / 3 + n / 7 - n / 21;} // Driver codepublic static void main (String args [] ){    System.out.println("Count = " +                        countMultiples(25));}} // This code is contributed by aj_36
# Python 3 program to find count of# all numbers that multiples # Returns count of all numbers# smaller than or equal to n and# multiples of 3 or 7 or bothdef countMultiples(n):    return n / 3 + n / 7 - n / 21; # Driver coden = ((int)(countMultiples(25)));print("Count =", n); # This code is contributed# by Shivi_Aggarwal
// A better Java program to// find count of all numbers// that multiplesusing System; class GFG{     // Returns count of all numbers// smaller than or equal to n// and multiples of 3 or 7 or bothstatic int countMultiples(int n){    return n / 3 + n / 7 - n / 21;} // Driver Codestatic public void Main (){    Console.WriteLine("Count = " +                       countMultiples(25));}} // This code is contributed by m_kit
<?php// A better PHP program to find count// of all numbers that multiples // Returns count of all numbers// smaller than or equal to n// and multiples of 3 or 7 or bothfunction countMultiples($n){return floor($n / 3 + $n / 7 - $n / 21);} // Driver codeecho "Count = " , countMultiples(25); // This code is contributed by ajit?>
<script> // JavaScript program to find count// of all numbers that multiples // Returns count of all numbers// smaller than or equal to n// and multiples of 3 or 7 or bothfunction countMultiples(n){return Math.floor(n / 3 + n / 7 - n / 21);} // Driver codedocument.write( "Count = " +countMultiples(25)); // This code is contributed by bobby </script>
Output : 
Count = 10
Time Complexity : O(1)Exercise: Now try the problem of finding sum of all numbers less than or equal to n and multiples of 3 or 7 or both in O(1) time.This article is contributed by Saurabh Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
jit_t
Mithun Kumar
Shivi_Aggarwal
gottumukkalabobby
surinderdawra388
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Program to find sum of elements in a given array
The Knight's tour problem | Backtracking-1
Euclidean algorithms (Basic and Extended)
Program to convert a given number to words
Algorithm to solve Rubik's Cube
Modular multiplicative inverse
Print all possible combinations of r elements in a given array of size n
Find minimum number of coins that make a given value
Program to find LCM of two numbers
Program to count digits in an integer (4 Different Methods)