Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

gcd and lcm in c++

long long gcd(long long int a, long long int b)
{
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
long long lcm(ll int a, ll int b)
{
    return (a / gcd(a, b)) * b;
}
// there is a math formula used in this code which you can search up about,
// but you can just use it as it is.
Comment

find lcm with gcd

import java.util.*;

public class LCM {
  
  public static long lcm(int a, int b) {

    long x = (a/euclidGcd(a, b));

    return x * b;

  }

  public static int euclidGcd(int a, int b) {

    if (b == 0)
      return a;

    int aprime = a%b;
    
    return euclidGcd(b, aprime);
  }
  
  public static void main(String args[]) {
    Scanner scanner = new Scanner(System.in);
    int a = scanner.nextInt();
    int b = scanner.nextInt();

    System.out.println(lcm(a, b));
  }
}
Comment

Program to Compute LCM Using GCD

# Python program to find the L.C.M. of two input number

# This function computes GCD 
def compute_gcd(x, y):

   while(y):
       x, y = y, x % y
   return x

# This function computes LCM
def compute_lcm(x, y):
   lcm = (x*y)//compute_gcd(x,y)
   return lcm

num1 = 54
num2 = 24 

print("The L.C.M. is", compute_lcm(num1, num2))
Comment

gcd and lcm of an array

//We write the function to find the GCD of two numbers.

//We consider the first element of the array to be the gcd of itself and iterate through 
//each element of the array and update gcd with GCD of current element and previous gcd.

//We then find the LCM using the identity 
//           LCM*GCD = Multiplication of each number in the array
using System;
					
public class Program
{
	public static void Main()
	{
		string[] input = Console.ReadLine().Split(' ');
		int[] arr = Array.ConvertAll(input, int.Parse);
		int[] res = GCDandLCM(arr);
		Console.WriteLine("GCD is: {0}", res[0]);
		Console.WriteLine("LCM is: {0}", res[1]);
	}
	//GCD and LCM of numbers given in an array
	public static int[] GCDandLCM(int[] arr)
	{
		int gcd = arr[0];
		for(int i=1; i<arr.Length; i++)
		{
			gcd = GCD(gcd, arr[i]);
		}
		int lcm = Multiply(arr)/gcd;
		return new int[] {gcd, lcm};
	}
    //GCD using repetitive subtraction method
	public static int GCD(int a, int b)
	{
		while(a!=b)
		{
			if(a>b) a=a-b;
			else b=b-a;
		}
		return a;
	}
	public static int Multiply(int[] arr)
	{
		int mult = 1;
		for(int i=0; i<arr.Length; i++)
		{
			mult*=arr[i];
		}
		return mult;
	}
}
Comment

PREVIOUS NEXT
Code Example
Python :: simple bmi calculator using python 
Python :: django 3.2 compatible to python 3.10? 
Python :: cross validation sklearn 
Python :: reading from a text file 
Python :: describe in python 
Python :: Set .difference() Operation in python3 
Python :: Lambda Functions using for loop 
Python :: can we use else without if in python 
Python :: duplicate remove 
Python :: pandas df mode 
Python :: try except in list comprehension 
Python :: local variable referenced before assignment 
Python :: strip function in python 
Python :: how to remove outliers in dataset in python 
Python :: date and time using tkinter 
Python :: pybase64 tutorial 
Python :: iloc[:,0:-1] 
Python :: string pythhon 
Python :: for _ in range() in python 
Python :: cudart64_110.dll not found 
Python :: sub function python 
Python :: python double underscore methods 
Python :: python csv to excel 
Python :: google map distance 
Python :: range(n,n) python 
Python :: full body tracking module 
Python :: Patch loop runner _run_once 
Python :: doormat pattern 
Python :: convert only time to unix timestamp python 
Python :: flask int route 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =