使用最小测试,我正在尝试通过此测试,要求获得5!
的总和:
def test_sum_of_factorials
sum_of_factorials = 0
numbers = [1, 2, 3, 4, 5]
assert_equal 153, sum_of_factorials
end
我写了一个通过的解决方案,但是它又长又重复:
fractorial_5 = numbers.inject(1) {|aggregate, num| aggregate * num}
fractorial_4 = numbers[0..3].inject(1) {|aggregate, num| aggregate * num}
fractorial_3 = numbers[0..2].inject(1) {|aggregate, num| aggregate * num}
fractorial_2 = numbers[0..1].inject(1) {|aggregate, num| aggregate * num}
fractorial_1 = 1
fractorials_array = [fractorial_1, fractorial_2, fractorial_3, fractorial_4, fractorial_5]
fractorials_array.each {|fractorial| sum_of_factorials += fractorial}
有人愿意提供一个更清洁,更简单的解决方案吗?
答案 0 :(得分:1)
def sum_of_factorials(n)
(1..n).reduce([1, 0]) { |(f, sum), e| [(f *= e), (sum + f)] }.last
end
产生
sum_of_factorials(5) #=> 153
说明:
def sum_of_factorials(n)
(1..n). # n times
reduce([1, 0]) do # iterate and use [factorial, sum] as an accumulator
|(f, sum), e| # splat the accumulator into f and sum variables
[ # each time recalculate the accumulator as
f *= e, # product of the element and previous factorial,
sum + f # sum of current factorial and previous sum
]
end.last # and return only last part(sum) of the accumulator
end
答案 1 :(得分:1)
看来您已获得:
def test_sum_of_factorials
sum_of_factorials = 0
numbers = [1, 2, 3, 4, 5]
<...missing bits...>
assert_equal 153, sum_of_factorials
end
,并被要求填写缺失位。我认为有人要求以下内容。
def test_sum_of_factorials
sum_of_factorials = 0
numbers = [1, 2, 3, 4, 5]
fac = 1
numbers.each do |n|
fac *= n
sum_of_factorials += fac
end
assert_equal 153, sum_of_factorials
end
我们可以这样写:
def test_sum_of_factorials
numbers = [1, 2, 3, 4, 5]
assert_equal 153, sum_of_factorials(numbers)
end
def sum_of_factorials(numbers)
fac_sum = 0
fac = 1
numbers.each do |n|
fac *= n
fac_sum += fac
end
fac_sum
end
其中
sum_of_factorials([1,2,3,4,5])
#=> 153
但是,使用Array#sum来编写sum_of_factorials
的方式会更像Ruby:
def sum_of_factorials(numbers)
fac = 1
numbers.sum { |n| fac *= n }
end
与问题的标题一起,这就是为什么其他答案的作者都假设您正在询问如何改进方法sum_of_factorials
。首先,可以将其传递给参数numbers.max
,而不是数组numbers
。
写sum_of_factorials
的另一种方法是使用方法Enumerator::produce,该方法在v2.7中首次出现。
def sum_of_factorials(n)
enum = Enumerator.produce([1,1]) { |n0, n1| [n0+1, (n0+1)*n1] }
n.times.sum { enum.next.last }
end
(1..8).each { |n| puts "%d: %6d" % [n, sum_of_factorials(n)] }
1: 1
2: 3
3: 9
4: 33
5: 153
6: 873
7: 5913
8: 46233
请注意,如果
enum = Enumerator.produce([1,1]) { |n0, n1| [n0+1, (n0+1)*n1] }
#=> #<Enumerator: #<Enumerator::Producer:0x000059d490c742a0>:each>
然后
enum.next #=> [1, 1]
enum.next #=> [2, 2]
enum.next #=> [3, 6]
enum.next #=> [4, 24]
因此1
到4
的阶乘由(在重新定义或倒退enum
之后)给出:
enum.next.last #=> 1
enum.next.last #=> 2
enum.next.last #=> 6
enum.next.last #=> 24
如果n
等于零,则在方法开头添加行return 1 if n.zero?
。
答案 2 :(得分:1)
另一种选择是定义阶乘函数(在这里被盗:https://stackoverflow.com/a/12415362/5239030),或者作为Integer
类的补丁:
module MyMathFunctions
def factorial
(1..self).reduce(1, :*)
end
end
Integer.include MyMathFunctions
因此您可以通过以下方式使用它:10.factorial #=> 3628800
然后,只需致电:
[1, 2, 3, 4, 5].sum &:factorial
#=> 153
答案 3 :(得分:1)
我想简单地如下,
[1, 2, 3, 4, 5].map { |x| (1..x).inject(1, :*) }.sum
# => 153
答案 4 :(得分:0)
由于您可以写5!+4!+3!+2!+1 !,通常为1!+2!+3!+ ... + n作为1 + 2(1 + 3(1 + 4(1 + 5(1 + ...(1 + n))))))),您可以在O(n)中完成
sum_of_factorials = 1
numbers.drop(1).reverse_each { |i| sum_of_factorials = 1 + i * sum_of_factorials }