Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report