Home > Backend Development > PHP Tutorial > javascript - 如何Python双阶乘

javascript - 如何Python双阶乘

WBOY
Release: 2016-06-06 20:24:41
Original
1818 people have browsed it

<code class="php">5! = 5*4*3*2*1 = 120;
print reduce(lambda x,y:x*y,range(1,6))#120
5!! = 5*3*1 = 15;
//这个怎么处理?
</code>
Copy after login
Copy after login

回复内容:

<code class="php">5! = 5*4*3*2*1 = 120;
print reduce(lambda x,y:x*y,range(1,6))#120
5!! = 5*3*1 = 15;
//这个怎么处理?
</code>
Copy after login
Copy after login

<code class="python3">import operator
reduce(operator.mul,range(5,0,-2))</code>
Copy after login

或者 你如你坚持自己写乘法,

<code class="python3">reduce(lambda x,y:x*y,range(5,0,-2))</code>
Copy after login

另外 如果你不想import

<code class="python3">reduce(int.__mul__,range(5,0,-2))</code>
Copy after login

不知道双阶乘的意思 不过你看这个能用吗

<code class="python">reduce(lambda x,y:x*y,range(1,6, 2))</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template