Files
30-seconds-of-code/snippets/js/s/geometric-progression.md
Angelos Chalaris 4d0316a062 Update covers
2023-05-07 22:25:00 +03:00

1.1 KiB

title, type, language, tags, cover, excerpt, dateModified
title type language tags cover excerpt dateModified
Geometric progression snippet javascript
math
algorithm
half-trees Initializes an array containing the numbers in the specified geometric progression range. 2020-12-28T13:49:24+02:00

Initializes an array containing the numbers in the specified range where start and end are inclusive and the ratio between two terms is step. Returns an error if step equals 1.

  • Use Array.from(), Math.log() and Math.floor() to create an array of the desired length, Array.prototype.map() to fill with the desired values in a 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]