python 序列中前 n 项的乘积

发布于 2024-12-03 21:29:08 字数 530 浏览 0 评论 0原文

我正在尝试创建一个函数,它接受一个参数(一个数字)并返回该数字的阶乘。

例如 f(5) 将返回 1*2*3*4*5

到目前为止我所拥有的是

def product(n, term):
    """Return the product of the first n terms in a sequence.

    term -- a function that takes one argument
    """
    k, total = 1, 1
    while k <= n:
        k, total = k + 1, total * term(k, 1)
    return total


def factorial(n):
    """Return n factorial by calling product.

    >>> factorial(4)
    24
    """
    return product(n, mul)

但是,是否可以使该术语仅接受 1 个参数?

I'm trying to create a function that takes one argument (a number) and returns the factorial of that number.

For example f(5) will return 1*2*3*4*5

What I have so far is

def product(n, term):
    """Return the product of the first n terms in a sequence.

    term -- a function that takes one argument
    """
    k, total = 1, 1
    while k <= n:
        k, total = k + 1, total * term(k, 1)
    return total


def factorial(n):
    """Return n factorial by calling product.

    >>> factorial(4)
    24
    """
    return product(n, mul)

However, is it possible to make it so that term only takes 1 argument?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

∝单色的世界 2024-12-10 21:29:08
import math

def factorial(n):
    return math.factorial(n)

替代实现:

def factorial(n):
    return reduce(lambda x,y:x*y,range(1,n+1))

使用递归:

def factorial(n):
     if n == 0:
         return 1
     else:
         return n * factorial(n-1)
import math

def factorial(n):
    return math.factorial(n)

Alternative implementation:

def factorial(n):
    return reduce(lambda x,y:x*y,range(1,n+1))

Using recursion:

def factorial(n):
     if n == 0:
         return 1
     else:
         return n * factorial(n-1)
素手挽清风 2024-12-10 21:29:08

计算 n 的阶乘是递归函数的标准示例:

def fac(n):
    return n * fac(n-1) if n > 1 else 1

Computing the factorial of n is a standard example of a recursive function:

def fac(n):
    return n * fac(n-1) if n > 1 else 1
何其悲哀 2024-12-10 21:29:08

又怎样呢?

import operator

def product(nums):
    return reduce(operator.mul, nums, 1)

def factorial(num):
    return product(range(2, num+1))

What about?

import operator

def product(nums):
    return reduce(operator.mul, nums, 1)

def factorial(num):
    return product(range(2, num+1))
回忆追雨的时光 2024-12-10 21:29:08

如果您的意思是在 product(n, term) 中,term(n) 应该是一个从索引 n 串联到的函数该点的值;那么你的 factorial(n) 将被定义为 def Factorial(n): return Product(n, Identity) 其中,identity 是 defidentity(n): return n

换句话说:

def product(n, term):
    """Return the product of the first n terms in a sequence.

    term -- a function that takes one argument
    """
    k, total = 1, 1
    while k <= n:
        k, total = k + 1, total * term(k)
    return total


def identity(n):
    return n

def factorial(n):
    """Return n factorial by calling product.

    >>> factorial(4)
    24
    """
    return product(n, identity)

if what you mean is that in product(n, term), term(n) should be a function from an index n in series to the value at that point; then your factorial(n) would be defined as def factorial(n): return product(n, identity) where identity is def identity(n): return n

in other words:

def product(n, term):
    """Return the product of the first n terms in a sequence.

    term -- a function that takes one argument
    """
    k, total = 1, 1
    while k <= n:
        k, total = k + 1, total * term(k)
    return total


def identity(n):
    return n

def factorial(n):
    """Return n factorial by calling product.

    >>> factorial(4)
    24
    """
    return product(n, identity)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文