For more information about concepts covered in this lesson, you can check out:
Other Important math Functions: gcd()
The first function we’ll talk about is
gcd(). This is short for the Greatest Common Divisor, and it takes a variable number of integers and it computes the greatest common divisor of those numbers. Now, usually, you want to compute the greatest common divisor of two integers, but
gcd() actually takes either one integer or zero integers, In the case where zero integers are passed, then it returns
0, and if you just pass in one integer, it’s going to return that same integer.
So just be aware of that in case you’re using a version of Python prior to 3.9. Next, we’ve got
fsum() takes an iterable as the argument, and it’s very similar to the
sum() function: It computes the sum of the numbers in the iterable, but we’ll see that it’s a little bit more accurate than the sum() function that’s built into Python. Then we’ve got
sqrt(), which is short for square root. this computes the square root of a number, and it has to be a positive number or
Then there are a couple of functions that convert from degrees to radians. The
degrees() function converts to degrees from radians, and the
radians() function converts to radians from degrees. In most computations with trigonometric functions, the inputs are interpreted as radians or are assumed to be radians, and so if you’re working with degrees, you’ll want to convert to radians.
And then we’ve got your favorite trig functions like cosine, sine, and tangent. These compute their values on the assumption that the input is in radians, and so, again, you may want to use the
radians() function to do that if you’re working initially with degrees.
And then you’ve got the inverses of the sine, cosine, and tangent functions. These are
acos()—that’s the arccosine of
asin()—that’s the inverse of sine, and then
atan()—that is the inverse or arctangent of
x. Now, I’ll mention this right here that the
asin(), they have domain limitations on the input.
Let’s start off with
gcd() function will compute the greatest common divisor of an arbitrary number of integers. If you don’t pass any integers, you’ll get zero. If you pass in one integer,
then you just get the integers back. And if you pass in two, then this is what you may have learned about the
gcd(), is that it finds the greatest common divisor of two integers. In this case, you get
Then you get
8. So this is kind of cool because if you have a list of integers and you want to find the greatest common divisor of all of the numbers in the list, then you can use the
And if you’re familiar with the unpacking operator (
*) in Python, then you can use that instead of having to do, say, some sort of recursion or a reducing type of operation. Let me show you what I mean.
03:50 Here’s a list of integers, and if you wanted to find the greatest common divisor of these numbers, you could just put these in one by one… Oh, this is kind of slow! Hey, why don’t we use the unpacking operator?
04:04 So, if you’re not familiar with it, there will be a link underneath the video description there for you to read up. But basically, if you have an iterable and you want to use those iterable elements, the elements in the iterable, as arguments to a function that takes on an arbitrary number or however many number of elements in the iterable, then you can use this star or unpacking operator.
Become a Member to join the conversation.