arithmeticProgression

JavaScript, Math, Algorithm

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

  • Use Array.from() to create an array of the desired length, lim/n, and a map function 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 );
Examples
arithmeticProgression(5, 25); // [5, 10, 15, 20, 25]

Recommended snippets

  • geometricProgression

    JavaScript, Math

    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.

  • powerset

    JavaScript, Math

    Returns the powerset of a given array of numbers.

  • primes

    JavaScript, Math

    Generates primes up to a given number, using the Sieve of Eratosthenes.