Highest power of 2 that divides the LCM of first N Natural numbers.
Last Updated :
17 Nov, 2021
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++
#include <bits/stdc++.h>
using namespace std;
int findlcm( int n)
{
int ans = 1;
for ( int i = 1; i <= n; i++)
ans = (((i * ans)) / (__gcd(i, ans)));
return ans;
}
int highestPower( int n)
{
int lcm = findlcm(n);
int ans = 0;
for ( int i = 1;; i++) {
int x = pow (2, i);
if (lcm % x == 0) {
ans = i;
}
if (x > n)
break ;
}
return ans;
}
int main()
{
int n = 15;
cout << highestPower(n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findlcm( int n)
{
int ans = 1 ;
for ( int i = 1 ; i <= n; i++)
ans = (((i * ans)) / (__gcd(i, ans)));
return ans;
}
static int highestPower( int n)
{
int lcm = findlcm(n);
int ans = 0 ;
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);
}
public static void main(String[] args)
{
int n = 15 ;
System.out.print(highestPower(n));
}
}
|
Python3
def findlcm(n):
ans = 1 ;
for i in range ( 1 , n + 1 ):
ans = (((i * ans)) / /
(__gcd(i, ans)));
return ans;
def highestPower(n):
lcm = findlcm(n);
ans = 0 ;
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);
if __name__ = = '__main__' :
n = 15 ;
print (highestPower(n));
|
C#
using System;
class GFG{
static int findlcm( int n)
{
int ans = 1;
for ( int i = 1; i <= n; i++)
ans = (((i * ans)) /
(__gcd(i, ans)));
return ans;
}
static int highestPower( int n)
{
int lcm = findlcm(n);
int ans = 0;
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);
}
public static void Main(String[] args)
{
int n = 15;
Console.Write(highestPower(n));
}
}
|
Javascript
<script>
function findlcm(n)
{
let ans = 1;
for (let i = 1; i <= n; i++)
ans = (((i * ans)) / (__gcd(i, ans)));
return ans;
}
function highestPower(n)
{
let lcm = findlcm(n);
let ans = 0;
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);
}
let n = 15;
document.write(highestPower(n));
</script>
|
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
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int highestPower( int n)
{
return log (n) / log (2);
}
int main()
{
int n = 15;
cout << highestPower(n);
return 0;
}
|
Java
class GFG{
static int highestPower( int n)
{
return ( int )(Math.log(n) / Math.log( 2 ));
}
public static void main(String[] args)
{
int n = 15 ;
System.out.println(highestPower(n));
}
}
|
Python3
import math
def highestPower(n):
return int ((math.log(n) / / math.log( 2 )));
if __name__ = = '__main__' :
n = 15 ;
print (highestPower(n));
|
C#
using System;
class GFG{
static int highestPower( int n)
{
return ( int )(Math.Log(n) / Math.Log(2));
}
public static void Main(String[] args)
{
int n = 15;
Console.WriteLine(highestPower(n));
}
}
|
Javascript
<script>
function highestPower(n)
{
return parseInt(Math.log(n) / Math.log(2));
}
var n = 15;
document.write( highestPower(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...