Sunday, January 12, 2025
Google search engine
HomeData Modelling & AIHighest power of 2 that divides the LCM of first N Natural...

Highest power of 2 that divides the LCM of first N Natural numbers.

Given a number N, the task is to find the largest power of 2 that divides LCM of first N Natural numbers.

Examples:

Input: N = 5
Output: 2
Explanation:
LCM of {1, 2, 3, 4, 5} = 60
60 is divisible by 22

Input: N = 15 
Output: 3
Explanation:
LCM of {1, 2, 3…..14, 15} = 360360
360360 is divisible by 23

 

Naive Approach: The idea is to find the Least common multiple of first N natural numbers. Then iterate a loop from i = 1 and check if 2i Divides the LCM or not and keep the track of maximum i that divides LCM.

Below is the implementation of the above approach:

C++




// C++ implementation of the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find LCM of
// first N natural numbers
int findlcm(int n)
{
    // Initialize result
    int ans = 1;
 
    // Ans contains LCM of 1, 2, 3, ..i
    // after i'th iteration
    for (int i = 1; i <= n; i++)
        ans = (((i * ans)) / (__gcd(i, ans)));
    return ans;
}
 
// Function to find the
// highest power of 2
// which divides LCM of
// first n natural numbers
int highestPower(int n)
{
    // Find lcm of first
    // N natural numbers
    int lcm = findlcm(n);
 
    // To store the highest
    // required power of 2
    int ans = 0;
 
    // Counting number of consecutive zeros
    // from the end in the given binary string
    for (int i = 1;; i++) {
        int x = pow(2, i);
        if (lcm % x == 0) {
            ans = i;
        }
        if (x > n)
            break;
    }
    return ans;
}
 
// Driver code
int main()
{
    int n = 15;
    cout << highestPower(n);
    return 0;
}


Java




// Java implementation of the approach
import java.util.*;
 
class GFG{
 
// Function to find LCM of
// first N natural numbers
static int findlcm(int n)
{
     
    // Initialize result
    int ans = 1;
 
    // Ans contains LCM of 1, 2, 3, ..i
    // after i'th iteration
    for(int i = 1; i <= n; i++)
        ans = (((i * ans)) / (__gcd(i, ans)));
         
    return ans;
}
 
// Function to find the
// highest power of 2
// which divides LCM of
// first n natural numbers
static int highestPower(int n)
{
     
    // Find lcm of first
    // N natural numbers
    int lcm = findlcm(n);
 
    // To store the highest
    // required power of 2
    int ans = 0;
 
    // Counting number of consecutive zeros
    // from the end in the given binary String
    for(int i = 1;; i++)
    {
        int x = (int) Math.pow(2, i);
        if (lcm % x == 0)
        {
            ans = i;
        }
        if (x > n)
            break;
    }
    return ans;
}
 
static int __gcd(int a, int b)
{
    return b == 0 ? a : __gcd(b, a % b);    
}
 
// Driver code
public static void main(String[] args)
{
    int n = 15;
     
    System.out.print(highestPower(n));
}
}
 
// This code is contributed by 29AjayKumar


Python3




# Python3 implementation of the approach
 
# Function to find LCM of
# first N natural numbers
def findlcm(n):
     
    # Initialize result
    ans = 1;
 
    # Ans contains LCM of 1, 2, 3, ..i
    # after i'th iteration
    for i in range(1, n + 1):
        ans = (((i * ans)) //
          (__gcd(i, ans)));
 
    return ans;
 
# Function to find the highest power
# of 2 which divides LCM of first n
# natural numbers
def highestPower(n):
     
    # Find lcm of first
    # N natural numbers
    lcm = findlcm(n);
 
    # To store the highest
    # required power of 2
    ans = 0;
 
    # Counting number of consecutive zeros
    # from the end in the given binary String
    for i in range(1, n):
        x = int(pow(2, i));
         
        if (lcm % x == 0):
            ans = i;
        if (x > n):
            break;
 
    return ans;
 
def __gcd(a, b):
     
    if (b == 0):
        return a;
    else:
        return __gcd(b, a % b);
 
# Driver code
if __name__ == '__main__':
     
    n = 15;
 
    print(highestPower(n));
 
# This code is contributed by 29AjayKumar


C#




