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

**URL:** <https://discourse.processing.org/t/series-from-for-loop-is-there-a-repository-for-integer-sequences/44970>\
**Category:** Beginners\
**Created:** [August 21, 2024, 6:44am UTC](https://discourse.processing.org/t/series-from-for-loop-is-there-a-repository-for-integer-sequences/44970 "2024-08-21T06:44:37Z")\
**Posts on this page:** 13\
**Page:** 2

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [August 23, 2024, 12:38pm UTC](https://discourse.processing.org/t/series-from-for-loop-is-there-a-repository-for-integer-sequences/44970/21 "2024-08-23T12:38:34Z")

</div>

> [@GoToLoop](#):
>
> I don’t get what would be the desired effect of your 4th parameter.

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

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [August 23, 2024, 12:42pm UTC](https://discourse.processing.org/t/series-from-for-loop-is-there-a-repository-for-integer-sequences/44970/22 "2024-08-23T12:42:53Z")

</div>

> [@GoToLoop](#):
>
> You didn’t say what would be the expected output for those 4 arguments.

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).

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 23, 2024, 2:23pm UTC](https://discourse.processing.org/t/series-from-for-loop-is-there-a-repository-for-integer-sequences/44970/23 "2024-08-23T14:23:02Z")

</div>

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:

```java
/**
 * 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

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [August 23, 2024, 5:26pm UTC](https://discourse.processing.org/t/series-from-for-loop-is-there-a-repository-for-integer-sequences/44970/24 "2024-08-23T17:26:36Z")

</div>

Excellent! But how come you can have:

> [@GoToLoop](#):
>
> `println(str(seq3_1_10));`

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

```auto
[0] 3
[1] 5
[2] 7
[3] 9
[4] 3
[5] 5
[6] 7
[7] 9

```

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 23, 2024, 5:47pm UTC](https://discourse.processing.org/t/series-from-for-loop-is-there-a-repository-for-integer-sequences/44970/25 "2024-08-23T17:47:10Z")

</div>

> [@paulstgeorge](#):
>
> How can I get just the sequence as you have it and not:

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.

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [August 23, 2024, 5:49pm UTC](https://discourse.processing.org/t/series-from-for-loop-is-there-a-repository-for-integer-sequences/44970/26 "2024-08-23T17:49:53Z")

</div>

Yes. I use:

```auto
  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…?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 23, 2024, 5:51pm UTC](https://discourse.processing.org/t/series-from-for-loop-is-there-a-repository-for-integer-sequences/44970/27 "2024-08-23T17:51:01Z")

</div>

> [@paulstgeorge](#):
>
> 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!

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [August 23, 2024, 5:56pm UTC](https://discourse.processing.org/t/series-from-for-loop-is-there-a-repository-for-integer-sequences/44970/28 "2024-08-23T17:56:30Z")

</div>

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.`

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 23, 2024, 5:59pm UTC](https://discourse.processing.org/t/series-from-for-loop-is-there-a-repository-for-integer-sequences/44970/29 "2024-08-23T17:59:45Z")

</div>

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”!

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [August 23, 2024, 8:15pm UTC](https://discourse.processing.org/t/series-from-for-loop-is-there-a-repository-for-integer-sequences/44970/30 "2024-08-23T20:15:10Z")

</div>

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???

```auto
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;

}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 23, 2024, 9:27pm UTC](https://discourse.processing.org/t/series-from-for-loop-is-there-a-repository-for-integer-sequences/44970/31 "2024-08-23T21:27:00Z")

</div>

> [@paulstgeorge](#):
>
> Result should be 3 3 3 5 5 5 7 7 7

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

```java
/**
 * 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

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 23, 2024, 9:33pm UTC](https://discourse.processing.org/t/series-from-for-loop-is-there-a-repository-for-integer-sequences/44970/32 "2024-08-23T21:33:28Z")

</div>

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

```java
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;
}

```

```java
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`

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [August 24, 2024, 12:58pm UTC](https://discourse.processing.org/t/series-from-for-loop-is-there-a-repository-for-integer-sequences/44970/33 "2024-08-24T12:58:12Z")

</div>

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

[Previous page](https://discourse.processing.org/t/series-from-for-loop-is-there-a-repository-for-integer-sequences/44970.md?page=1)
