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

We could also create separate functions which would create the sequence array of the desired type for us.

Here’s your 1st example as a function named seqIntsRepeatedNTimesEach(n, lo, hi):

int[] seq4_1_20, seqNeg4_1_20, seq1_10_Neg10;

void setup() {
  seq4_1_20 = seqIntsRepeatedNTimesEach(4, 20);
  seqNeg4_1_20 = seqIntsRepeatedNTimesEach(4, -20);
  seq1_10_Neg10 = seqIntsRepeatedNTimesEach(-1, 10, -10);

  println(str(seq4_1_20));
  println();
  println(str(seqNeg4_1_20));
  println();
  println(str(seq1_10_Neg10));

  exit();
}

static final int[] seqIntsRepeatedNTimesEach(final int n, final int hi) {
  return seqIntsRepeatedNTimesEach(n, hi >= 0 ? 1 : -1, hi);
}

static final int[] seqIntsRepeatedNTimesEach(
  final int n, final int lo, final int hi)
{
  final int
    nn = Math.abs(n), 
    len = (Math.abs(hi - lo) + 1) * nn, 
    dir = hi >= lo ? 1 : -1, 
    seq[] = new int[len];

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

  return seq;
}
1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10 10 11 11 11 11 12 12 12 12 13 13 13 13 14 14 14 14 15 15 15 15 16 16 16 16 17 17 17 17 18 18 18 18 19 19 19 19 20 20 20 20 -1 -1 -1 -1 -2 -2 -2 -2 -3 -3 -3 -3 -4 -4 -4 -4 -5 -5 -5 -5 -6 -6 -6 -6 -7 -7 -7 -7 -8 -8 -8 -8 -9 -9 -9 -9 -10 -10 -10 -10 -11 -11 -11 -11 -12 -12 -12 -12 -13 -13 -13 -13 -14 -14 -14 -14 -15 -15 -15 -15 -16 -16 -16 -16 -17 -17 -17 -17 -18 -18 -18 -18 -19 -19 -19 -19 -20 -20 -20 -20 10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10
1 Like