Encontrar LCM usando GCD se explica aquí, pero aquí la tarea es encontrar LCM sin calcular primero GCD.
Ejemplos:
Input: 7, 5 Output: 35 Input: 2, 6 Output: 6
El enfoque es comenzar con el mayor de los 2 números y seguir incrementando el número mayor por sí mismo hasta que el número menor divida perfectamente la resultante.
C++
// C++ program to find LCM of 2 numbers // without using GCD #include <bits/stdc++.h> using namespace std; // Function to return LCM of two numbers int findLCM(int a, int b) { int lar = max(a, b); int small = min(a, b); for (int i = lar; ; i += lar) { if (i % small == 0) return i; } } // Driver program to test above function int main() { int a = 5, b = 7; cout << "LCM of " << a << " and " << b << " is " << findLCM(a, b); return 0; }
Java
// Java program to find LCM of 2 numbers // without using GCD import java.io.*; import java.lang.*; class GfG { // Function to return LCM of two numbers public static int findLCM(int a, int b) { int lar = Math.max(a, b); int small = Math.min(a, b); for (int i = lar; ; i += lar) { if (i % small == 0) return i; } } // Driver program to test above function public static void main(String [] argc) { int a = 5, b = 7; System.out.println( "LCM of " + a + " and " + b + " is " + findLCM(a, b)); } } // This dose is contributed by Sagar Shukla.
Python 3
# Python 3 program to find # LCM of 2 numbers without # using GCD import sys # Function to return # LCM of two numbers def findLCM(a, b): lar = max(a, b) small = min(a, b) i = lar while(1) : if (i % small == 0): return i i += lar # Driver Code a = 5 b = 7 print("LCM of " , a , " and ", b , " is " , findLCM(a, b), sep = "") # This code is contributed # by Smitha
C#
// C# program to find // LCM of 2 numbers // without using GCD using System; class GfG { // Function to return // LCM of two numbers public static int findLCM(int a, int b) { int lar = Math.Max(a, b); int small = Math.Min(a, b); for (int i = lar; ; i += lar) { if (i % small == 0) return i; } } // Driver Code public static void Main() { int a = 5, b = 7; Console.WriteLine("LCM of " + a + " and " + b + " is " + findLCM(a, b)); } } // This code is contributed by anuj_67.
PHP
<?php // PHP program to find // LCM of 2 numbers // without using GCD // Function to return // LCM of two numbers function findLCM($a, $b) { $lar = max($a, $b); $small = min($a, $b); for ($i = $lar; ; $i += $lar) { if ($i % $small == 0) return $i; } } // Driver Code $a = 5; $b = 7; echo "LCM of " , $a , " and ", $b , " is " , findLCM($a, $b); // This code is contributed // by Smitha ?>
Javascript
<script> // javascript program to find LCM of 2 numbers // without using GCD // Function to return LCM of two numbers function findLCM(a , b) { var lar = Math.max(a, b); var small = Math.min(a, b); for (i = lar;; i += lar) { if (i % small == 0) return i; } } // Driver program to test above function var a = 5, b = 7; document.write("LCM of " + a + " and " + b + " is " + findLCM(a, b)); // This code contributed by umadevi9616 </script>
Producción:
LCM of 5 and 7 is 35
Complejidad del tiempo: O(min(a, b))
Espacio Auxiliar: O(1)