# Arithmetic and geometric progression in JavaScript

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.

```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`.

```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]```

## More like this

• Collection · 27 snippets

### JavaScript Algorithms

Learn a handful of popular algorithms, implemented in JavaScript ES6.

• JavaScript ·

### Implementing the Luhn Algorithm in JavaScript

Implement the Luhn Algorithm, used to validate a variety of identification numbers.

• JavaScript ·

### Check if a number is prime in JavaScript

Learn how to check if a number is prime and how to generate prime numbers up to a given number in JavaScript.

• JavaScript ·

### How can I calculate the greatest common divisor & least common multiple in JavaScript?

Use JavaScript to calculate the greatest common divisor and least common multiple of two or more numbers.

Start typing a keyphrase to see matching snippets.