📜  具有n个因子或更大的最小整数

📅  最后修改于: 2021-04-29 02:48:20             🧑  作者: Mango

给定n,找到具有n个因子或更大因子的最小整数。可以假定结果小于1000001。

例子:

Input : n = 3
Output : 4
Explanation: 4 has factors 1, 2 and 4.

Input : n = 2 
Output : 2
Explanation: 2 has one factor 1 and 2.

计算因子数量的方法有很多,但是可以在这里找到有效的方法

简单方法:一个简单的方法是运行循环以找出数字的因数。找出O(x)中数字因数的一种方法是运行从1到x的循环,并查看所有除以x的数字。
时间复杂度:我们尝试的每个数字x的O(x),直到找到答案或达到极限。

高效的方法:我们可以在每次迭代中找出sqrt(x)中的因素。
时间复杂度:尝试找到每个数字x的O(sqrt(x)),直到找到答案或达到限制。

最佳方法是遍历每个数字并计算因子数量。然后检查计数是否等于或大于n,然后得到具有n或更多因子的所需最小整数。

下面是上述方法的实现:

C++
// c++ program to print the smallest
// integer with n factors or more
#include 
using namespace std;
  
const int MAX = 1000001;
  
// array to store prime factors
int factor[MAX] = { 0 };
  
// function to generate all prime factors
// of numbers from 1 to 10^6
void generatePrimeFactors()
{
    factor[1] = 1;
  
    // Initializes all the positions
    // with their value.
    for (int i = 2; i < MAX; i++)
        factor[i] = i;
  
    // Initializes all multiples of 2 with 2
    for (int i = 4; i < MAX; i += 2)
        factor[i] = 2;
  
    // A modified version of Sieve of
    // Eratosthenes to store the smallest
    // prime factor that divides every number.
    for (int i = 3; i * i < MAX; i++) {
  
        // check if it has no prime factor.
        if (factor[i] == i) {
  
            // Initializes of j starting from i*i
            for (int j = i * i; j < MAX; j += i) {
  
                // if it has no prime factor
                // before, then stores the
                // smallest prime divisor
                if (factor[j] == j)
                    factor[j] = i;
            }
        }
    }
}
  
// function to calculate number of factors
int calculateNoOFactors(int n)
{
    if (n == 1)
        return 1;
  
    int ans = 1;
  
    // stores the smallest prime number
    // that divides n
    int dup = factor[n];
  
    // stores the count of number of times
    // a prime number divides n.
    int c = 1;
  
    // reduces to the next number after prime
    // factorization of n
    int j = n / factor[n];
  
    // false when prime factorization is done
    while (j != 1) {
  
        // if the same prime number is dividing n,
        // then we increase the count
        if (factor[j] == dup)
            c += 1;
  
        /* if its a new prime factor that is
           factorizing n, then we again set c=1 
           and change dup to the new prime factor,
           and apply the formula explained above. */
        else {
            dup = factor[j];
            ans = ans * (c + 1);
            c = 1;
        }
  
        // prime factorizes a number
        j = j / factor[j];
    }
  
    // for the last prime factor
    ans = ans * (c + 1);
  
    return ans;
}
  
// function to find the smallest integer
// with n factors or more.
int smallest(int n)
{
    for (int i = 1;; i++)
  
        // check if no of factors is more
        // than n or not
        if (calculateNoOFactors(i) >= n)
            return i;
}
  
// Driver program to test above function
int main()
{
    // generate prime factors of number
    // upto 10^6
    generatePrimeFactors();
  
    int n = 4;
    cout << smallest(n);
    return 0;
}


Java
// Java program to print the smallest
// integer with n factors or more
import java.util.*;
import java.lang.*;
  
public class GfG{
    private static final int MAX = 1000001;
  
    // array to store prime factors
    private static final int[] factor = new int [MAX];
  
