Python sympy.gcd() Method

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.gcd() Method:

We find the greatest common divisor of two numbers that are passed as arguments to the sympy.gcd() method.

Syntax:

  sympy.gcd(num1, num2)

Return Value:

The greatest common divisor value is returned by the gcd() function.

sympy.gcd() Method in Python

Method #1: Using gcd() 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 gcd() function to get the greatest common divisor(gcd) value for the given two numbers.
  • Store it in another variable.
  • Print the GCD 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 = 20
# Give the second number as static input and store it in another variable.
gvn_num2 = 3
# Pass the above given two numbers as arguments to the gcd() function to get the 
# greatest common divisor(gcd) value for the given two numbers.
# Store it in another variable.
rslt = gcd(gvn_num1, gvn_num2)

# Print the GCD of the given two numbers
print("The GCD of the given two numbers {",gvn_num1,",",gvn_num2,"} = ", rslt)

Output:

The GCD of the given two numbers { 20 , 3 } = 1

Method #2: Using gcd() 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 gcd() function to get the greatest common divisor(gcd) value for the given two numbers.
  • Store it in another variable.
  • Print the GCD 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 gcd() function to get the 
# greatest common divisor(gcd) value for the given two numbers.
# Store it in another variable.
rslt = gcd(gvn_num1, gvn_num2)

# Print the GCD of the given two numbers
print("The GCD of the given two numbers {",gvn_num1,",",gvn_num2,"} = ", rslt)

Output:

Enter some random number = 60
Enter some random number = 4
The GCD of the given two numbers { 60 , 4 } = 4