// C# implementation of the approach
using System;
class GFG{
 
// Function to find LCM of
// first N natural numbers
static int findlcm(int n)
{   
    // Initialize result
    int ans = 1;
 
    // Ans contains LCM of 1, 2, 3, ..i
    // after i'th iteration
    for(int i = 1; i <= n; i++)
        ans = (((i * ans)) /
               (__gcd(i, ans)));
         
    return ans;
}
 
// Function to find the
// highest power of 2
// which divides LCM of
// first n natural numbers
static int highestPower(int n)
{   
    // Find lcm of first
    // N natural numbers
    int lcm = findlcm(n);
 
    // To store the highest
    // required power of 2
    int ans = 0;
 
    // Counting number of consecutive zeros
    // from the end in the given binary String
    for(int i = 1;; i++)
    {
        int x = (int) Math.Pow(2, i);
        if (lcm % x == 0)
        {
            ans = i;
        }
        if (x > n)
            break;
    }
    return ans;
}
 
static int __gcd(int a, int b)
{
    return b == 0 ? a : __gcd(b, a % b);    
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 15;   
    Console.Write(highestPower(n));
}
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
// JavaScript program for the
// above approach
 
// Function to find LCM of
// first N natural numbers
function findlcm(n)
{
      
    // Initialize result
    let ans = 1;
  
    // Ans contains LCM of 1, 2, 3, ..i
    // after i'th iteration
    for(let i = 1; i <= n; i++)
        ans = (((i * ans)) / (__gcd(i, ans)));
          
    return ans;
}
  
// Function to find the
// highest power of 2
// which divides LCM of
// first n natural numbers
function highestPower(n)
{
      
    // Find lcm of first
    // N natural numbers
    let lcm = findlcm(n);
  
    // To store the highest
    // required power of 2
    let ans = 0;
  
    // Counting number of consecutive zeros
    // from the end in the given binary String
    for(let i = 1;; i++)
    {
        let x =  Math.pow(2, i);
        if (lcm % x == 0)
        {
            ans = i;
        }
        if (x > n)
            break;
    }
    return ans;
}
  
function __gcd(a, b)
{
    return b == 0 ? a : __gcd(b, a % b);   
}
 
// Driver Code
 
    let n = 15;
      
    document.write(highestPower(n));
 
</script>


Output

3

Time Complexity: O(N)

Auxiliary Space: O(1)

Efficient Approach: The LCM of first N natural numbers is always divisible by a power of 2 and since the LCM of first N natural numbers contains the product 2 * 4 * 8 * 16 ……N. Therefore, the largest power of 2 that divides LCM of first N Natural numbers will always be 
\lfloor \log_2 N \rfloor

Below is the implementation of the above approach:

C++




// C++ implementation of the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the
// highest power of 2
// which divides LCM of
// first n natural numbers
int highestPower(int n)
{
    return log(n) / log(2);
}
 
// Driver code
int main()
{
    int n = 15;
    cout << highestPower(n);
    return 0;
}


Java




// Java implementation of the approach
class GFG{
     
// Function to find the highest
// power of 2 which divides LCM of
// first n natural numbers
static int highestPower(int n)
{
    return (int)(Math.log(n) / Math.log(2));
}
 
// Driver code
public static void main(String[] args)
{
    int n = 15;
    System.out.println(highestPower(n));
}
}
 
// This code is contributed by dewantipandeydp


Python3




# Python3 implementation of the approach
import math
 
# Function to find the highest
# power of 2 which divides LCM of
# first n natural numbers
def highestPower(n):
     
    return int((math.log(n) // math.log(2)));
 
# Driver code
if __name__ == '__main__':
     
    n = 15;
    print(highestPower(n));
 
# This code is contributed by Rajput-Ji


C#




// C# implementation of the approach
using System;
 
class GFG{
     
// Function to find the highest
// power of 2 which divides LCM of
// first n natural numbers
static int highestPower(int n)
{
    return (int)(Math.Log(n) / Math.Log(2));
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 15;
     
    Console.WriteLine(highestPower(n));
}
}
 
// This code is contributed by sapnasingh4991


Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to find the
// highest power of 2
// which divides LCM of
// first n natural numbers
function highestPower(n)
{
    return parseInt(Math.log(n) / Math.log(2));
}
 
// Driver code
var n = 15;
document.write( highestPower(n));
 
 
</script>


Output

3

Time Complexity: O(1)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Last Updated :
17 Nov, 2021
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments