# Find Multiples of 2 or 3 or 5 less than or equal to N

Given an integer . The task is to count all such numbers that are less than or equal to N which are divisible by any of 2 or 3 or 5.

Note: If a number less than N is divisible by both 2 or 3, or 3 or 5, or all of 2,3 and 5 then also it should be counted only once.

Examples:

Input : N = 5
Output : 4

Input : N = 10
Output : 8


## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Simple Approach: A simple approach is to traverse from 1 to N and count multiple of 2, 3, 5 which are less than equal to N. To do this, iterate up to N and just check whether a number is divisible by 2 or 3 or 5. If it is divisible, increment the counter and after reaching N, print the result.

Time Complexity: O(N).

Efficient Approach: An efficient approach is to use the concept of set theory. As we have to find numbers that are divisible by 2 or 3 or 5.  Now the task is to find n(a),n(b),n(c),n(a b), n(b c), n(a c), and n(a b c). All these terms can be calculated using Bit masking. In this problem we have taken three numbers 2,3, and 5. So, the bit mask should be of 2^3 bits i.e 8 to generate all combination of 2,3, and 5.

Now according to the formula of set union, all terms containing odd numbers of (2,3,5) will add into the result and terms containing even number of (2,3,5) will get subtracted.

Below is the implementation of the above approach:

## C++

 // CPP program to count number of multiples   // of 2 or 3 or 5 less than or equal to N     #include     using namespace std;     // Function to count number of multiples   // of 2 or 3 or 5 less than or equal to N  int countMultiples(int n)  {      // As we have to check divisibility      // by three numbers, So we can implement      // bit masking      int multiple[] = { 2, 3, 5 };             int count = 0, mask = pow(2, 3);             for (int i = 1; i < mask; i++) {             // we check whether jth bit          // is set or not, if jth bit          // is set, simply multiply          // to prod          int prod = 1;                     for (int j = 0; j < 3; j++) {                 // check for set bit              if (i & 1 << j)                  prod = prod * multiple[j];          }                     // check multiple of product           if (__builtin_popcount(i) % 2 == 1)              count = count + n / prod;          else             count = count - n / prod;      }             return count;  }     // Driver code  int main()  {      int n = 10;             cout << countMultiples(n) << endl;             return 0;  }

## Java

 // Java program to count number of multiples   // of 2 or 3 or 5 less than or equal to N     class GFG{  static int count_setbits(int N)  {       int cnt=0;      while(N>0)      {          cnt+=(N&1);          N=N>>1;      }      return cnt;  }     // Function to count number of multiples   // of 2 or 3 or 5 less than or equal to N  static int countMultiples(int n)  {      // As we have to check divisibility      // by three numbers, So we can implement      // bit masking      int multiple[] = { 2, 3, 5 };             int count = 0, mask = (int)Math.pow(2, 3);             for (int i = 1; i < mask; i++) {             // we check whether jth bit          // is set or not, if jth bit          // is set, simply multiply          // to prod          int prod = 1;                     for (int j = 0; j < 3; j++) {                 // check for set bit              if ((i & 1 << j)>0)                  prod = prod * multiple[j];          }                     // check multiple of product           if (count_setbits(i) % 2 == 1)              count = count + n / prod;          else             count = count - n / prod;      }             return count;  }     // Driver code  public static void main(String[] args)  {      int n = 10;             System.out.println(countMultiples(n));  }  }  // this code is contributed by mits

## Python 3

 # Python3 program to count number of multiples   # of 2 or 3 or 5 less than or equal to N         # Function to count number of multiples   # of 2 or 3 or 5 less than or equal to N   def countMultiples( n):          # As we have to check divisibility       # by three numbers, So we can implement       # bit masking       multiple = [ 2, 3, 5 ]              count = 0     mask = int(pow(2, 3))       for i in range(1,mask):           # we check whether jth bit           # is set or not, if jth bit           # is set, simply multiply           # to prod           prod = 1         for j in range(3):                  # check for set bit               if (i & (1 << j)):                   prod = prod * multiple[j]                      # check multiple of product           if (bin(i).count('1') % 2 == 1):               count = count + n // prod           else:              count = count - n // prod              return count         # Driver code  if __name__=='__main__':      n = 10     print(countMultiples(n))          # This code is contributed by ash264

## C#

 // C#  program to count number of multiples   // of 2 or 3 or 5 less than or equal to N         using System;     public class GFG{      static int count_setbits(int N)   {       int cnt=0;       while(N>0)       {           cnt+=(N&1);           N=N>>1;       }       return cnt;   }      // Function to count number of multiples   // of 2 or 3 or 5 less than or equal to N   static int countMultiples(int n)   {       // As we have to check divisibility       // by three numbers, So we can implement       // bit masking       int []multiple = { 2, 3, 5 };              int count = 0, mask = (int)Math.Pow(2, 3);              for (int i = 1; i < mask; i++) {              // we check whether jth bit           // is set or not, if jth bit           // is set, simply multiply           // to prod           int prod = 1;                      for (int j = 0; j < 3; j++) {                  // check for set bit               if ((i & 1 << j)>0)                   prod = prod * multiple[j];           }                      // check multiple of product           if (count_setbits(i) % 2 == 1)               count = count + n / prod;           else             count = count - n / prod;       }              return count;   }      // Driver code       static public void Main (){                 int n = 10;              Console.WriteLine(countMultiples(n));   }   }   //This code is contributed by ajit.

## PHP

 > 1;      }         return $count;  }    // Function to count number of  // multiples of 2 or 3 or 5 less  // than or equal to N  function countMultiples($n)  {      // As we have to check divisibility      // by three numbers, So we can       // implement bit masking      $multiple = array(2, 3, 5);     $count = 0;      $mask = pow(2, 3);     for ($i = 1; $i < $mask; $i++)   {     // we check whether jth bit   // is set or not, if jth bit   // is set, simply multiply   // to prod   $prod = 1;                     for ($j = 0; $j < 3; $j++)   {     // check for set bit   if ($i & 1 << $j)   $prod = $prod * $multiple[$j];   }     // check multiple of product   if (popcount($i) % 2 == 1)              $count = $count + (int)($n / $prod);                         else             $count = $count - (int)($n / $prod);                     }             return $count;  }    // Driver code  $n = 10;         echo countMultiples(\$n);         // This code is contributed by ash264  ?>

Output:

8


My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : ash264, Mithun Kumar, jit_t

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.