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:
if (n == 0)
findLCM(arr);
for j in range[i, N]:
arr.push_back(j);
recursive_function(arr, j + 1, N - j);
arr.pop_back(j);
Below is the implementation of the above approach:
// 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;
}
#mathematical #recursion #lcm #function