📌  相关文章
📜  计算主要因子分别为2和3的范围内的数字

📅  最后修改于: 2021-05-31 18:33:40             🧑  作者: Mango

给定两个正整数LR ,任务是计算素数仅为23的范围[L,R]中的元素。
例子:

方法:LR以及每个num元素开始循环:

  • num2整除时,将其除以2
  • 虽然num可被3整除,但将其除以3
  • 如果num = 1,则递增计数,因为num仅将23作为其主要因子。

最后打印计数
下面是上述方法的实现:

C++
// C++ program to count the numbers within a range
// whose prime factors are only 2 and 3
#include 
using namespace std;
 
// Function to count the number within a range
// whose prime factors are only 2 and 3
int findTwoThreePrime(int l, int r)
{
    // Start with 2 so that 1 doesn't get counted
    if (l == 1)
        l++;
 
    int count = 0;
 
    for (int i = l; i <= r; i++) {
        int num = i;
 
        // While num is divisible by 2, divide it by 2
        while (num % 2 == 0)
            num /= 2;
 
        // While num is divisible by 3, divide it by 3
        while (num % 3 == 0)
            num /= 3;
 
        // If num got reduced to 1 then it has
        // only 2 and 3 as prime factors
        if (num == 1)
            count++;
    }
 
    return count;
}
 
// Driver code
int main()
{
    int l = 1, r = 10;
    cout << findTwoThreePrime(l, r);
    return 0;
}


Java
//Java program to count the numbers within a range
// whose prime factors are only 2 and 3
 
import java.io.*;
 
class GFG {
     
// Function to count the number within a range
// whose prime factors are only 2 and 3
static int findTwoThreePrime(int l, int r)
{
    // Start with 2 so that 1 doesn't get counted
    if (l == 1)
        l++;
 
    int count = 0;
 
    for (int i = l; i <= r; i++) {
        int num = i;
 
        // While num is divisible by 2, divide it by 2
        while (num % 2 == 0)
            num /= 2;
 
        // While num is divisible by 3, divide it by 3
        while (num % 3 == 0)
            num /= 3;
 
        // If num got reduced to 1 then it has
        // only 2 and 3 as prime factors
        if (num == 1)
            count++;
    }
 
    return count;
}
 
// Driver code
    public static void main (String[] args) {
 
        int l = 1, r = 10;
        System.out.println (findTwoThreePrime(l, r));
    }
//This code is contributed by ajit   
}


Python3
# Python3 program to count the numbers
# within a range whose prime factors
# are only 2 and 3
 
# Function to count the number within
# a range whose prime factors are only
# 2 and 3
def findTwoThreePrime(l, r) :
 
    # Start with 2 so that 1
    # doesn't get counted
    if (l == 1) :
        l += 1
 
    count = 0
 
    for i in range(l, r + 1) :
        num = i
 
        # While num is divisible by 2,
        # divide it by 2
        while (num % 2 == 0) :
            num //= 2;
 
        # While num is divisible by 3,
        # divide it by 3
        while (num % 3 == 0) :
            num //= 3
 
        # If num got reduced to 1 then it has
        # only 2 and 3 as prime factors
        if (num == 1) :
            count += 1
 
    return count
 
# Driver code
if __name__ == "__main__" :
 
    l = 1
    r = 10
     
    print(findTwoThreePrime(l, r))
     
# This code is contributed by Ryuga


C#
// C# program to count the numbers
// within a range whose prime factors
// are only 2 and 3
using System;
 
class GFG
{
         
// Function to count the number
// within a range whose prime
// factors are only 2 and 3
static int findTwoThreePrime(int l, int r)
{
    // Start with 2 so that 1
    // doesn't get counted
    if (l == 1)
        l++;
 
    int count = 0;
 
    for (int i = l; i <= r; i++)
    {
        int num = i;
 
        // While num is divisible by 2,
        // divide it by 2
        while (num % 2 == 0)
            num /= 2;
 
        // While num is divisible by 3,
        // divide it by 3
        while (num % 3 == 0)
            num /= 3;
 
        // If num got reduced to 1 then it 
        // has only 2 and 3 as prime factors
        if (num == 1)
            count++;
    }
    return count;
}
 
// Driver code
static public void Main ()
{
    int l = 1, r = 10;
    Console.WriteLine(findTwoThreePrime(l, r));
}
}
 
// This code is contributed by akt_mit


PHP


Javascript


输出:
6

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。