# Compound Interest

`D00-F02-A02` · Financial Mathematics, Statistics, and Data Foundations → Financial Arithmetic, Time Value, and Returns · archetype `record-transform` · difficulty 1/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/foundations/financial-arithmetic-time-value-and-returns/compound-interest/
Agent skill: `npx skills add IslamBaraka90/Fintech-Algorithms-Library` — https://docs.thefintechbuilder.com/guides/agent-skill/

## Install and import

```bash
npm install fintech-algorithms
```

```ts
import { compoundInterest } from "fintech-algorithms/foundations/financial-arithmetic-time-value-and-returns/compound-interest";
```

## Signature

```ts
compoundInterest(input)
```

Grows a principal at a periodic rate applied `compoundsPerPeriod` times within each period, so earned interest starts earning interest itself.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `input` | `D00Input` | yes | A plain object. Every F02 topic first reads `principal`, `rate` and `periods` and validates them before any per-topic branch runs. This topic then reads `compoundsPerPeriod`, the number of compounding steps inside one period, coerced with `Number`. |

## Returns

`{ endingValue: number; interest: number }`

`endingValue` is `principal * (1 + rate / compoundsPerPeriod)` raised to `compoundsPerPeriod * periods`. `interest` is that value less the original principal.

## Errors

- When input is not a plain object — throws TypeError
- When principal is negative, periods is negative, or rate is at or below -1 — throws RangeError
- When compoundsPerPeriod is zero or negative — throws RangeError

## Complexity

Time `O(1)`, space `O(1)`.

## Worked example

This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.

### Input

`input`:

```json
{
  "principal": 1000,
  "rate": 0.05,
  "periods": 3,
  "compoundsPerPeriod": 12,
  "futureValue": 1200,
  "cashFlows": [-1000, 400, 400, 400],
  "startValue": 100,
  "endValue": 110,
  "returns": [0.1, -0.05, 0.08],
  "frequency": 12,
  "periodicReturn": 0.01
}
```

### Call

```ts
compoundInterest(input)
```

### Returns

object with 2 fields: endingValue, interest

```json
{
  "endingValue": 1161.4722313334678,
  "interest": 161.4722313334678
}
```

## Verification and provenance

Tier: **verified** (via input-expected).

The worked example below is the figure published in this algorithm's article, replayed and asserted by the test suite on every build. The arithmetic cannot drift without the build failing.

Both tiers guarantee the signature. Full explanation: https://docs.thefintechbuilder.com/guides/verification/

Generated from the docs.json payload shipped inside fintech-algorithms@0.13.0.
The signature and parameter list are checked against the compiled implementation at build time,
so a description that contradicts the code fails the build rather than reaching this file.

## Links

- Article (how it works, step by step): https://thefintechbuilder.com/foundations/financial-arithmetic-time-value-and-returns/compound-interest/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/foundations/financial-arithmetic-time-value-and-returns/compound-interest/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/foundations/llms.txt
