Series from For loop (is there a repository for Integer Sequences?)

Now I’ve understood it!

Made some changes to my previous periodicIntSeqRepeatedNTimes() to include the 4th parameter.

Also renamed it to just repeatIntSeq() and added docstrings:

/**
 * Repeat Int Sequence (v1.0.2)
 * GoToLoop (2024/Aug/23)
 *
 * https://Discourse.Processing.org/t/series-from-for-loop-
 * is-there-a-repository-for-integer-sequences/44970/23
 */

int[] seq3_1_10, seqNeg3_1_10, seq2_5_Neg5, seq2_3_9_2;

void setup() {
  seq3_1_10 = repeatIntSeq(3, 10);
  seqNeg3_1_10 = repeatIntSeq(3, -10);
  seq2_5_Neg5 = repeatIntSeq(-2, 5, -5);
  seq2_3_9_2 = repeatIntSeq(2, 3, 9, -2);

  println(str(seq3_1_10));
  println();
  println(str(seqNeg3_1_10));
  println();
  println(str(seq2_5_Neg5));
  println();
  println(str(seq2_3_9_2));

  exit();
}

/**
 * Generates a repeated integer sequence from a specified high value.
 *
 * @param n Number of times to repeat the sequence.
 * @param hi Upper limit of the sequence.
 * If positive, the sequence increments; if negative, it decrements.
 *
 * @return An array of integers representing the repeated sequence.
 */
static final int[] repeatIntSeq(final int n, final int hi) {
  return repeatIntSeq(n, hi >= 0 ? 1 : -1, hi);
}

/**
 * Generates a repeated integer sequence between a specified low and high
 * range.
 *
 * @param n Number of times to repeat the sequence.
 * @param lo Lower limit of the sequence.
 * @param hi Upper limit of the sequence.
 *
 * @return An array of integers representing the repeated sequence.
 */
static final int[] repeatIntSeq(final int n, final int lo, final int hi) {
  return repeatIntSeq(n, lo, hi, 1);
}

/**
 * Generates a repeated integer sequence between a specified low and high
 * range with a defined step.
 *
 * @param n Number of times to repeat the sequence.
 * @param lo Lower limit of the sequence.
 * @param hi Upper limit of the sequence.
 * @param step Increment for generating the sequence. If 0 defaults to 1.
 *
 * @return An array of integers representing the repeated sequence.
 */
static final int[] repeatIntSeq(
  final int n, final int lo, final int hi, final int step)
{
  final int
    inc = step != 0 ? abs(step) : 1, 
    dir = inc * (lo < hi ? 1 : -1), 
    qty = abs(hi - lo) / inc + 1, 
    len = abs(n) * qty, 
    seq[] = new int[len];

  for (int i = 0; i < len; ++i) seq[i] = i % qty * dir + lo;

  return seq;
}
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 5 4 3 2 1 0 -1 -2 -3 -4 -5 5 4 3 2 1 0 -1 -2 -3 -4 -5 3 5 7 9 3 5 7 9
2 Likes