Get the App
SLTechnology News&Howtos  ›  Development  › 

How to achieve the least common multiple of a list by Python

Shulou Source: shulou.com Published: 2022-06-02 16:11:08 09月23日 Update

Xiaobian to share with you how Python to achieve the least common multiple of the list, I believe most people still do not know how to share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

1. lcmfrom functools import reducefrom math import gcddef lcm(numbers): return reduce((lambda x, y: int(x * y / gcd(x, y))), numbers)# EXAMPLESlcm([12, 7]) # 84lcm([1, 3, 4, 5]) # 60

The greatest common divisor and least common multiple of two numbers satisfy the following formula:

lcm(a, b) * gcd(a, b) = abs(a * b)

For a list of more than two data, it is only necessary to continue calculating the least common multiple of any two numbers and the rest of the numbers.

That is to say:

lcm(a, b, c, ...) = lcm(lcm(a, b), c, ...)

So we iterate over the list using functools.reduce.

2. functools.reducefunctools.reduce(function, iterable[, initializer])

The first argument in the reduce function is the function function, which takes two arguments. The reduce function cumulatively applies the function to the second argument, iterable, on the iterable object. The function takes the first two values of the iterable object as input parameters, and iterates through the next iteration of the function, taking the return value and the next value of the iterable object as input parameters, until all values of the iterable object are exhausted. For example:

# ((((1+2)+3)+4)+5)reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])

The reduce function is roughly equivalent to:

def reduce(function, iterable, initializer=None): it = iter(iterable) if initializer is None: value = next(it) else: value = initializer for element in it: value = function(value, element) return value3. actual use

The code snippet above shows one way to use Python. Careful students can see that if you want to put into practical production applications, the lcm function has some problems, mainly two points, one is exception handling, including dividing zero, etc.; the other is that the least common multiple is a positive integer, and the return value of the function may appear negative.

30-seconds-of-python(https://github.com/30-seconds... The code is mainly a way of showing a thought. It doesn't check for edge cases and anomalies, and the reason behind it is to keep the project simple and show interesting techniques and ways of thinking. All code snippets assume that the user has a basic understanding of the problem, language, and potential errors that can occur, so no exception handling or parameter checking is added.

That's all for "How Python implements the least common multiple of lists." Thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Tags: Function minimum common multiple least common multiple parameter iteration two object code article content actual fragment problem processing application check input interesting careful Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Linux Redmi vpn Apple