📜  2的最高幂,它除以前N个自然数的LCM。

📅  最后修改于: 2021-05-06 23:39:26             🧑  作者: Mango

给定数字N ,任务是找到2的最大幂,该幂将LCM除以前N个自然数。

例子:

天真的方法:这个想法是找到前N个自然数的最小公倍数。然后迭代从i循环= 1,并检查是否2 I除以LCM或不和保持最大的I划分LCM的轨道。

下面是上述方法的实现:

C++
// C++ implementation of the approach
  
#include 
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


C++
// C++ implementation of the approach
  
#include 
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


输出:
3

有效的方法:前N个自然数的LCM总是可以被2的幂整除,并且因为前N个自然数的LCM包含乘积2 * 4 * 8 * 16…N。因此,将前N个自然数的LCM除以2的最大幂将始终为
\lfloor \log_2 N \rfloor

下面是上述方法的实现:

C++

// C++ implementation of the approach
  
#include 
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 
输出:
3