Geometric progression

JavaScript, Math, Algorithm · Dec 28, 2020

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.

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

  • Arithmetic progression

    Creates an array of numbers in the arithmetic progression, starting with the given positive integer and up to the specified limit.

    JavaScript, Math · Oct 13, 2021

  • Powerset

    Returns the powerset of a given array of numbers.

    JavaScript, Math · Sep 27, 2021

  • Greatest common divisor

    Calculates the greatest common divisor between two or more numbers/arrays.

    JavaScript, Math · Dec 29, 2020