Skip to content
Related Articles

Related Articles

Find Landau’s function for a given number N
  • Last Updated : 13 Aug, 2020

Given an integer N, the task is to find the Landau’s Function of the number N.

In number theory, The Landau’s function finds the largest LCM among all partitions of the given number N.

For Example: If N = 4, then possible partitions are:
1. {1, 1, 1, 1}, LCM = 1
2. {1, 1, 2}, LCM = 2
3. {2, 2}, LCM = 2
4. {1, 3}, LCM = 3

Among the above partitions, the partitions whose LCM is maximum is {1, 3} as LCM  = 3.

Examples:



Input: N = 4
Output: 4
Explanation:
Partitions of 4 are [1, 1, 1, 1], [1, 1, 2], [2, 2], [1, 3], [4] among which maximum LCM is of the last partition 4 whose LCM is also 4.

Input: N = 7
Output: 12
Explanation:
For N = 7 the maximum LCM is 12.

Approach: The idea is to use Recursion to generate all possible partitions for the given number N and find the maximum value of LCM among all the partitions. Consider every integer from 1 to N such that the sum N can be reduced by this number at each recursive call and if at any recursive call N reduces to zero then find the LCM of the value stored in the vector. Below are the steps for recursion:

  1. Get the number N whose sum has to be broken into two or more positive integers.
  2. Recursively iterate from value 1 to N as index i:
    • Base Case: If the value called recursively is 0, then find the LCM of the value stored in the current vector as this is the one of the way to broke N into two or more positive integers. 
      if (n == 0)
          findLCM(arr);
      
    • Recursive Call: If the base case is not met, then Recursively iterate from [i, N – i]. Push the current element j into vector(say arr) and recursively iterate for the next index and after the this recursion ends then pop the element j inserted previously: 
      for j in range[i, N]:
          arr.push_back(j);
          recursive_function(arr, j + 1, N - j);
          arr.pop_back(j);
      
  3. After all the recursive call, print the maximum of all the LCM calculated.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
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<int>& 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<int>& 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<int> 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<Integer> 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<Integer> 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<Integer> 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<int> 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<int> 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<int> arr = new List<int>();
  
    // 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

chevron_right


Output: 

4

Time Complexity: O(2N
Auxiliary Space: O(N2
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :