A Very Big Sum

ECMAScript has +0 and -0. Read more:

TypeScript

The TypeScript version of this challenge sounds very difficult because they mention things like LONG_INTEGER and LONG_INTEGER_ARRAY, which don’t matter for languages like ECMAScript (JavaScript, TypeScript) or other languages that don’t bother too much with different numeric types. It was probably ported from C or C++, or some other language where you can’t store a long int into an int (or other similar situations) without losing precision.

Test Cases

import { assertEquals } from "/deps.ts";
import { sum } from "./bigSum-v3.ts";

Deno.test("sum()", async (t) => {
  await t.step("should sum empty array", () => {
    assertEquals(sum([]), 0);
  });

  await t.step("should sum arrays of 1 element", () => {
    assertEquals(sum([0]), 0);
    assertEquals(sum([-0]), 0);

    //
    // $ deno repl
    //
    // > -0 === -0
    // true
    // > -0 === +0
    // true
    // > +0 === -0
    // true
    //
    // Yet, this fails. Must be the way `assertEquals()` is implemented.
    //
    // assertEquals(sum([-0]), -0);
    //

    assertEquals(sum([1]), 1);
    assertEquals(sum([-1]), -1);
  });

  await t.step("should sum arrays of 2 or more elements", () => {
    assertEquals(sum([1, 2]), 3);
    assertEquals(sum([-1, -2]), -3);

    assertEquals(sum([-1, 1, -2, 2]), 0);
    assertEquals(sum([-1, 1, -2, 2]), 0);

    assertEquals(sum([1e4]), 1e4);
    assertEquals(sum([-1e4]), -1e4);
  });

  await t.step("should sum test case from problem description", () => {
    assertEquals(sum([
      1000000001,
      1000000002,
      1000000003,
      1000000004,
      1000000005,
    ]),
    5000000015,
    );
  });
});

v1 Procedural for loop

/**
 * Sums an array of numbers.
 *
 * This solution uses a for loop in a procedural style.
 *
 * **TIME COMPLEXITY**: O(n). We iterate once for each element of the
 * input array of numbers.
 *
 * **SPACE COMPLEXITY**: O(1). We simply add to the `total` variable.
 *
 * @param xs The array of numbers to sum.
 * @returns The sum.
 */
function sum(xs: number[]): number {
  let total: number = 0;

  for (let i: number = 0; i < xs.length; ++i)
    total += xs[i];

  return total;
}

export { sum };

v2 FPish with reduce and inline add function

/**
 * Sums an array of numbers.
 *
 * This solution uses a reducing function in a more FPish way. The
 * reducing function is defined in place through an arrow function.
 *
 * **TIME COMPLEXITY**: O(n). We iterate once for each element of the
 * input array of numbers.
 *
 * **SPACE COMPLEXITY**: O(1). We simply add to the `total` variable.
 *
 * @param xs The array of numbers to sum.
 * @returns The sum.
 */
function sum(xs: number[]): number {
  return xs.reduce((acc: number, n: number): number => {
    return acc + n;
  }, 0);
}

export { sum };

v3 FPish with reduce and helper add function

import { add } from "/lib/add.ts";

/**
 * Sums an array of numbers.
 *
 * This solution uses a reducing function in a more FPish way making
 * use of the `add` helper.

 * **TIME COMPLEXITY**: O(n). We iterate once for each element of the
 * input array of numbers.
 *
 * **SPACE COMPLEXITY**: O(1). We simply add to the `total` variable.
 *
 * @param xs The array of numbers to sum.
 * @returns The sum.
 */
function sum(xs: number[]): number {
  return xs.reduce(add, 0);
}

export { sum };

Ramda REPL