Open In App

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


 


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(ab), n(bc), n(ac), and n(abc). 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: 
 

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

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

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

                    
<script>
 
// javascript program to count number of multiples
// of 2 or 3 or 5 less than or equal to N
function count_setbits(N)
{
    var 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
function countMultiples(n)
{
    // As we have to check divisibility
    // by three numbers, So we can implement
    // bit masking
    var multiple = [ 2, 3, 5 ];
     
    var count = 0, mask = parseInt(Math.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
        var prod = 1;
         
        for (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 + parseInt(n / prod);
        else
            count = count - parseInt(n / prod);
    }
     
    return count;
}
 
// Driver code
var n = 10;
 
document.write(countMultiples(n));
 
// This code is contributed by 29AjayKumar
 
</script>

                    

Output: 
8

 

Time Complexity: O(1)

Auxiliary Space: O(1)


Article Tags :