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

Given an integer N. 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

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.

Picture
  \begin{document} \begin{itemize} \item Let $n(a) \colon $ count of numbers divisible by 2. \item Let $n(b) \colon $ count of numbers divisible by 3. \item Let $n(c) \colon $ count of numbers divisible by 5. \item $n(a \bigcap b) \colon $ count of numbers divisible by 2 and 3. \item $n(a \bigcap c) \colon $ count of numbers divisible by 2 and 5. \item $n(b \bigcap c) \colon $ count of numbers divisible by 3 and 5. \item $n(a \bigcap b \bigcap c) \colon $ count of numbers divisible by 2 and 3 and 5. \end{itemize} According to set theory,  $n\left( a \bigcup b \bigcup c \right)=n(a)+n(b)+n(c)-n(a \bigcap b)-n(b \bigcap c)-n(a \bigcap c)+n(a \bigcap b \bigcap c)$ \end{document}

Now the task is to find n(a),n(b),n(c),n(a\bigcapb), n(b\bigcapc), n(a\bigcapc), and n(a\bigcapb\bigcapc). 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to count number of multiples 
// of 2 or 3 or 5 less than or equal to N
  
#include <bits/stdc++.h>
  
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to count number 
// of multiples of 2 or 3 or 5 
// less than or equal to N
  
// Bit count function
function popcount($value)
{
    $count = 0;
    while($value)
    {
        $count += ($value & 1);
        $value = $value >> 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
?>

chevron_right


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