📜  计数小于或等于N的整数,该整数具有正好为9的除数

📅  最后修改于: 2021-04-29 01:43:19             🧑  作者: Mango

给定数字N(1 <= N <= 10 9 ),任务是找到小于等于n的整数,该整数具有正好为9的除数。

例子:

天真的方法是对所有数字进行迭代直到N,然后计算正好有9个除数的数字。要计算除数的数量,可以轻松地迭代到N,然后检查N是否可被i整除并保持计数。
下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Function to count factors in O(N)
int numberOfDivisors(int num)
{
    int c = 0;
  
    // iterate and check if factor or not
    for (int i = 1; i <= num; i++) {
        if (num % i == 0) {
            c += 1;
        }
    }
    return c;
}
  
// Function to count numbers having
// exactly 9 divisors
int countNumbers(int n)
{
    int c = 0;
  
    // check for all numbers <=N
    for (int i = 1; i <= n; i++) {
        // check if exactly 9 factors or not
        if (numberOfDivisors(i) == 9)
            c += 1;
    }
    return c;
}
  
// Driver Code
int main()
{
    int n = 1000;
  
    cout << countNumbers(n);
  
return 0;
}


Java
// Java implementation of above approach
  
import java.io.*;
  
class GFG {
  
// Function to count factors in O(N)
static int numberOfDivisors(int num)
{
    int c = 0;
  
    // iterate and check if factor or not
    for (int i = 1; i <= num; i++) {
        if (num % i == 0) {
            c += 1;
        }
    }
    return c;
}
  
// Function to count numbers having
// exactly 9 divisors
static int countNumbers(int n)
{
    int c = 0;
  
    // check for all numbers <=N
    for (int i = 1; i <= n; i++) {
        // check if exactly 9 factors or not
        if (numberOfDivisors(i) == 9)
            c += 1;
    }
    return c;
}
  
       // Driver Code
    public static void main (String[] args) {
    int n = 1000;
  
    System.out.print(countNumbers(n));
    }
}
  
// This code is contributed by inder_verma..


Python 3
# Python 3 implementation of
# above approach
  
# Function to count factors in O(N)
def numberOfDivisors(num):
    c = 0
  
    # iterate and check if 
    # factor or not
    for i in range(1, num + 1) :
        if (num % i == 0) :
            c += 1
          
    return c
  
# Function to count numbers having
# exactly 9 divisors
def countNumbers(n):
  
    c = 0
  
    # check for all numbers <=N
    for i in range(1, n + 1) :
          
        # check if exactly 9 factors or not
        if (numberOfDivisors(i) == 9):
            c += 1
    return c
  
# Driver Code
if __name__ == "__main__":
    n = 1000
  
    print(countNumbers(n))
  
# This code is contributed
# by ChitraNayal


C#
// C# implementation of above approach
using System;
  
class GFG 
{
  
// Function to count factors in O(N)
static int numberOfDivisors(int num)
{
    int c = 0;
  
    // iterate and check if factor or not
    for (int i = 1; i <= num; i++) 
    {
        if (num % i == 0) 
        {
            c += 1;
        }
    }
    return c;
}
  
// Function to count numbers having
// exactly 9 divisors
static int countNumbers(int n)
{
    int c = 0;
  
    // check for all numbers <=N
    for (int i = 1; i <= n; i++) {
        // check if exactly 9 factors or not
        if (numberOfDivisors(i) == 9)
            c += 1;
    }
    return c;
}
  
// Driver Code
public static void Main ()
{
int n = 1000;
  
Console.Write(countNumbers(n));
}
}
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)


PHP


