📜  查找给定数字N的Landau函数

📅  最后修改于: 2021-05-04 19:36:49             🧑  作者: Mango

给定整数N ,任务是找到数字N的Landau函数。

例子:

方法:想法是使用递归为给定数N生成所有可能的分区,并在所有分区中找到LCM的最大值。考虑从1到N的每个整数,以便可以在每个递归调用中将总和N减少此数字,如果在任何递归调用中N都减少为零,则找到向量中存储的值的LCM。以下是递归的步骤:

  1. 获取必须将其总和分解为两个或更多个正整数的数字N。
  2. 从值1递归地迭代到N作为索引i
    • 基本情况:如果递归调用的值为0 ,则找到存储在当前向量中的值的LCM,因为这是将N分解为两个或多个正整数的方法之一。
      if (n == 0)
          findLCM(arr);
      
    • 递归调用:如果不满足基本条件,则从[i,N – i]递归迭代。将当前元素j推入vector(say arr )中,并递归地迭代下一个索引,在此递归结束后,弹出先前插入的元素j
      for j in range[i, N]:
          arr.push_back(j);
          recursive_function(arr, j + 1, N - j);
          arr.pop_back(j);
      
  3. 在所有递归调用之后,打印所有计算出的LCM的最大值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// To store Landau's function of the number
int Landau = INT_MIN;
  
// Function to return gcd of 2 numbers
int gcd(int a, int b)
{
  
    if (a == 0)
  
        return b;
  
    return gcd(b % a, a);
}
  
// Function to return LCM of two numbers
int lcm(int a, int b)
{
    return (a * b) / gcd(a, b);
}
  
// Function to find max lcm value
// among all representations of n
void findLCM(vector& arr)
{
    int nth_lcm = arr[0];
  
    for (int i = 1; i < arr.size(); i++)
  
        nth_lcm = lcm(nth_lcm, arr[i]);
  
    // Calculate Landau's value
    Landau = max(Landau, nth_lcm);
}
  
// Recursive function to find different
// ways in which n can be written as
// sum of atleast one positive integers
void findWays(vector& arr, int i, int n)
{
    // Check if sum becomes n,
    // consider this representation
    if (n == 0)
        findLCM(arr);
  
    // Start from previous element
    // in the representation till n
    for (int j = i; j <= n; j++) {
  
        // Include current element
        // from representation
        arr.push_back(j);
  
        // Call function again
        // with reduced sum
        findWays(arr, j, n - j);
  
        // Backtrack - remove current
        // element from representation
        arr.pop_back();
    }
}
  
// Function to find the Landau's function
void Landau_function(int n)
{
    vector arr;
  
    // Using recurrence find different
    // ways in which n can be written
    // as a sum of atleast one +ve integers
    findWays(arr, 1, n);
  
    // Print the result
    cout << Landau;
}
  