    // function to generate all prime factors
    // of numbers from 1 to 10^6
    public static void generatePrimeFactors()
    {
        factor[1] = 1;
  
        // Initializes all the positions
        // with their value.
        for (int i = 2; i < MAX; i++)
            factor[i] = i;
  
        // Initializes all multiples of 2 with 2
        for (int i = 4; i < MAX; i += 2)
            factor[i] = 2;
  
        // A modified version of Sieve of
        // Eratosthenes to store the smallest
        // prime factor that divides every number.
        for (int i = 3; i * i < MAX; i++) {
  
            // check if it has no prime factor.
            if (factor[i] == i) {
  
                // Initializes of j starting from i*i
                for (int j = i * i; j < MAX; j += i) {
  
                    // if it has no prime factor
                    // before, then stores the
                    // smallest prime divisor
                    if (factor[j] == j)
                        factor[j] = i;
                }
            }
        }
    }
  
    // function to calculate number of factors
    public static int calculateNoOFactors(int n)
    {
        if (n == 1)
            return 1;
  
        int ans = 1;
  
        // stores the smallest prime number
        // that divides n
        int dup = factor[n];
  
        // stores the count of number of times
        // a prime number divides n.
        int c = 1;
  
        // reduces to the next number after prime
        // factorization of n
        int j = n / factor[n];
  
        // false when prime factorization is done
        while (j != 1) {
  
            // if the same prime number is dividing n,
            // then we increase the count
            if (factor[j] == dup)
                c += 1;
  
            /* if its a new prime factor that is
            factorizing n, then we again set c=1 
            and change dup to the new prime factor,
            and apply the formula explained above. */
            else {
                dup = factor[j];
                ans = ans * (c + 1);
                c = 1;
            }
  
            // prime factorizes a number
            j = j / factor[j];
        }
  
        // for the last prime factor
        ans = ans * (c + 1);
  
        return ans; 
    }
  
    // function to find the smallest integer
    // with n factors or more.
    public static int smallest(int n)
    {
        for (int i = 1;; i++)
  
            // check if no of factors is more
            // than n or not
            if (calculateNoOFactors(i) >= n)
                return i;
    }
      
    // driver function
    public static void main(String args[])
    {
        // generate prime factors of number
        // upto 10^6
        generatePrimeFactors();
  
        int n = 4;
        System.out.println(smallest(n));
    }
}
  
/* This code is contributed by Sagar Shukla */


Python3
# Python3 program to print the 
# smallest integer with n
# factors or more
MAX = 100001;
  
# array to store
# prime factors
factor = [0] * MAX;
  
# function to generate all 
# prime factors of numbers
# from 1 to 10^6
def generatePrimeFactors():
      
    factor[1] = 1;
  
    # Initializes all the 
    # positions with their value.
    for i in range(2, MAX):
        factor[i] = i;
  
    # Initializes all
    # multiples of 2 with 2
    i = 4
    while(i < MAX):
        factor[i] = 2;
        i += 2;
  
    # A modified version of 
    # Sieve of Eratosthenes 
    # to store the smallest
    # prime factor that 
    # divides every number.
    i = 3; 
    while(i * i < MAX):
  
        # check if it has
        # no prime factor.
        if (factor[i] == i):
  
            # Initializes of j 
            # starting from i*i
            j = i * i;
            while(j < MAX): 
  
                # if it has no prime factor
                # before, then stores the
                # smallest prime divisor
                if (factor[j] == j):
                    factor[j] = i;
                j += i;
        i += 1;
  
