📜  查找小于或等于N的2或3或5的倍数

📅  最后修改于: 2021-04-24 17:22:47             🧑  作者: Mango

给定一个整数N   。任务是计算所有小于或等于N的,可被2或3或5整除的数字。
注意:如果小于N的数字可被2或3、3或5或2,3和5整除,则也应仅计数一次。
例子

Input : N = 5
Output : 4

Input : N = 10
Output : 8

简单方法:一种简单方法是从1遍历到N,并计数小于等于N的2、3、5的倍数。为此,迭代到N,然后检查数字是否可被2或3整除或5.如果可以整除,请增加计数器,并在达到N后打印结果。
时间复杂度:O(N)。
有效的方法:有效的方法是使用集合论的概念。因为我们必须找到可以被2或3或5整除的数字。

图片

\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}
现在的任务是找到n(a),n(b),n(c),n(a \bigcap   b),n(b \bigcap   c),n(a \bigcap   c)和n(a \bigcap   b \bigcap   C)。所有这些项都可以使用位掩码来计算。在这个问题中,我们采用了3个数字2,3和5。因此,位掩码应为2 ^ 3位,即8,以生成2,3和5的所有组合。
现在根据集合并集的公式,将所有包含奇数(2,3,5)的项加到结果中,然后将包含偶数(2,3,5)的项相减。
下面是上述方法的实现:

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


Javascript


输出:
8