📌  相关文章
📜  计算给定范围内除数最大的元素

📅  最后修改于: 2021-04-26 10:06:22             🧑  作者: Mango

给定两个数字X和Y。任务是查找[X,Y]范围内(包括两个端点)的元素的数量,这些元素的除数最大。

例子

方法1:

  • 逐一遍历从X到Y的所有元素。
  • 查找每个元素的除数。
  • 将除数的数量存储在数组中,并更新除数的最大数量(maxDivisors)。
  • 遍历包含除数的数组,并计算等于maxDivisors的元素数。
  • 返回计数。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Function to count the divisors
int countDivisors(int n)
{
    int count = 0;
  
    // Note that this loop runs till square root
    for (int i = 1; i <= sqrt(n); i++) {
        if (n % i == 0) {
  
            // If divisors are equal, print only one
            if (n / i == i)
                count++;
  
            else // Otherwise print both
                count += 2;
        }
    }
  
    return count;
}
  
// Function to count the number with
// maximum divisors
int MaximumDivisors(int X, int Y)
{
    int maxDivisors = INT_MIN, result = 0;
  
    // to store number of divisors
    int arr[Y - X + 1];
  
    // Traverse from X to Y
    for (int i = X; i <= Y; i++) {
  
            // Count the number of divisors of i
             int Div = countDivisors(i);
  
            // Store the value of div in an array
             arr[i - X] = Div;
  
            // Update the value of maxDivisors
             maxDivisors = max(Div, maxDivisors);
  
    }
  
    // Traverse the array 
    for (int i = 0; i < (Y - X + 1); i++)
  
        // Count the value equals to maxDivisors
        if (arr[i] == maxDivisors)
            result++;
  
    return result;
}
  
// Driver Code
int main()
{
    int X = 1, Y = 10;
  
    // function call
    cout << MaximumDivisors(X, Y) << endl;
  
    return 0;
}


Java
// Java implementation of above approach
class GFG 
{
  
// Function to count the divisors
static int countDivisors(int n)
{
int count = 0;
  
// Note that this loop 
// runs till square root
for (int i = 1; i <= Math.sqrt(n); i++) 
{
    if (n % i == 0) 
    {
  
        // If divisors are equal,
        // print only one
        if (n / i == i)
            count++;
  
        else // Otherwise print both
            count += 2;
    }
}
  
return count;
}
  
// Function to count the number 
// with maximum divisors
static int MaximumDivisors(int X, int Y)
{
int maxDivisors = 0, result = 0;
  
// to store number of divisors
int[] arr = new int[Y - X + 1];
  
// Traverse from X to Y
for (int i = X; i <= Y; i++) 
{
  
    // Count the number of divisors of i
    int Div = countDivisors(i);
  
    // Store the value of div in an array
    arr[i - X] = Div;
  
    // Update the value of maxDivisors
    maxDivisors = Math.max(Div, maxDivisors);
  
}
  
// Traverse the array 
for (int i = 0; i < (Y - X + 1); i++)
  
    // Count the value equals 
    // to maxDivisors
    if (arr[i] == maxDivisors)
        result++;
  
return result;
}
  
// Driver Code
public static void main(String[] args) 
{
    int X = 1, Y = 10;
  
    // function call
    System.out.println(MaximumDivisors(X, Y));
}
}
  
// This code is contributed 
// by ChitraNayal


Python3
# from math module import everything
from math import *
  
# Python 3 implementation of above approach
  
# Function to count the divisors 
def countDivisors(n) :
    count = 0
      
    # Note that this loop runs till square root 
    for i in range(1,int(sqrt(n)+1)) :
        if n % i == 0 :
  
            # If divisors are equal, print only one 
            if n / i == i :
                count += 1
                  
            # Otherwise print both
            else :
                count += 2
  
    return count
  