C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Function to count numbers having
// exactly 9 divisors
int countNumbers(int n)
{
    int c = 0;
  
    int limit = sqrt(n);
  
    // Sieve array
    int prime[limit + 1];
  
    // initially prime[i] = i
    for (int i = 1; i <= limit; i++)
        prime[i] = i;
  
    // use sieve concept to store the
    // first prime factor of every number
    for (int i = 2; i * i <= limit; i++) {
        if (prime[i] == i) {
            // mark all factors of i
            for (int j = i * i; j <= limit; j += i)
                if (prime[j] == j)
                    prime[j] = i;
        }
    }
  
    // check for all numbers if they can be
    // expressed in form p*q
    for (int i = 2; i <= limit; i++) {
        // p prime factor
        int p = prime[i];
  
        // q prime factor
        int q = prime[i / prime[i]];
  
        // if both prime factors are different
        // if p*q<=n and q!=
        if (p * q == i && q != 1 && p != q) {
            c += 1;
        }
        else if (prime[i] == i) {
  
            // Check if it can be expressed as p^8
            if (pow(i, 8) <= n) {
  
                c += 1;
            }
        }
    }
  
    return c;
}
  
// Driver Code
int main()
{
    int n = 1000;
  
    cout << countNumbers(n);
  
return 0;
}


Java
// Java implementation of above approach
public class GFG {
  
// Function to count numbers having
// exactly 9 divisors
    static int countNumbers(int n) {
        int c = 0;
  
        int limit = (int) Math.sqrt(n);
  
        // Sieve array
        int prime[] = new int[limit + 1];
  
        // initially prime[i] = i
        for (int i = 1; i <= limit; i++) {
            prime[i] = i;
        }
  
        // use sieve concept to store the
        // first prime factor of every number
        for (int i = 2; i * i <= limit; i++) {
            if (prime[i] == i) {
                // mark all factors of i
                for (int j = i * i; j <= limit; j += i) {
                    if (prime[j] == j) {
                        prime[j] = i;
                    }
                }
            }
        }
  
        // check for all numbers if they can be
        // expressed in form p*q
        for (int i = 2; i <= limit; i++) {
            // p prime factor
            int p = prime[i];
  
            // q prime factor
            int q = prime[i / prime[i]];
  
            // if both prime factors are different
            // if p*q<=n and q!=
            if (p * q == i && q != 1 && p != q) {
                c += 1;
            } else if (prime[i] == i) {
  
                // Check if it can be expressed as p^8
                if (Math.pow(i, 8) <= n) {
  
                    c += 1;
                }
            }
        }
  
        return c;
    }
  
// Driver Code
    public static void main(String[] args) {
        int n = 1000;
  
        System.out.println(countNumbers(n));
  
    }
}
/*This code is contributed by PrinciRaj1992*/


Python3
# Python3 implementation of the above approach 
  
