

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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...\n", np.lcm(15, 30)) print("LCM...\n", np.lcm(10, 50)) print("LCM...\n", np.lcm.reduce([6, 18, 30]))
To return the lowest common multiple, use the numpy.lcm() method in Python Numpy −
print("GCD...\n", np.gcd(15, 30)) print("GCD...\n", np.gcd(10, 50)) print("GCD...\n", 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...\n", np.lcm(15, 30)) print("LCM...\n", np.lcm(10, 50)) print("LCM...\n", np.lcm.reduce([6, 18, 30])) # To returns the lowest common multiple, use the numpy.lcm() method in Python Numpy print("GCD...\n", np.gcd(15, 30)) print("GCD...\n", np.gcd(10, 50)) print("GCD...\n", np.gcd.reduce([25, 75, 100, 125]))
Output
LCM... 30 LCM... 50 LCM... 90 GCD... 15 GCD... 10 GCD... 25
- Related Questions & Answers
- Greatest Common Divisor of Strings in Python
- Greatest common divisors in Python
- Return the common filling value of two masked arrays in Numpy
- Lowest Common Ancestor of Deepest Leaves in Python
- Comparing objects in JavaScript and return array of common keys having common values
- C program to find Highest Common Factor (HCF) and Least Common Multiple (LCM)
- Lowest Common Ancestor of a Binary Tree in Python
- Lowest Common Ancestor of a Binary Search Tree in Python
- Return the remainder of division with dividend and divisor array-like in Numpy
- C++ Program to Find Lowest Common Ancestor in a Binary Search Tree
- Function to check two strings and return common words in JavaScript
- How to find the common elements in multiple vectors in R?
- Program to find out the lowest common ancestor of a binary tree using Python
- How to share common data among multiple Python files?
- C++ Queries on XOR of Greatest Odd Divisor of the Range