【问题标题】:How do I create a non-recursive calculation of factorial using iterators and ranges?如何使用迭代器和范围创建阶乘的非递归计算?
【发布时间】:2023-12-30 05:09:01
【问题描述】:

我遇到了一个一直困扰着我的 Rustlings 练习:

pub fn factorial(num: u64) -> u64 {
    // Complete this function to return factorial of num
    // Do not use:
    // - return
    // For extra fun don't use:
    // - imperative style loops (for, while)
    // - additional variables
    // For the most fun don't use:
    // - recursion
    // Execute `rustlings hint iterators4` for hints.
}

解决方案的提示告诉我...

在命令式语言中,您可以编写一个 for 循环来迭代 通过将值乘以可变变量。或者你可能 使用递归和匹配子句编写更实用的代码。但 你也可以使用范围和迭代器来解决这个问题。

我尝试了这种方法,但我遗漏了一些东西:

if num > 1 {
    (2..=num).map(|n| n * ( n - 1 ) ??? ).???
} else {
    1
}

我是否必须使用.take_while 之类的东西来代替if

【问题讨论】:

  • if num > 1 { (2..=num).fold(1, |acc, n| acc * n) } else { 1 }
  • 与折叠完美搭配!谢谢那个提示
  • 你甚至不需要用这种方法对<= 1进行特殊处理。
  • 嗯......这是真的,我什至没有意识到...... Rust 对我来说仍然很神奇;D

标签: rust iterator non-recursive


【解决方案1】:

阶乘定义为从起始数字到 1 的所有数字的乘积。我们使用该定义和Iterator::product

fn factorial(num: u64) -> u64 {
    (1..=num).product()
}

如果您查看整数的 the implementationProduct,您会发现它在底层使用了 Iterator::fold

impl Product for $a {
    fn product<I: Iterator<Item=Self>>(iter: I) -> Self {
        iter.fold($one, Mul::mul)
    }
}

你可以自己硬编码:

fn factorial(num: u64) -> u64 {
    (1..=num).fold(1, |acc, v| acc * v)
}

另见:

【讨论】:

  • 哇!我怎么错过了那个??非常感谢
【解决方案2】:

虽然使用.product().fold() 可能是最好的答案,但您也可以使用.for_each()

fn factorial(num: u64) -> u64 {
    let mut x = 1;
    (1..=num).for_each(|i| x *= i);
    x
}

【讨论】:

    最近更新 更多