📜  求N的最大因数,使N / F小于K

📅  最后修改于: 2021-06-27 01:54:26             🧑  作者: Mango

给定两个数字NK ,任务是找到最小值X ,使N

例子:

天真的方法:给定的问题陈述可以可视化为方程K * X = N。在该方程式中,目标是使X最小。因此,我们必须找到除以N最大K。步骤如下:

  • 迭代[1,K]
  • 检查每个数字i ,使(N%i)= 0 。继续更新max变量,该变量存储遍历到iN的最大除数。
  • 所需的答案是N / max

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the value of X
void findMaxValue(int N, int K)
{
    int packages;
    int maxi = 1;
 
    // Loop to check all the numbers
    // divisible by N that yield
    // minimum N/i value
    for (int i = 1; i <= K; i++) {
        if (N % i == 0)
            maxi = max(maxi, i);
    }
 
    packages = N / maxi;
 
    // Print the value of packages
    cout << packages << endl;
}
 
// Driver Code
int main()
{
    // Given N and K
    int N = 8, K = 7;
 
    // Function Call
    findMaxValue(N, K);
    return 0;
}


Java
// Java program for the above approach
import java.util.Arrays;
 
class GFG{
     
// Function to find the value of X
static void findMaxValue(int N, int K)
{
    int packages;
    int maxi = 1;
 
    // Loop to check all the numbers
    // divisible by N that yield
    // minimum N/i value
    for(int i = 1; i <= K; i++)
    {
        if (N % i == 0)
            maxi = Math.max(maxi, i);
    }
    packages = N / maxi;
 
    // Print the value of packages
    System.out.println(packages);
}
 
// Driver code
public static void main (String[] args)
{
     
    // Given N and K
    int N = 8, K = 7;
 
    // Function call
    findMaxValue(N, K);
}
}
 
// This code is contributed by Shubham Prakash


Python3
# Python3 program for the above approach
 
# Function to find the value of X
def findMaxValue(N, K):
    packages = 0;
    maxi = 1;
 
    # Loop to check all the numbers
    # divisible by N that yield
    # minimum N/i value
    for i in range(1, K + 1):
        if (N % i == 0):
            maxi = max(maxi, i);
 
    packages = N // maxi;
 
    # Prthe value of packages
    print(packages);
 
# Driver code
if __name__ == '__main__':
   
    # Given N and K
    N = 8;
    K = 7;
     
    # Function call
    findMaxValue(N, K);
 
# This code is contributed by sapnasingh4991


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the value of X
static void findMaxValue(int N, int K)
{
    int packages;
    int maxi = 1;
 
    // Loop to check all the numbers
    // divisible by N that yield
    // minimum N/i value
    for(int i = 1; i <= K; i++)
    {
        if (N % i == 0)
            maxi = Math.Max(maxi, i);
    }
    packages = N / maxi;
 
    // Print the value of packages
    Console.WriteLine(packages);
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Given N and K
    int N = 8, K = 7;
 
    // Function call
    findMaxValue(N, K);
}
}
 
// This code is contributed by Amit Katiyar


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the largest
// factor of N which is less than
// or equal to K
int solve(int n, int k)
{
    // Initialise the variable to
    // store the largest factor of
    // N <= K
    int ans = 0;
 
    // Loop to find all factors of N
    for (int j = 1;
        j * j <= n; j++) {
 
        // Check if j is a
        // factor of N or not
        if (n % j == 0) {
 
            // Check if j <= K
            // If yes, then store
            // the larger value between
            // ans and j in ans
            if (j <= k) {
                ans = max(ans, j);
            }
 
            // Check if N/j <= K
            // If yes, then store
            // the larger value between
            // ans and j in ans
            if (n / j <= k) {
                ans = max(ans, n / j);
            }
        }
    }
 
    // Since max value is always
    // stored in ans, the maximum
    // value divisible by N less than
    // or equal to K will be returned.
    return ans;
}
 