// Driver Code
int main()
{
    // Given N
    int N = 4;
  
    // Function Call
    Landau_function(N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
  
class GFG{
  
// To store Landau's function of the number
static int Landau = Integer.MIN_VALUE;
  
// Function to return gcd of 2 numbers
static int gcd(int a, int b)
{
    if (a == 0)
        return b;
  
    return gcd(b % a, a);
}
  
// Function to return LCM of two numbers
static int lcm(int a, int b)
{
    return (a * b) / gcd(a, b);
}
  
// Function to find max lcm value
// among all representations of n
static void findLCM(Vector arr)
{
    int nth_lcm = arr.get(0);
  
    for(int i = 1; i < arr.size(); i++)
        nth_lcm = lcm(nth_lcm, arr.get(i));
  
    // Calculate Landau's value
    Landau = Math.max(Landau, nth_lcm);
}
  
// Recursive function to find different
// ways in which n can be written as
// sum of atleast one positive integers
static void findWays(Vector arr, 
                     int i, int n)
{
      
    // Check if sum becomes n,
    // consider this representation
    if (n == 0)
        findLCM(arr);
  
    // Start from previous element
    // in the representation till n
    for(int j = i; j <= n; j++)
    {
          
        // Include current element
        // from representation
        arr.add(j);
  
        // Call function again
        // with reduced sum
        findWays(arr, j, n - j);
  
        // Backtrack - remove current
        // element from representation
        arr.remove(arr.size() - 1);
    }
}
  
// Function to find the Landau's function
static void Landau_function(int n)
{
    Vector arr = new Vector<>();
  
    // Using recurrence find different
    // ways in which n can be written
    // as a sum of atleast one +ve integers
    findWays(arr, 1, n);
  
    // Print the result
    System.out.print(Landau);
}
  
// Driver Code
public static void main(String[] args)
{
    // Given N
    int N = 4;
  
    // Function call
    Landau_function(N);
}
}
  
// This code is contributed by amal kumar choubey


Python3
# Python3 program for the above approach
import sys
  
# To store Landau's function of the number
Landau = -sys.maxsize - 1
  
# Function to return gcd of 2 numbers
def gcd(a, b):
  
    if (a == 0):
        return b
          
    return gcd(b % a, a)
  
# Function to return LCM of two numbers
def lcm(a, b):
      
    return (a * b) // gcd(a, b)
  
# Function to find max lcm value
# among all representations of n
def findLCM(arr):
  
    global Landau
  
    nth_lcm = arr[0]
  
    for i in range(1, len(arr)):
        nth_lcm = lcm(nth_lcm, arr[i])
  
    # Calculate Landau's value
    Landau = max(Landau, nth_lcm)
      
# Recursive function to find different
# ways in which n can be written as
# sum of atleast one positive integers
def findWays(arr, i, n):
  
    # Check if sum becomes n,
    # consider this representation
    if (n == 0):
        findLCM(arr)
  
    # Start from previous element
    # in the representation till n
    for j in range(i, n + 1):
  
        # Include current element
        # from representation
        arr.append(j)
  
        # Call function again
        # with reduced sum
        findWays(arr, j, n - j)
  
        # Backtrack - remove current
        # element from representation
        arr.pop()
      
# Function to find the Landau's function
def Landau_function(n):
  
    arr = []
  
    # Using recurrence find different
    # ways in which n can be written
    # as a sum of atleast one +ve integers
    findWays(arr, 1, n)
  
    # Print the result
    print(Landau)
  
# Driver Code
  
# Given N
N = 4
  
# Function call
Landau_function(N)
  
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
  
class GFG{
  
// To store Landau's function of the number
static int Landau = int.MinValue;
  
// Function to return gcd of 2 numbers
static int gcd(int a, int b)
{
    if (a == 0)
        return b;
  
    return gcd(b % a, a);
}
  
// Function to return LCM of two numbers
static int lcm(int a, int b)
{
    return (a * b) / gcd(a, b);
}
  
// Function to find max lcm value
// among all representations of n
static void findLCM(List arr)
{
    int nth_lcm = arr[0];
  
    for(int i = 1; i < arr.Count; i++)
        nth_lcm = lcm(nth_lcm, arr[i]);
  
    // Calculate Landau's value
    Landau = Math.Max(Landau, nth_lcm);
}
  
// Recursive function to find different
// ways in which n can be written as
// sum of atleast one positive integers
static void findWays(List arr, 
                     int i, int n)
{
      
    // Check if sum becomes n,
    // consider this representation
    if (n == 0)
        findLCM(arr);
  
    // Start from previous element
    // in the representation till n
    for(int j = i; j <= n; j++)
    {
          
        // Include current element
        // from representation
        arr.Add(j);
  
        // Call function again
        // with reduced sum
        findWays(arr, j, n - j);
  
        // Backtrack - remove current
        // element from representation
        arr.RemoveAt(arr.Count - 1);
    }
}
  
// Function to find the Landau's function
static void Landau_function(int n)
{
    List arr = new List();
  
    // Using recurrence find different
    // ways in which n can be written
    // as a sum of atleast one +ve integers
    findWays(arr, 1, n);
  
    // Print the result
    Console.Write(Landau);
}
  
// Driver Code
public static void Main(String[] args)
{
      
    // Given N
    int N = 4;
  
    // Function call
    Landau_function(N);
}
}
  
// This code is contributed by amal kumar choubey


输出:
4

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