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

Instead of incrementing by 1, the increment might be 2 or 3 or 100, or any integer value (including 1).

With those 4 arguments the expected output is:
3, 5, 7, 9
repeated twice, so:
3, 5, 7, 9, 3, 5, 7, 9
so first argument is number of repetitions
second argument is lo
third argument is hi
fourth argument is the increment. Instead of incrementing by 1, the jump is 2 (in this example).

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

Excellent! But how come you can have:

This does not work for me. How can I get just the sequence as you have it and not:

[0] 3
[1] 5
[2] 7
[3] 9
[4] 3
[5] 5
[6] 7
[7] 9
int[] testsequence;

// first parameter is number of repetitions
// second parameter is lo
// third parameter is hi
// fourth parameter is step

void setup() {
  testsequence = int_series_repeated(2, 3, 9, 2);
  //println(testsequence);
  exit();
}

int[] int_series_repeated(int n, int lo, int hi, int step) {

  int inc;
  if (step != 0) {
    inc = abs(step);
  } else {
    inc = 1;
  }

  int dir;
  if (lo < hi) {
    dir = 1 * inc;
  } else {
    dir = -1 * inc;
  }

  int qty = abs(hi - lo)/ inc + 1;
  int len = abs(n) * qty;
  int seq[] = new int[len];
  for (int i = 0; i < len; i++) {
    seq[i] = i % qty * dir + lo;
    println(seq[i]);  // here during testing
  }

  return seq;
}

Regardless if we do: println(str(seq3_1_10)); or just println(seq3_1_10);, it won’t change the contents of seq3_1_10[]!

It’s just a style of displaying/viewing what’s inside an array!

Callback setup() merely contains a sample code of how to use the actual repeatIntSeq() main function.

1 Like

Yes. I use:

  for (int j = 0; j < testsequence.length; j++) {
    print(testsequence[j], " ");
  }

But I wondered why
println(str(seq3_1_10));
works for you but not for me…?

Have you actually run (CTRL + R) my code w/o changes?
It should run on any Processing version, including v1.5.1!

1 Like

Yes, I run your code without changes.

Type String \[ \] of the last argument to method println(Object…) doesn’t exactly match the vararg parameter type. Cast to Object \ [ \ ] to confirm the non-varargs invocation, or pass individual arguments of type Object for a varargs invocation.

Does that actually block my code from running?

I’ve already told you I don’t see such message b/c I always had “Continuously check for errors” disabled in “Preferences”!

1 Like

Just curious about the println(str( ));

I started to try to include steps in the seqIntsRepeatedNTimesEach() code:
Result should be 3 3 3 5 5 5 7 7 7
Is it nearly there???

int[] testsequence;

// first argument is number of repetitions of each int number
// second argument is lo
// third argument is hi
// fourth parameter is step
void setup() {
  testsequence = each_repeated(3, 3, 7, 2);

  for (int j = 0; j < testsequence.length; j++) {
    print(testsequence[j], " ");
  }


  exit();
}

int[] each_repeated(int n, int lo, int hi, int step) {

  int inc;
  if (step != 0) {
    inc = abs(step);
  } else {
    inc = 1;
  }

  int dir;
  if (lo < hi) {
    dir = 1 * inc;
  } else {
    dir = -1 * inc;
  }

  int qty = abs(hi - lo)/ inc + 1;
  int len = abs(n) * qty;
  
  int seq [ ] = new int[len];

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

  return seq;

}

Well, it’s already giving the output you expected; so it seems correct.
Anyways, here’s my own take on it as repeatedIntSeq():

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

int[] seq3_1_10, seqNeg3_1_10, seq2_5_Neg5, seq3_3_7_2;

void setup() {
  seq3_1_10 = repeatedIntSeq(3, 10);
  seqNeg3_1_10 = repeatedIntSeq(3, -10);
  seq2_5_Neg5 = repeatedIntSeq(-2, 5, -5);
  seq3_3_7_2 = repeatedIntSeq(3, 3, 7, -2);

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

  exit();
}

/**
 * Generates a sequence of integers where each integer is repeated a specified
 * number of times.
 *
 * @param n Number of times each integer should be repeated.
 * @param hi Highest integer in the sequence.
 * If positive, the sequence increments; if negative, it decrements.
 *
 * @return An array of integers representing the repeated sequence.
 */
static final int[] repeatedIntSeq(final int n, final int hi) {
  return repeatedIntSeq(n, hi >= 0 ? 1 : -1, hi);
}

/**
 * Generates a sequence of integers where each integer is repeated a specified
 * number of times, with a defined lower and upper limit.
 *
 * @param n Number of times each integer should be repeated.
 * @param lo Lowest integer in the sequence.
 * @param hi Highest integer in the sequence.
 *
 * @return An array of integers representing the repeated sequence.
 */
static final int[] repeatedIntSeq(final int n, final int lo, final int hi) {
  return repeatedIntSeq(n, lo, hi, 1);
}

/**
 * Generates a sequence of integers where each integer is repeated a specified
 * number of times, with defined lower and upper limits and a specified step.
 *
 * @param n Number of times each integer should be repeated.
 * @param lo Lowest integer in the sequence.
 * @param hi Highest integer in 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[] repeatedIntSeq(
  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(n), 
    len = (abs(hi - lo) / inc + 1) * qty, 
    seq[] = new int[len];

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

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

Here’s both repeatedIntSeq() & repeatIntSeq() together for easier implementation comparison:

static final int[] repeatedIntSeq(
  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(n), 
    len = (abs(hi - lo) / inc + 1) * qty, 
    seq[] = new int[len];

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

  return seq;
}
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;
}

Besides i / qty vs i % qty operator difference, local variables qty & len are also calculated differently:

  1. qty: abs(n) vs abs(hi - lo) / inc + 1
  2. len: (abs(hi - lo) / inc + 1) * qty vs abs(n) * qty
2 Likes

All wonderful. Should these be submitted to the O.E.I.S?