# Function to count numbers 
# having exactly 9 divisors 
def countNumbers(n): 
      
    c = 0
    limit = int(n ** (0.5))
  
    # Sieve array, initially prime[i] = i 
    prime = [i for i in range(limit + 1)] 
      
    # use sieve concept to store the 
    # first prime factor of every number 
    i = 2
    while i * i <= limit: 
        if prime[i] == i: 
              
            # mark all factors of i 
            for j in range(i * i, limit + 1, i): 
                if prime[j] == j: 
                    prime[j] = i 
          
        i += 1
  
    # check for all numbers if they 
    # can be expressed in form p*q 
    for i in range(2, limit + 1): 
          
        # p prime factor 
        p = prime[i] 
  
        # q prime factor 
        q = prime[i // prime[i]] 
  
        # if both prime factors are different 
        # if p*q<=n and q!= 
        if p * q == i and q != 1 and p != q: 
            c += 1
          
        elif prime[i] == i: 
  
            # Check if it can be 
            # expressed as p^8 
            if i ** 8 <= n: 
                c += 1
      
    return c 
  
# Driver Code 
if __name__ == "__main__": 
  
    n = 1000
    print(countNumbers(n)) 
  
# This code is contributed
# by Rituraj Jain


C#
// C# implementation of above approach
using System; 
  
public class GFG { 
  
// Function to count numbers having
// exactly 9 divisors
    static int countNumbers(int n) {
        int c = 0;
   
        int limit = (int) Math.Sqrt(n);
   
        // Sieve array
        int []prime = new int[limit + 1];
   
        // initially prime[i] = i
        for (int i = 1; i <= limit; i++) {
            prime[i] = i;
        }
   
        // use sieve concept to store the
        // first prime factor of every number
        for (int i = 2; i * i <= limit; i++) {
            if (prime[i] == i) {
                // mark all factors of i
                for (int j = i * i; j <= limit; j += i) {
                    if (prime[j] == j) {
                        prime[j] = i;
                    }
                }
            }
        }
   
        // check for all numbers if they can be
        // expressed in form p*q
        for (int i = 2; i <= limit; i++) {
            // p prime factor
            int p = prime[i];
   
            // q prime factor
            int q = prime[i / prime[i]];
   
            // if both prime factors are different
            // if p*q<=n and q!=
            if (p * q == i && q != 1 && p != q) {
                c += 1;
            } else if (prime[i] == i) {
   
                // Check if it can be expressed as p^8
                if (Math.Pow(i, 8) <= n) {
   
                    c += 1;
                }
            }
        }
   
        return c;
    }
   
// Driver Code
    public static void Main() {
        int n = 1000;
   
        Console.WriteLine(countNumbers(n));
   
    }
}
/*This code is contributed by PrinciRaj1992*/


PHP


输出:
8

一种有效的方法是使用质因子属性来计算数字的除数的数量。该方法可以在这里找到。如果可以用(p ^ 2 * q ^ 2)或(p ^ 8)表示任何数字(let x),其中p和q是X的素数,则X共有9个除数。可以按照以下步骤解决上述问题。

  1. 使用筛子技术标记数字的最小质数。
  2. 我们只需要检查可以用p * q表示的range [1-sqrt(n)]中的所有数字,因为(p ^ 2 * q ^ 2)有9个因数,因此(p * q)^ 2也将有9个因子。
  3. 从1迭代到sqrt(n)并检查i是否可以表示为p * q ,其中p和q是质数。
  4. 还要检查i是否为质数,然后pow(i,8)<= n ,在这种情况下也应计算该数字。
  5. 可以以p * q和p ^ 8形式表示的数量计数之和是我们的答案。

下面是上述方法的实现:

C++

// C++ implementation of above approach
#include 
using namespace std;
  
// Function to count numbers having
// exactly 9 divisors
int countNumbers(int n)
{
    int c = 0;
  
    int limit = sqrt(n);
  
    // Sieve array
    int prime[limit + 1];
  
    // initially prime[i] = i
    for (int i = 1; i <= limit; i++)
        prime[i] = i;
  
    // use sieve concept to store the
    // first prime factor of every number
    for (int i = 2; i * i <= limit; i++) {
        if (prime[i] == i) {
            // mark all factors of i
            for (int j = i * i; j <= limit; j += i)
                if (prime[j] == j)
                    prime[j] = i;
        }
    }
  
    // check for all numbers if they can be
    // expressed in form p*q
    for (int i = 2; i <= limit; i++) {
        // p prime factor
        int p = prime[i];
  
        // q prime factor
        int q = prime[i / prime[i]];
  
        // if both prime factors are different
        // if p*q<=n and q!=
        if (p * q == i && q != 1 && p != q) {
            c += 1;
        }
        else if (prime[i] == i) {
  
            // Check if it can be expressed as p^8
            if (pow(i, 8) <= n) {
  
                c += 1;
            }
        }
    }
  
    return c;
}
  
// Driver Code
int main()
{
    int n = 1000;
  
    cout << countNumbers(n);
  
return 0;
}

Java

// Java implementation of above approach
public class GFG {
  
// Function to count numbers having
// exactly 9 divisors
    static int countNumbers(int n) {
        int c = 0;
  
        int limit = (int) Math.sqrt(n);
  
        // Sieve array
        int prime[] = new int[limit + 1];
  
        // initially prime[i] = i
        for (int i = 1; i <= limit; i++) {
            prime[i] = i;
        }
  
        // use sieve concept to store the
        // first prime factor of every number
        for (int i = 2; i * i <= limit; i++) {
            if (prime[i] == i) {
                // mark all factors of i
                for (int j = i * i; j <= limit; j += i) {
                    if (prime[j] == j) {
                        prime[j] = i;
                    }
                }
            }
        }
  
        // check for all numbers if they can be
        // expressed in form p*q
        for (int i = 2; i <= limit; i++) {
            // p prime factor
            int p = prime[i];
  
            // q prime factor
            int q = prime[i / prime[i]];
  
            // if both prime factors are different
            // if p*q<=n and q!=
            if (p * q == i && q != 1 && p != q) {
                c += 1;
            } else if (prime[i] == i) {
  
                // Check if it can be expressed as p^8
                if (Math.pow(i, 8) <= n) {
  
                    c += 1;
                }
            }
        }
  
        return c;
    }
  
// Driver Code
    public static void main(String[] args) {
        int n = 1000;
  
        System.out.println(countNumbers(n));
  
    }
}
/*This code is contributed by PrinciRaj1992*/

Python3

# Python3 implementation of the above approach 
  
# Function to count numbers 
# having exactly 9 divisors 
def countNumbers(n): 
      
    c = 0
    limit = int(n ** (0.5))
  
    # Sieve array, initially prime[i] = i 
    prime = [i for i in range(limit + 1)] 
      
    # use sieve concept to store the 
    # first prime factor of every number 
    i = 2
    while i * i <= limit: 
        if prime[i] == i: 
              
            # mark all factors of i 
            for j in range(i * i, limit + 1, i): 
                if prime[j] == j: 
                    prime[j] = i 
          
        i += 1
  
    # check for all numbers if they 
    # can be expressed in form p*q 
    for i in range(2, limit + 1): 
          
        # p prime factor 
        p = prime[i] 
  
        # q prime factor 
        q = prime[i // prime[i]] 
  
        # if both prime factors are different 
        # if p*q<=n and q!= 
        if p * q == i and q != 1 and p != q: 
            c += 1
          
        elif prime[i] == i: 
  
            # Check if it can be 
            # expressed as p^8 
            if i ** 8 <= n: 
                c += 1
      
    return c 
  
# Driver Code 
if __name__ == "__main__": 
  
    n = 1000
    print(countNumbers(n)) 
  
# This code is contributed
# by Rituraj Jain

C#

// C# implementation of above approach
using System; 
  
public class GFG { 
  
// Function to count numbers having
// exactly 9 divisors
    static int countNumbers(int n) {
        int c = 0;
   
        int limit = (int) Math.Sqrt(n);
   
        // Sieve array
        int []prime = new int[limit + 1];
   
        // initially prime[i] = i
        for (int i = 1; i <= limit; i++) {
            prime[i] = i;
        }
   
        // use sieve concept to store the
        // first prime factor of every number
        for (int i = 2; i * i <= limit; i++) {
            if (prime[i] == i) {
                // mark all factors of i
                for (int j = i * i; j <= limit; j += i) {
                    if (prime[j] == j) {
                        prime[j] = i;
                    }
                }
            }
        }
   
        // check for all numbers if they can be
        // expressed in form p*q
        for (int i = 2; i <= limit; i++) {
            // p prime factor
            int p = prime[i];
   
            // q prime factor
            int q = prime[i / prime[i]];
   
            // if both prime factors are different
            // if p*q<=n and q!=
            if (p * q == i && q != 1 && p != q) {
                c += 1;
            } else if (prime[i] == i) {
   
                // Check if it can be expressed as p^8
                if (Math.Pow(i, 8) <= n) {
   
                    c += 1;
                }
            }
        }
   
        return c;
    }
   
// Driver Code
    public static void Main() {
        int n = 1000;
   
        Console.WriteLine(countNumbers(n));
   
    }
}
/*This code is contributed by PrinciRaj1992*/

的PHP


输出:
8

时间复杂度: O(sqrt(N))
辅助空间: O(sqrt(N))