# function to calculate 
# number of factors
def calculateNoOFactors(n):
    if (n == 1):
        return 1;
  
    ans = 1;
  
    # stores the smallest prime 
    # number that divides n
    dup = factor[n];
  
    # stores the count of 
    # number of times a 
    # prime number divides n.
    c = 1;
  
    # reduces to the next
    # number after prime
    # factorization of n
    j = int(n / factor[n]);
  
    # false when prime 
    # factorization is done
    while (j != 1):
  
        # if the same prime number 
        # is dividing n, then we 
        # increase the count
        if (factor[j] == dup):
            c += 1;
  
        # if its a new prime factor 
        # that is factorizing n, then 
        # we again set c=1 and change 
        # dup to the new prime factor,
        # and apply the formula 
        # explained above. 
        else:
            dup = factor[j];
            ans = ans * (c + 1);
            c = 1;
  
        # prime factorizes a number
        j = int(j / factor[j]);
  
    # for the last prime factor
    ans = ans * (c + 1);
  
    return ans;
  
# function to find the 
# smallest integer with
# n factors or more.
def smallest(n):
    i = 1;
    while(True):
          
        # check if no of 
        # factors is more
        # than n or not
        if (calculateNoOFactors(i) >= n):
            return i;
        i += 1;
  
# Driver Code
  
# generate prime factors 
# of number upto 10^6
generatePrimeFactors();
  
n = 4;
print(smallest(n));
  
# This code is contributed by mits


C#
// C# program to print the smallest
// integer with n factors or more
using System;
  
class GfG {
    private static int MAX = 1000001;
  
    // array to store prime factors
    private static int []factor = new int [MAX];
  
    // function to generate all prime 
    // factorsof numbers from 1 to 10^6
    public static void generatePrimeFactors()
    {
        factor[1] = 1;
  
        // Initializes all the positions
        // with their value.
        for (int i = 2; i < MAX; i++)
            factor[i] = i;
  
        // Initializes all multiples of 2 with 2
        for (int i = 4; i < MAX; i += 2)
            factor[i] = 2;
  
        // A modified version of Sieve of
        // Eratosthenes to store the smallest
        // prime factor that divides every number.
        for (int i = 3; i * i < MAX; i++) {
  
            // check if it has no prime factor.
            if (factor[i] == i) {
  
                // Initializes of j starting from i*i
                for (int j = i * i; j < MAX; j += i)
                {
  
                    // if it has no prime factor
                    // before, then stores the
                    // smallest prime divisor
                    if (factor[j] == j)
                        factor[j] = i;
                }
            }
        }
    }
  
    // function to calculate number of factors
    public static int calculateNoOFactors(int n)
    {
        if (n == 1)
            return 1;
  
        int ans = 1;
  
        // stores the smallest prime number
        // that divides n
        int dup = factor[n];
  
        // stores the count of number of times
        // a prime number divides n.
        int c = 1;
  
        // reduces to the next number after prime
        // factorization of n
        int j = n / factor[n];
  
        // false when prime factorization is done
        while (j != 1) {
  
            // if the same prime number is dividing n,
            // then we increase the count
            if (factor[j] == dup)
                c += 1;
  
            // if its a new prime factor that is
            // factorizing n, then we again set c=1 
            // and change dup to the new prime factor,
            // and apply the formula explained above.
            else {
                dup = factor[j];
                ans = ans * (c + 1);
                c = 1;
            }
  
            // prime factorizes a number
            j = j / factor[j];
        }
  
        // for the last prime factor
        ans = ans * (c + 1);
  
        return ans; 
    }
  
    // function to find the smallest integer
    // with n factors or more.
    public static int smallest(int n)
    {
        for (int i = 1; ; i++)
  
            // check if no of factors is more
            // than n or not
            if (calculateNoOFactors(i) >= n)
                return i;
    }
      
    // Driver Code
    public static void Main()
    {
          
        // generate prime factors of 
        // number upto 10^6
        generatePrimeFactors();
  
        int n = 4;
        Console.Write(smallest(n));
    }
}
  
// This code is contributed by Nitin Mittal.


PHP
= $n)
            return $i;
}
  
// Driver Code
  
// generate prime factors 
// of number upto 10^6
generatePrimeFactors();
  
$n = 4;
echo smallest($n);
  
// This code is contributed by mits
?>


输出:

6

时间复杂度:在得到答案之前,我们检查的每个计算数字为O(log(max(max(number))))。