Python SymPy Module:
SymPy is a Python symbolic mathematics library. It aims to be a full-featured computer algebra system (CAS) while keeping the code as basic(simple) as possible in order to be understandable and easily expandable. SymPy is entirely written in Python. SymPy is simple to use because it only depends on mpmath, a pure Python library for arbitrary floating-point arithmetic.
Rational and Integer are the numerical types defined by SymPy. A rational number is represented by the Rational class as a pair of two Integers, numerator and denominator, therefore Rational(1, 2) is 1/2, Rational(3, 2) is 3/2, and so on. Integer numbers are represented by the Integer class.
SymPy uses mpmath in the background, allowing it to execute arbitrary-precision arithmetic computations. Some special constants, such as exp, pi, and oo (Infinity), are thus considered as symbols and can be evaluated with arbitrary precision.
Installation:
pip install sympy
Python sympy.lcm() Method:
We find the least common multiple(lcm) of two numbers that are passed as arguments to the sympy.lcm() method.
Syntax:
sympy.lcm(num1, num2)
Return Value:
The least common multiple value is returned by the lcm() function.
sympy.lcm() Method in Python
Method #1: Using lcm() Function (Static Input)
Approach:
- Import all the functions from sympy module using the import keyword
- Give the first number as static input and store it in a variable.
- Give the second number as static input and store it in another variable.
- Pass the above given two numbers as arguments to the lcm() function to get the least common multiple(lcm) value for the given two numbers.
- Store it in another variable.
- Print the LCM of the given two numbers.
- The Exit of the Program.
Below is the implementation:
# Import all the functions from sympy module using the import keyword from sympy import * # Give the first number as static input and store it in a variable. gvn_num1 = 10 # Give the second number as static input and store it in another variable. gvn_num2 = 6 # Pass the above given two numbers as arguments to the lcm() function to get the # least common multiple(lcm) value for the given two numbers. # Store it in another variable. rslt = lcm(gvn_num1, gvn_num2) # Print the LCM of the given two numbers print("The LCM of the given two numbers {",gvn_num1,",",gvn_num2,"} = ", rslt)
Output:
The LCM of the given two numbers { 10 , 6 } = 30
Method #2: Using lcm() Function (User Input)
Approach:
- Import all the functions from sympy module using the import keyword
- Give the first number as user input using the int(input()) function and store it in a variable.
- Give the second number as user input using the int(input()) function and store it in another variable.
- Pass the above given two numbers as arguments to the lcm() function to get the least common multiple(lcm) value for the given two numbers.
- Store it in another variable.
- Print the LCM of the given two numbers.
- The Exit of the Program.
Below is the implementation:
# Import all the functions from sympy module using the import keyword from sympy import * # Give the first number as user input using the int(input()) function # and store it in a variable. gvn_num1 = int(input("Enter some random number = ")) # Give the second number as user input using the int(input()) function # and store it in another variable. gvn_num2 = int(input("Enter some random number = ")) # Pass the above given two numbers as arguments to the lcm() function to get the # least common multiple(lcm) value for the given two numbers. # Store it in another variable. rslt = lcm(gvn_num1, gvn_num2) # Print the LCM of the given two numbers print("The LCM of the given two numbers {",gvn_num1,",",gvn_num2,"} = ", rslt)
Output:
Enter some random number = 5 Enter some random number = 7 The LCM of the given two numbers { 5 , 7 } = 35