Added prod and sum functions for arrays

This commit is contained in:
2021-03-24 17:55:38 -04:00
parent dbc2670a48
commit 879fdfd126
2 changed files with 79 additions and 2 deletions

View File

@@ -21,6 +21,7 @@ Copyright (C) 2021 Matthew Ellison
*/
import { SSL_OP_SSLEAY_080_CLIENT_DH_BUG } from "constants";
import { InvalidResult } from "./InvalidResult";
@@ -387,3 +388,34 @@ export function getFactorsBig(goalNumber: bigint): bigint[]{
//Return the list of factors
return factors;
}
export function getSum(ary: number[]): number{
let sum: number = 0;
ary.forEach(a => sum += a);
return sum;
}
export function getSumBig(ary: bigint[]): bigint{
let sum: bigint = 0n;
ary.forEach(a => sum += a);
return sum;
}
export function getProd(ary: number[]): number{
//Return 0 if the array is empty
if(ary.length == 0){
return 0;
}
let prod: number = 1;
ary.forEach(a => prod *= a);
return prod;
}
export function getProdBig(ary: bigint[]): bigint{
//Return 0 if the array is empty
if(ary.length == 0){
return 0n;
}
let prod: bigint = 1n;
ary.forEach(a => prod *= a);
return prod;
}