0

I have a quadratic sequence in which I need to use to loop over, however, I am not sure how to execute this kind of logic... I want to use the quadratic sequence to count up to a given number <= n. The problem is that I have to give my quadratic sequence for loop some number to count and iterate up to as well... I know it's possible to iterate by 2s, 3s, 4s, etc. by doing things like i += 2, is it possible to use a quadratic expression as well? Is there a method using Javascript in which will allow me to iterate through quadratic sequences? I hope this is clear, thanks!

Here's my code:

The quadratic sequence is the sequence being console logged. The first 14 terms are: 1,3,6,10,15,21,28,36,45,55,66,78,91,105

for (let i = 1; i <= 14; i++) {
      let a = i;
      let nthTerm = a - 1;
      let b = 1 / 2;
      let quadraticSequence = b * a ** 2 + b * a;
      
console.log(`${nthTerm} ${quadraticSequence}`);
      
const num = 93;

      // use quadratic sequence to count up to a number <= 93;
      for (i = 0; i <= num; i++quadraticSequence) {
        console.log(i);
      }
    }

The result should console.log a count 0 > 105. I know the second for loop is not correct, but I'm not quite sure how to approach a proper attempt.

So instead of counting like, 1,2,3,4,5,6,7,8,9... It should count 1,3,6,10,15,21,28,36,45,55... Ultimately, I am trying to count up to a given number <= n (i.e. 93) through the quadratic sequence instead of the usually, 1,2,3,4,5, sequence.

King Muze
  • 75
  • 7
  • Can you be more specific what exactly you want? If you want to iterate through numbers like 1, 4, 9, 16, etc, just use a regular for loop and then use the square of i. Otherwise please make the question more clear – S. Strempfer Mar 21 '21 at 03:33
  • The quadratic sequence is what is being console.logged. That's the sequence in which I want to iterate by. It starts, 1, 3, 6, 10, 15, 21, 28, 36, etc... Is there a simpler way of producing this sequence? – King Muze Mar 21 '21 at 03:37

1 Answers1

1
let num = 93;
for (let i = 1, j = 2; i < 100; i += j, j += 1) {
  console.log(i);
  for (let k = 0; k <= num; k += i) {
    console.log(k);
  }
}

This will produce the sequence 1, 3, 6, 10, 15, 21, 28, 36, ... and then use each of them in a seperate for loop as step size.

An alternative interpretation would be using the 0th element in the quadratic series as the first step size and the next element as the next step size, etc.

for (let i = 1, j = 2, k = 0; k < 1000; k += i, i += j, j += 1) {
  console.log(`Current, step: ${k}, ${i}`);
}

This should do that

S. Strempfer
  • 290
  • 2
  • 6