GCD and LCM & New HTTP Status Codes
Here are resources for more information about GCD, LCM, and HTTP status codes:
- Greatest Common Divisor (GCD): Wikipedia article
- Least Common Multiple (LCM): Wikipedia article
- HTTP Status Code Registry
- 103 (Early Hints)
- 425 (Too Early)
- Hyper Text Coffee Pot Control Protocol (HTCPCP)
- 418 (I’m a Teapot)
- Save 418 Movement: Wikipedia article
- Status 418 I’m a teapot: google.com/teapot
In the previous lesson, I showed you the prefix and suffix removal functions for the
str (string) library and the new
graphlib topological graph sorting feature. In this lesson, I’ll be talking about changes to GCD, and the addition of LCM to the
math library, and new HTTP status codes. Before I get going here, I want to warn you a little bit.
00:22 I had a horrendous experience in the eighth grade covering GCD and LCM. It went on and on forever, and the teacher was horrific, so if you hear a little tremble in my voice as I go through this, it’s PTSD from middle school. That being said, what’s a GCD? That’s the Greatest Common Divisor, or the largest number that divides two other numbers. For example, the GCD of 49 and 14 is 7.
00:51 7 * 2 = 14, 7 * 7 = 49, and no number larger than 7 goes into both 14 and 49. A common companion to GCD is LCM, or Least Common Multiple. This is the smallest number that can be divided by two other numbers.
01:11 For example, the LCM of 49 and 14 is 98. 98 is 49 * 2, and 98 is 14 * 7. No number smaller than 98 can be divided by both 49 and 14 evenly. GCD and LCM are both very common functions in cryptography.
math library in Python has a
gcd() function, but it didn’t have an
lcm() function. The
gcd() function in Python 3.9 is now capable of doing GCD of multiple numbers, and the
lcm() function has been added to the library.
Let me demonstrate these two functions to you. You need to import them from the
math library. Using the same numbers as before, calling
14, you get the expected result of
14, you get
98. Prior to Python 3.9, if you wanted to do the GCD of more than two numbers, you had to combine things.
reduce() calls a function—in this case,
gcd()—on multiple numbers. It takes the GCD of
1729, computes the result, and then does the
gcd() of that result and the next number in the list—in this case,
When you call a web page on a web server, the content is returned back with some metadata. One of the pieces of that metadata is a response code. The collection of possible HTTP response codes is stored in the
HTTPStatus object in the
http library. In keeping up with this standard, some new codes have been added to Python 3.9.
103 Early Hints is a response header indicating that headers are coming before the final message. This allows a web server to send information ahead of time, reducing the amount of time that a user has to wait for the first amount of information to show up on the screen.
The ability for the browser to ask for early response headers introduces a possible replay attack, particularly during TLS handshake. In order to get around this, web servers also have the ability to tell a client to wait. The status code
425 Too Early is the server indicating to the browser that it’s asked for
Early Hints too early, and it has to wait a little longer.
There’s its value,
103, and you can also ask by number, using the callable on the object instead. Once you have the
HTTPStatus object, you can use the number or the keyword to get at any of the response statuses, as well as their values and their English-language descriptions. One more status was added, but it needs a little bit of an explanation first.
A few years back as part of an April Fools’ joke, the IETF introduced RFC 2324—that’s the Hyper Text Coffee Pot Control Protocol. This protocol added new HTTP methods like
BREW to control your coffee pot, and otherwise mostly just reused the existing status codes—with one exception.
If you want to see one of them in action yourself, go off to google.com/teapot. The IETF standards body attempted to remove
418, but as often happens on the internet, calmness happened—no, wait.
Become a Member to join the conversation.