// Driver Code
int main()
{
    // Given N and K
    int N = 8, K = 7;
 
    // Function Call
    cout << (N / solve(N, K));
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the largest
// factor of N which is less than
// or equal to K
static int solve(int n, int k)
{
     
    // Initialise the variable to
    // store the largest factor of
    // N <= K
    int ans = 0;
 
    // Loop to find all factors of N
    for(int j = 1; j * j <= n; j++)
    {
         
        // Check if j is a
        // factor of N or not
        if (n % j == 0)
        {
             
            // Check if j <= K
            // If yes, then store
            // the larger value between
            // ans and j in ans
            if (j <= k)
            {
                ans = Math.max(ans, j);
            }
 
            // Check if N/j <= K
            // If yes, then store
            // the larger value between
            // ans and j in ans
            if (n / j <= k)
            {
                ans = Math.max(ans, n / j);
            }
        }
    }
 
    // Since max value is always
    // stored in ans, the maximum
    // value divisible by N less than
    // or equal to K will be returned.
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given N and K
    int N = 8, K = 7;
 
    // Function call
    System.out.print((N / solve(N, K)));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to find the largest
# factor of N which is less than
# or equal to K
def solve(n, k):
 
    # Initialise the variable to
    # store the largest factor of
    # N <= K
    ans = 0;
 
    # Loop to find all factors of N
    for j in range(1, n + 1):
        if (j * j > n):
            break;
 
        # Check if j is a
        # factor of N or not
        if (n % j == 0):
 
            # Check if j <= K
            # If yes, then store
            # the larger value between
            # ans and j in ans
            if (j <= k):
                ans = max(ans, j);
             
            # Check if N/j <= K
            # If yes, then store
            # the larger value between
            # ans and j in ans
            if (n // j <= k):
                ans = max(ans, n // j);
             
    # Since max value is always
    # stored in ans, the maximum
    # value divisible by N less than
    # or equal to K will be returned.
    return ans;
 
# Driver Code
if __name__ == '__main__':
 
    # Given N and K
    N = 8; K = 7;
 
    # Function call
    print((N // solve(N, K)));
 
# This code is contributed by gauravrajput1


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the largest
// factor of N which is less than
// or equal to K
static int solve(int n, int k)
{
     
    // Initialise the variable to
    // store the largest factor of
    // N <= K
    int ans = 0;
 
    // Loop to find all factors of N
    for(int j = 1; j * j <= n; j++)
    {
         
        // Check if j is a
        // factor of N or not
        if (n % j == 0)
        {
             
            // Check if j <= K
            // If yes, then store
            // the larger value between
            // ans and j in ans
            if (j <= k)
            {
                ans = Math.Max(ans, j);
            }
 
            // Check if N/j <= K
            // If yes, then store
            // the larger value between
            // ans and j in ans
            if (n / j <= k)
            {
                ans = Math.Max(ans, n / j);
            }
        }
    }
 
    // Since max value is always
    // stored in ans, the maximum
    // value divisible by N less than
    // or equal to K will be returned.
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given N and K
    int N = 8, K = 7;
 
    // Function call
    Console.Write((N / solve(N, K)));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
2

时间复杂度: O(K)
辅助空间: O(1)

高效方法:为了优化上述方法,我们将使用本文中讨论的高效方法来找到因素。步骤如下:

  1. 初始化ans变量以存储N的最大因子。
  2. 遍历[1,sqrt(N)]并执行以下操作:
    • 检查N是否可被i整除。
    • 如果没有,请检查下一个数字。
    • 否则,如果i≤KN / i≤K ,则将ans更新为最大值(i,N / i)
  3. X的值为N / ans。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the largest
// factor of N which is less than
// or equal to K
int solve(int n, int k)
{
    // Initialise the variable to
    // store the largest factor of
    // N <= K
    int ans = 0;
 
    // Loop to find all factors of N
    for (int j = 1;
        j * j <= n; j++) {
 
        // Check if j is a
        // factor of N or not
        if (n % j == 0) {
 
            // Check if j <= K
            // If yes, then store
            // the larger value between
            // ans and j in ans
            if (j <= k) {
                ans = max(ans, j);
            }
 
            // Check if N/j <= K
            // If yes, then store
            // the larger value between
            // ans and j in ans
            if (n / j <= k) {
                ans = max(ans, n / j);
            }
        }
    }
 
    // Since max value is always
    // stored in ans, the maximum
    // value divisible by N less than
    // or equal to K will be returned.
    return ans;
}
 
// Driver Code
int main()
{
    // Given N and K
    int N = 8, K = 7;
 
    // Function Call
    cout << (N / solve(N, K));
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the largest
// factor of N which is less than
// or equal to K
static int solve(int n, int k)
{
     
    // Initialise the variable to
    // store the largest factor of
    // N <= K
    int ans = 0;
 
    // Loop to find all factors of N
    for(int j = 1; j * j <= n; j++)
    {
         
        // Check if j is a
        // factor of N or not
        if (n % j == 0)
        {
             
            // Check if j <= K
            // If yes, then store
            // the larger value between
            // ans and j in ans
            if (j <= k)
            {
                ans = Math.max(ans, j);
            }
 
            // Check if N/j <= K
            // If yes, then store
            // the larger value between
            // ans and j in ans
            if (n / j <= k)
            {
                ans = Math.max(ans, n / j);
            }
        }
    }
 
    // Since max value is always
    // stored in ans, the maximum
    // value divisible by N less than
    // or equal to K will be returned.
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given N and K
    int N = 8, K = 7;
 
    // Function call
    System.out.print((N / solve(N, K)));
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python3 program for the above approach
 
# Function to find the largest
# factor of N which is less than
# or equal to K
def solve(n, k):
 
    # Initialise the variable to
    # store the largest factor of
    # N <= K
    ans = 0;
 
    # Loop to find all factors of N
    for j in range(1, n + 1):
        if (j * j > n):
            break;
 
        # Check if j is a
        # factor of N or not
        if (n % j == 0):
 
            # Check if j <= K
            # If yes, then store
            # the larger value between
            # ans and j in ans
            if (j <= k):
                ans = max(ans, j);
             
            # Check if N/j <= K
            # If yes, then store
            # the larger value between
            # ans and j in ans
            if (n // j <= k):
                ans = max(ans, n // j);
             
    # Since max value is always
    # stored in ans, the maximum
    # value divisible by N less than
    # or equal to K will be returned.
    return ans;
 
# Driver Code
if __name__ == '__main__':
 
    # Given N and K
    N = 8; K = 7;
 
    # Function call
    print((N // solve(N, K)));
 
# This code is contributed by gauravrajput1

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the largest
// factor of N which is less than
// or equal to K
static int solve(int n, int k)
{
     
    // Initialise the variable to
    // store the largest factor of
    // N <= K
    int ans = 0;
 
    // Loop to find all factors of N
    for(int j = 1; j * j <= n; j++)
    {
         
        // Check if j is a
        // factor of N or not
        if (n % j == 0)
        {
             
            // Check if j <= K
            // If yes, then store
            // the larger value between
            // ans and j in ans
            if (j <= k)
            {
                ans = Math.Max(ans, j);
            }
 
            // Check if N/j <= K
            // If yes, then store
            // the larger value between
            // ans and j in ans
            if (n / j <= k)
            {
                ans = Math.Max(ans, n / j);
            }
        }
    }
 
    // Since max value is always
    // stored in ans, the maximum
    // value divisible by N less than
    // or equal to K will be returned.
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given N and K
    int N = 8, K = 7;
 
    // Function call
    Console.Write((N / solve(N, K)));
}
}
 
// This code is contributed by 29AjayKumar

Java脚本


输出:
2

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