# Function to count the number with 
# maximum divisors 
def MaximumDivisors(X,Y) :
    result = 0
    maxDivisors = 0
  
    # create list to store number of divisors 
    arr = []
      
    # initialize with 0 upto length Y-X+1
    for i in range(Y - X + 1) :
        arr.append(0)
  
    # Traverse from X to Y   
    for i in range(X,Y+1) :
  
        # Count the number of divisors of i 
        Div = countDivisors(i)
  
        # Store the value of div in an array 
        arr[i - X] = Div
          
        # Update the value of maxDivisors 
        maxDivisors = max(Div,maxDivisors)
          
    # Traverse the array  
    for i in range (Y - X + 1) :
  
        # Count the value equals to maxDivisors
        if arr[i] == maxDivisors :
            result += 1
  
    return result
  
# Driver code
if __name__ == "__main__" :
  
    X, Y = 1, 10
  
    # function call 
    print(MaximumDivisors(X,Y))


C#
// C# implementation of above approach
using System;
  
class GFG
{
  
// Function to count the divisors
static int countDivisors(int n)
{
int count = 0;
  
// Note that this loop 
// runs till square root
for (int i = 1; i <= Math.Sqrt(n); i++)
{
    if (n % i == 0) 
    {
  
        // If divisors are equal,
        // print only one
        if (n / i == i)
            count++;
  
        else // Otherwise print both
            count += 2;
    }
}
  
return count;
}
  
// Function to count the number 
// with maximum divisors
static int MaximumDivisors(int X, int Y)
{
int maxDivisors = 0, result = 0;
  
// to store number of divisors
int[] arr = new int[Y - X + 1];
  
// Traverse from X to Y
for (int i = X; i <= Y; i++)
{
  
    // Count the number of divisors of i
    int Div = countDivisors(i);
  
    // Store the value of div in an array
    arr[i - X] = Div;
  
    // Update the value of maxDivisors
    maxDivisors = Math.Max(Div, maxDivisors);
  
}
  
// Traverse the array 
for (int i = 0; i < (Y - X + 1); i++)
  
    // Count the value equals 
    // to maxDivisors
    if (arr[i] == maxDivisors)
        result++;
  
return result;
}
  
// Driver Code
public static void Main()
{
    int X = 1, Y = 10;
  
    // function call
    Console.Write(MaximumDivisors(X, Y));
}
}
  
// This code is contributed
// by ChitraNayal


PHP


C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Function to count the elements
// with maximum number of divisors
int MaximumDivisors(int X, int Y)
{
    // to store number of divisors
    int arr[Y - X + 1];
  
    // initialise with zero
    memset(arr, 0, sizeof(arr));
  
    // to store the maximum number of divisors
    int mx = INT_MIN;
  
    // to store required answer
    int cnt = 0;
  
    for (int i = 1; i * i <= Y; i++) {
        int sq = i * i;
        int first_divisible;
  
        // Find the first divisible number
        if ((X / i) * i >= X)
            first_divisible = (X / i) * i;
        else
            first_divisible = (X / i + 1) * i;
  
        // Count number of divisors
        for (int j = first_divisible; j <= Y; j += i) {
            if (j < sq)
                continue;
            else if (j == sq)
                arr[j - X]++;
            else
                arr[j - X] += 2;
        }
    }
  
    // Find number of elements with
    // maximum number of divisors
    for (int i = X; i <= Y; i++) {
        if (arr[i - X] > mx) {
            cnt = 1;
            mx = arr[i - X];
        }
        else if (arr[i - X] == mx)
            cnt++;
    }
  
    return cnt;
}
  
// Driver code
int main()
{
    int X = 1, Y = 10;
    cout << MaximumDivisors(X, Y) << endl;
  
    return 0;
}


