From 3d6f2e2da908ea6c2ba4924b510a419f0410f5bd Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Fri, 22 Mar 2019 19:29:53 -0400 Subject: [PATCH] Added getSum and getProd for tables --- Algorithms.lua | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Algorithms.lua b/Algorithms.lua index b219ba9..833ee8a 100644 --- a/Algorithms.lua +++ b/Algorithms.lua @@ -175,3 +175,23 @@ function getDivisors(goalNumber) --Return the list return divisors; end + +function getSum(ary) + sum = 0; --Holds the sum of all elements. Start at 0 because num+1 = num + --Look through every element in the array and add the number to the running sum + for location = 1, #ary do + sum = sum + ary[location]; + end + --Return the sum of all elements + return sum; +end + +function getProd(ary) + prod = 1; --Holds the product of all elements. Start at 1 because num*1 = num + --Look through every element in the array and add the number to the running product + for location = 1, #ary do + prod = prod * ary[location]; + end + --Return the product of all elements + return prod; +end