函数式编程Python:数字1到20中的每一个可以被整除的最小数字(Functional Programming Python: smallest number divisible by each o

系统教程 行业动态 更新时间:2024-06-14 17:03:52
函数式编程Python:数字1到20中的每一个可以被整除的最小数字(Functional Programming Python: smallest number divisible by each of the numbers 1 to 20)

我想使用它的函数式编程功能在python中建立一个while循环,但直到现在我失败了。

我所完成的是代码的和平,它应该计算可以被1到20中的每一个整除的最小数字。但它似乎并没有使用函数式编程功能。 并且还给我错误如下:

“i”增量行处的“RuntimeError:超过最大递归深度”;

即使这应该限制为20。

def byYmult(x, y): return x % y == 0 def first20div(): i=0 for y in range(1,20): i += byYmult(x, y) return i >= 20 def while_block(): global x if first20div(): print(x) return 1 else: x += 1 return 0 x = 0 while_FP = lambda: ((not first20div()) and while_block() ) or while_FP() while_FP()

I want to build a while loop in python using its functional programming capabilities, but until now I'm failing.

What I have accomplished is this peace of code, which should calculate the smallest number divisible by each of the numbers 1 to 20. But it doens't seem it's using functional programming capabilities a lot. And also gives me the error as below:

"RuntimeError: maximum recursion depth exceeded" at the line of the incrementation of "i";

even if this should be limited to 20.

def byYmult(x, y): return x % y == 0 def first20div(): i=0 for y in range(1,20): i += byYmult(x, y) return i >= 20 def while_block(): global x if first20div(): print(x) return 1 else: x += 1 return 0 x = 0 while_FP = lambda: ((not first20div()) and while_block() ) or while_FP() while_FP()

最满意答案

这是非功能性的,原因很多:

你不通过,也不返回函数; 你只在底部构造一个命名的lambda表达式,但这通常被认为是非pythonic ; 通常函数式编程意味着你不会改变数据,但是在这里你定义了一个你更新的global x ; 由一些全局变量也被视为非功能性的:所有的数据都应该传递给函数。

所以有很多工作要做。 此外,你所描述的算法不是非常优化的。 我们不要在暴力破解的时候继续猜测数字,直到最终幸运,但更好的方法是计算数字1..20的最小公倍数(LCM)

我们可以首先定义 - 以一种功能性的方式 - 我们可以通过首先计算最大公用分频器(GCD)来计算LCM,这可以通过欧几里得算法来完成。 对我们来说幸运的是,它已经在math包中:

from math import gcd

现在LCM是:

def lcm(a,b): return (a*b)//gcd(a,b)

三个或更多数字的LCM可以通过计算前两个数字的LCM,然后将其作为第一个参数传递给具有第三个数字的LCM来计算,或者更正式地传递给LCM:

lcm(x,y,z) == lcm(lcm(x,y),z)

这可以通过使用functools reduce来完成:

from functools import reduce def lcm_multiple(xs): return reduce(lcm, xs)

现在我们可以通过传递一个range(2,20)对象来计算答案:

answer = lcm_multiple(range(2, 20))

或者完整:

from math import gcd from functools import reduce def lcm(a,b): return (a*b)//gcd(a,b) def lcm_multiple(xs): return reduce(lcm, xs) answer = lcm_multiple(range(2, 20))

This is non-functial for a lot of reasons:

you do not pass, nor return functions; you only construct a named lambda expression at the bottom, but this is usually considered un-Pythonic; usually functional programming means you do not alter data, but here you define a global x, that you update; by some globals are also seen as non-functional: all data should be passed to the function.

So there is a lot to work with. Furthermore the algorithm you describe is not very optimal. Instead of performing a brute force approach where we keep guessing the number until finally we got lucky, a better approach is to calculate the least common multiple (LCM) of the numbers 1..20.

We can first define - in a functional way - we can calculate the LCM by calculating the greatest common divider (GCD) first, and this can be done by the Euclidean Algorithm. Lucky for us, it is already in the math package:

from math import gcd

Now the LCM is:

def lcm(a,b): return (a*b)//gcd(a,b)

The LCM of three or more numbers can be calculated by calculating the LCM of the first two numbers and then pass it as first argument to the LCM with the third number, or more formally:

lcm(x,y,z) == lcm(lcm(x,y),z)

and this can be done by using reduce from functools:

from functools import reduce def lcm_multiple(xs): return reduce(lcm, xs)

and now we can calculate the answer, by passing it a range(2,20) object:

answer = lcm_multiple(range(2, 20))

Or in full:

from math import gcd from functools import reduce def lcm(a,b): return (a*b)//gcd(a,b) def lcm_multiple(xs): return reduce(lcm, xs) answer = lcm_multiple(range(2, 20))

更多推荐

本文发布于:2023-04-24 12:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/d242f341d0e95ca8a7af7a82dbe02988.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字   函数   最小   Python   Functional

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!