Java
// Java implementation of above approach
class GFG 
{
  
// Function to count the elements
// with maximum number of divisors
static int MaximumDivisors(int X, int Y)
{
      
// to store number of divisors
int[] arr = new int[Y - X + 1];
  
// initialise with zero
for(int i = 0; i < arr.length; i++)
    arr[i] = 0;
  
// to store the maximum 
// number of divisors
int mx = 0;
  
// to store required answer
int cnt = 0;
  
for (int i = 1; i * i <= Y; i++)
{
    int sq = i * i;
    int first_divisible;
  
    // Find the first divisible number
    if ((X / i) * i >= X)
        first_divisible = (X / i) * i;
    else
        first_divisible = (X / i + 1) * i;
  
    // Count number of divisors
    for (int j = first_divisible;
             j <= Y; j += i) 
    {
        if (j < sq)
            continue;
        else if (j == sq)
            arr[j - X]++;
        else
            arr[j - X] += 2;
    }
}
  
// Find number of elements with
// maximum number of divisors
for (int i = X; i <= Y; i++)
{
    if (arr[i - X] > mx) 
    {
        cnt = 1;
        mx = arr[i - X];
    }
    else if (arr[i - X] == mx)
        cnt++;
}
  
return cnt;
}
  
// Driver code
public static void main(String[] args) 
{
    int X = 1, Y = 10;
    System.out.println(MaximumDivisors(X, Y));
}
}
  
// This code is contributed 
// by ChitraNayal


Python 3
# Python 3 implementation of above approach
  
