Skip to main content

HDF and LCM in python

 

HCF of Two Numbers

Here, in this section, we will discuss how to find the HCF of two numbers in python. HCF means (Highest Common Factor) also known as GCD (Greatest Common Divisor)

a=36
b=24

lis=[]
for i in range(1,min(a,b)+1):
    if a%i==0 and b%i==0:
        lis.append(i)
print(max(lis))

LCM of two numbers in Python

Basically the LCM of two numbers is the smallest number which can divide the both numbers equally. This is also called Least Common Divisor or LCD
a=6
b=4

lis=[]
for i in range(1,min(a,b)+1):
    if a%i==0 and b%i==0:
        lis.append(i)
print(a*b//max(lis))

Multiples of 4 are: 4,8,12,16,20,24…

Multiples of 6 are: 6,12,18,24….

The common multiples for 4 and 6 are 12,24,36,48…and so on. The least common multiple in that lot would be 12. Let us now try to find out the LCM of 24 and 15.


Comments

Popular posts from this blog

Magic constant generator -python3/hacker rank solution / tcs fresco play

  def   generator_Magic ( n1 ):      # the value starts from 3 and m is formula for constant,      #for generator  yield should use      for   a   in   range ( 3 , n1 + 1 ):          m = a * ( a ** 2 + 1 ) / 2          yield   m               # Write your code here if   __name__  ==  '__main__' :

Getting Started with NumPy: A Comprehensive Guide

  NumPy is a fundamental library for scientific computing with Python. It provides support for large, multi-dimensional arrays and matrices, along with mathematical functions to operate on these arrays. This tutorial will introduce you to the basics of NumPy and its core functionalities. Installing NumPy Before diving into NumPy, make sure you have it installed. You can install NumPy using the following command: pip install numpy pip install numpy Importing NumPy Once installed, you can import NumPy in your Python script or Jupyter notebook as follows: import numpy as np Now, let's explore some of the essential features of NumPy. NumPy Arrays NumPy's primary data structure is the array. An array is a grid of values, and it can be one-dimensional or multi-dimensional. Here's how you can create a simple one-dimensional array: import numpy as np # Creating a one-dimensional array arr1 = np.array([1, 2, 3, 4, 5]) print(arr1) For multi-dimensional arrays, you can use nested ...