Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Dependencies
*node_modules/

# TypeScript build output
dist/
*.tsbuildinfo

# Tests / coverage
coverage/

# Logs & env
*.log
.env
.env.*
!.env.example

# OS
.DS_Store
Thumbs.db
.history/
*.db
*.db-journal
54 changes: 54 additions & 0 deletions src/problem4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Problem 4: Summation to n

This folder contains three TypeScript implementations of the **triangular number** (sum of integers from 1 through n inclusive)

Example: `sum_to_n(5) === 1 + 2 + 3 + 4 + 5 === 15`.

**Assumption:** The mathematical result is always less than `Number.MAX_SAFE_INTEGER` (so you stay in the range where double-precision arithmetic in JavaScript is exact for these integers).

## Files

| File | Purpose |
| ------------------ | ----------------------------------------------------------------------------- |
| `sum_to_n.ts` | Three exported functions: `sum_to_n_a`, `sum_to_n_b`, `sum_to_n_c` |
| `sum_to_n.test.ts` | Vitest checks against known values and cross-checks all three implementations |
| `tsconfig.json` | Strict TypeScript; `module` / `moduleResolution` set to `Node16` |
| `vitest.config.ts` | Runs tests in Node against `*.test.ts` |

## Implementations

| Function | Approach | Time | Extra space |
| ------------ | ------------------------ | ---- | ------------ |
| `sum_to_n_a` | Closed form \(n(n+1)/2\) | O(1) | O(1) |
| `sum_to_n_b` | Iterative `for` loop | O(n) | O(1) |
| `sum_to_n_c` | `Array.from` + `reduce` | O(n) | O(n) (array) |

For large nonnegative `n`, **`sum_to_n_a`** does the least work. **`sum_to_n_b`** is straightforward and allocation-free. **`sum_to_n_c`** mirrors a functional style but pays for building an array.

## Setup

From this directory:

```bash
npm install
```

## Tests

```bash
npm test
```

Runs Vitest once (`vitest run`). For watch mode while editing:

```bash
npx vitest
```

## Compile (optional)

Emit JavaScript into `dist/`:

```bash
npx tsc -p .
```
Loading