- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return the greatest common divisor and lowest common multiple in Numpy
To return the greatest common divisor, use the numpy.gcd() method in Python Numpy. The parameters are arrays of values. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).
To return the lowest common multiple, use the numpy.lcm() method in Python Numpy. The greatest common divisor of the absolute value of the inputs This is a scalar if both x1 and x2 are scalars.
Steps
At first, import the required library −
import numpy as np
To return the greatest common divisor, use the numpy.gcd() method in Python Numpy. The parameters are arrays of values. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output) −
print("LCM...
", np.lcm(15, 30)) print("LCM...
", np.lcm(10, 50)) print("LCM...
", np.lcm.reduce([6, 18, 30]))
To return the lowest common multiple, use the numpy.lcm() method in Python Numpy −
print("GCD...
", np.gcd(15, 30)) print("GCD...
", np.gcd(10, 50)) print("GCD...
", np.gcd.reduce([25, 75, 100, 125]))
Example
import numpy as np # To returns the greatest common divisor, use the numpy.gcd() method in Python Numpy # The parameters are arrays of values. # If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output). print("LCM...
", np.lcm(15, 30)) print("LCM...
", np.lcm(10, 50)) print("LCM...
", np.lcm.reduce([6, 18, 30])) # To returns the lowest common multiple, use the numpy.lcm() method in Python Numpy print("GCD...
", np.gcd(15, 30)) print("GCD...
", np.gcd(10, 50)) print("GCD...
", np.gcd.reduce([25, 75, 100, 125]))
Output
LCM... 30 LCM... 50 LCM... 90 GCD... 15 GCD... 10 GCD... 25
- Related Articles
- Greatest Common Divisor of Strings in Python
- Find the lowest common denominator or greatest common factor in Excel
- Greatest common divisors in Python
- Return the common filling value of two masked arrays in Numpy
- Return the remainder of division with dividend and divisor array-like in Numpy
- Lowest Common Ancestor of Deepest Leaves in Python
- Comparing objects in JavaScript and return array of common keys having common values
- Lowest Common Ancestor of a Binary Tree in Python
- C program to find Highest Common Factor (HCF) and Least Common Multiple (LCM)
- Lowest Common Ancestor of a Binary Search Tree in Python
- How To Search Highest Common Multiple
- C++ Program to Find Lowest Common Ancestor in a Binary Search Tree
- Function to check two strings and return common words in JavaScript
- What are common factors and common multiples?
- How to find the common elements in multiple vectors in R?
