Dado un número N , la tarea es encontrar la mayor potencia de 2 que divide a MCM de los primeros N números naturales.
Ejemplos:
Entrada: N = 5
Salida: 2
Explicación:
MCM de {1, 2, 3, 4, 5} = 60
60 es divisible por 2 2Entrada: N = 15
Salida: 3
Explicación:
MCM de {1, 2, 3…..14, 15} = 360360
360360 es divisible por 2 3
Enfoque ingenuo: la idea es encontrar el mínimo común múltiplo de los primeros N números naturales. Luego, itere un ciclo desde i = 1 y verifique si 2 i divide el LCM o no y mantenga la pista del máximo i que divide el LCM.
A continuación se muestra la implementación del enfoque anterior:
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>
3
Complejidad de tiempo: O(N)
Espacio Auxiliar: O(1)
Enfoque eficiente: El MCM de los primeros N números naturales siempre es divisible por una potencia de 2 y dado que el MCM de los primeros N números naturales contiene el producto 2 * 4 * 8 * 16 ……N. Por lo tanto, la mayor potencia de 2 que divide a MCM de los primeros N números naturales siempre será
A continuación se muestra la implementación del enfoque anterior:
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>
3
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)