Skip to content

Commit

Permalink
Consolidate arithmetic and geometric progression
Browse files Browse the repository at this point in the history
  • Loading branch information
Chalarangelo committed Feb 17, 2024
1 parent 7f2da7b commit daf8c99
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 46 deletions.
6 changes: 6 additions & 0 deletions content/redirects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2638,3 +2638,9 @@
- from: /js/s/pad-number
to: /js/s/number-formatting
status: 301!
- from: /js/s/arithmetic-progression
to: /js/s/arithmetic-and-geometric-progression
status: 301!
- from: /js/s/geometric-progression
to: /js/s/arithmetic-and-geometric-progression
status: 301!
42 changes: 42 additions & 0 deletions content/snippets/js/s/arithmetic-and-geometric-progression.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: Arithmetic and geometric progression in JavaScript
shortTitle: Arithmetic and geometric progression
type: story
language: javascript
tags: [math,algorithm]
cover: half-trees
excerpt: Create arrays of numbers in arithmetic and geometric progression.
dateModified: 2024-02-16
---

Arithmetic progression refers to a sequence of numbers in which the **difference** between any two successive members is a constant. Geometric progression refers to a sequence of numbers in which the **ratio** between any two successive members is a constant. Both can be easily implemented in JavaScript.

## Arithmetic progression

Given a positive integer `n` and a positive limit `lim`, the task is to create an array of numbers in the arithmetic progression, starting with the given positive integer and up to the specified limit.

In order to do so, you can use `Array.from()` to create an array of the desired length, `lim / n`. Then, use a map function as the second argument to fill it with the desired values in the given range.

```js
const arithmeticProgression = (n, lim) =>
Array.from({ length: Math.ceil(lim / n) }, (_, i) => (i + 1) * n );

arithmeticProgression(5, 25); // [5, 10, 15, 20, 25]
```

## Geometric progression

Given a positive integer `end`, and optional positive integers `start` and `step`, the task is to create an array of numbers in the geometric progression, starting with the given positive integer and up to the specified limit.

In order to do so, you can use `Array.from()`, `Math.log()` and `Math.floor()` to create an array of the desired length. Then, use `Array.prototype.map()` to fill it with the desired values in the given range. Omit the second argument, `start`, to use a default value of `1`. Omit the third argument, `step`, to use a default value of `2`.

```js
const geometricProgression = (end, start = 1, step = 2) =>
Array.from({
length: Math.floor(Math.log(end / start) / Math.log(step)) + 1,
}).map((_, i) => start * step ** i);

geometricProgression(256); // [1, 2, 4, 8, 16, 32, 64, 128, 256]
geometricProgression(256, 3); // [3, 6, 12, 24, 48, 96, 192]
geometricProgression(256, 1, 4); // [1, 4, 16, 64, 256]
```
19 changes: 0 additions & 19 deletions content/snippets/js/s/arithmetic-progression.md

This file was deleted.

27 changes: 0 additions & 27 deletions content/snippets/js/s/geometric-progression.md

This file was deleted.

0 comments on commit daf8c99

Please sign in to comment.