# Function to count the elements
# with maximum number of divisors
def MaximumDivisors(X, Y):
  
    # to store number of divisors
    # initialise with zero
    arr = [0] * (Y - X + 1)
  
    # to store the maximum 
    # number of divisors
    mx = 0
  
    # to store required answer
    cnt = 0
  
    i = 1
    while i * i <= Y :
        sq = i * i
  
        # Find the first divisible number
        if ((X // i) * i >= X) :
            first_divisible = (X // i) * i
        else:
            first_divisible = (X // i + 1) * i
  
        # Count number of divisors
        for j in range(first_divisible, Y + 1, i):
            if j < sq :
                continue
            elif j == sq :
                arr[j - X] += 1
            else:
                arr[j - X] += 2
        i += 1
  
    # Find number of elements with
    # maximum number of divisors
    for i in range(X, Y + 1):
        if arr[i - X] > mx :
            cnt = 1
            mx = arr[i - X]
  
        elif arr[i - X] == mx :
            cnt += 1
  
    return cnt
  
# Driver code
if __name__ == "__main__":
    X = 1
    Y = 10
    print(MaximumDivisors(X, Y))
  
# This code is contributed 
# by ChitraNayal


C#
// C# implementation of above approach
using System;
  
class GFG 
{
  
// Function to count the elements
// with maximum number of divisors
static int MaximumDivisors(int X, int Y)
{
      
// to store number of divisors
int[] arr = new int[Y - X + 1];
  
// initialise with zero
for(int i = 0; i < arr.Length; i++)
    arr[i] = 0;
  
// to store the maximum 
// number of divisors
int mx = 0;
  
// to store required answer
int cnt = 0;
  
for (int i = 1; i * i <= Y; i++) 
{
    int sq = i * i;
    int first_divisible;
  
    // Find the first divisible number
    if ((X / i) * i >= X)
        first_divisible = (X / i) * i;
    else
        first_divisible = (X / i + 1) * i;
  
    // Count number of divisors
    for (int j = first_divisible; 
             j <= Y; j += i) 
    {
        if (j < sq)
            continue;
        else if (j == sq)
            arr[j - X]++;
        else
            arr[j - X] += 2;
    }
}
  
// Find number of elements with
// maximum number of divisors
for (int i = X; i <= Y; i++)
{
    if (arr[i - X] > mx) 
    {
        cnt = 1;
        mx = arr[i - X];
    }
    else if (arr[i - X] == mx)
        cnt++;
}
  
return cnt;
}
  
// Driver code
public static void Main()
{
    int X = 1, Y = 10;
    Console.Write(MaximumDivisors(X, Y));
}
}
  
// This code is contributed 
// by ChitraNayal


PHP
= $X)
            $first_divisible = ($X / $i) * $i;
        else
            $first_divisible = ($X / $i + 1) * $i;
  
        // Count number of divisors
        for ($j = $first_divisible; 
             $j < $Y; $j += $i)
        {
            if ($j < $sq)
                continue;
            else if ($j == $sq)
                $arr[$j - $X]++;
            else
                $arr[$j - $X] += 2;
        }
    }
  
    // Find number of elements with
    // maximum number of divisors
    for ($i = $X; $i <= $Y; $i++) 
    {
        if ($arr[$i - $X] > $mx)  
        {
            $cnt = 1;
            $mx = $arr[$i - $X];
        }
        else if ($arr[$i - $X] == $mx)
            $cnt++;
    }
  
    return $cnt;
}
  
// Driver code
$X = 1;
$Y = 10;
echo MaximumDivisors($X, $Y)."\n";
  
// This code is contributed 
// by ChitraNayal
?>


输出:
3

方法2:

  • 创建大小为Y-X + 1的数组,以将X的除数存储在arr [0]中,将X + 1的除数存储在arr [1]中,直到Y。
  • 要获取从X到Y的所有数字的除数的数量,请运行两个循环(嵌套)。
  • 运行从1到sqrt(Y)的外部循环。
  • 运行从first_divisible到Y的内部循环。
  • First_divisible是一个数字,它是第一个可被I(外循环)整除且大于或等于X的数字。

在这里,first_divisible是使用“查找最接近n且可被m整除的数”来计算的。然后找到该数字的除数。

下面是上述方法的实现:

C++

// C++ implementation of above approach
#include 
using namespace std;
  
// Function to count the elements
// with maximum number of divisors
int MaximumDivisors(int X, int Y)
{
    // to store number of divisors
    int arr[Y - X + 1];
  
    // initialise with zero
    memset(arr, 0, sizeof(arr));
  
    // to store the maximum number of divisors
    int mx = INT_MIN;
  
    // to store required answer
    int cnt = 0;
  
    for (int i = 1; i * i <= Y; i++) {
        int sq = i * i;
        int first_divisible;
  
        // Find the first divisible number
        if ((X / i) * i >= X)
            first_divisible = (X / i) * i;
        else
            first_divisible = (X / i + 1) * i;
  
        // Count number of divisors
        for (int j = first_divisible; j <= Y; j += i) {
            if (j < sq)
                continue;
            else if (j == sq)
                arr[j - X]++;
            else
                arr[j - X] += 2;
        }
    }
  
    // Find number of elements with
    // maximum number of divisors
    for (int i = X; i <= Y; i++) {
        if (arr[i - X] > mx) {
            cnt = 1;
            mx = arr[i - X];
        }
        else if (arr[i - X] == mx)
            cnt++;
    }
  
    return cnt;
}
  
// Driver code
int main()
{
    int X = 1, Y = 10;
    cout << MaximumDivisors(X, Y) << endl;
  
    return 0;
}

Java

// Java implementation of above approach
class GFG 
{
  
// Function to count the elements
// with maximum number of divisors
static int MaximumDivisors(int X, int Y)
{
      
// to store number of divisors
int[] arr = new int[Y - X + 1];
  
// initialise with zero
for(int i = 0; i < arr.length; i++)
    arr[i] = 0;
  
// to store the maximum 
// number of divisors
int mx = 0;
  
// to store required answer
int cnt = 0;
  
for (int i = 1; i * i <= Y; i++)
{
    int sq = i * i;
    int first_divisible;
  
    // Find the first divisible number
    if ((X / i) * i >= X)
        first_divisible = (X / i) * i;
    else
        first_divisible = (X / i + 1) * i;
  
    // Count number of divisors
    for (int j = first_divisible;
             j <= Y; j += i) 
    {
        if (j < sq)
            continue;
        else if (j == sq)
            arr[j - X]++;
        else
            arr[j - X] += 2;
    }
}
  
// Find number of elements with
// maximum number of divisors
for (int i = X; i <= Y; i++)
{
    if (arr[i - X] > mx) 
    {
        cnt = 1;
        mx = arr[i - X];
    }
    else if (arr[i - X] == mx)
        cnt++;
}
  
return cnt;
}
  
// Driver code
public static void main(String[] args) 
{
    int X = 1, Y = 10;
    System.out.println(MaximumDivisors(X, Y));
}
}
  
// This code is contributed 
// by ChitraNayal

的Python 3

# Python 3 implementation of above approach
  
# Function to count the elements
# with maximum number of divisors
def MaximumDivisors(X, Y):
  
    # to store number of divisors
    # initialise with zero
    arr = [0] * (Y - X + 1)
  
    # to store the maximum 
    # number of divisors
    mx = 0
  
    # to store required answer
    cnt = 0
  
    i = 1
    while i * i <= Y :
        sq = i * i
  
        # Find the first divisible number
        if ((X // i) * i >= X) :
            first_divisible = (X // i) * i
        else:
            first_divisible = (X // i + 1) * i
  
        # Count number of divisors
        for j in range(first_divisible, Y + 1, i):
            if j < sq :
                continue
            elif j == sq :
                arr[j - X] += 1
            else:
                arr[j - X] += 2
        i += 1
  
    # Find number of elements with
    # maximum number of divisors
    for i in range(X, Y + 1):
        if arr[i - X] > mx :
            cnt = 1
            mx = arr[i - X]
  
        elif arr[i - X] == mx :
            cnt += 1
  
    return cnt
  
# Driver code
if __name__ == "__main__":
    X = 1
    Y = 10
    print(MaximumDivisors(X, Y))
  
# This code is contributed 
# by ChitraNayal

C#

// C# implementation of above approach
using System;
  
class GFG 
{
  
// Function to count the elements
// with maximum number of divisors
static int MaximumDivisors(int X, int Y)
{
      
// to store number of divisors
int[] arr = new int[Y - X + 1];
  
// initialise with zero
for(int i = 0; i < arr.Length; i++)
    arr[i] = 0;
  
// to store the maximum 
// number of divisors
int mx = 0;
  
// to store required answer
int cnt = 0;
  
for (int i = 1; i * i <= Y; i++) 
{
    int sq = i * i;
    int first_divisible;
  
    // Find the first divisible number
    if ((X / i) * i >= X)
        first_divisible = (X / i) * i;
    else
        first_divisible = (X / i + 1) * i;
  
    // Count number of divisors
    for (int j = first_divisible; 
             j <= Y; j += i) 
    {
        if (j < sq)
            continue;
        else if (j == sq)
            arr[j - X]++;
        else
            arr[j - X] += 2;
    }
}
  
// Find number of elements with
// maximum number of divisors
for (int i = X; i <= Y; i++)
{
    if (arr[i - X] > mx) 
    {
        cnt = 1;
        mx = arr[i - X];
    }
    else if (arr[i - X] == mx)
        cnt++;
}
  
return cnt;
}
  
// Driver code
public static void Main()
{
    int X = 1, Y = 10;
    Console.Write(MaximumDivisors(X, Y));
}
}
  
// This code is contributed 
// by ChitraNayal

的PHP

= $X)
            $first_divisible = ($X / $i) * $i;
        else
            $first_divisible = ($X / $i + 1) * $i;
  
        // Count number of divisors
        for ($j = $first_divisible; 
             $j < $Y; $j += $i)
        {
            if ($j < $sq)
                continue;
            else if ($j == $sq)
                $arr[$j - $X]++;
            else
                $arr[$j - $X] += 2;
        }
    }
  
    // Find number of elements with
    // maximum number of divisors
    for ($i = $X; $i <= $Y; $i++) 
    {
        if ($arr[$i - $X] > $mx)  
        {
            $cnt = 1;
            $mx = $arr[$i - $X];
        }
        else if ($arr[$i - $X] == $mx)
            $cnt++;
    }
  
    return $cnt;
}
  
// Driver code
$X = 1;
$Y = 10;
echo MaximumDivisors($X, $Y)."\n";
  
// This code is contributed 
// by ChitraNayal
?>
输出:
3