1
$\begingroup$

If you were to take the following for-loop:

for (int i = 1; i <= 50; i++) {} 

And write it in mathematical form, it would look something like this:
$\sum\limits_{i=1}^{50}i$

Each interval of the above sequence would be the same as each interval i of the above for-loop

the $50$ is the maximum number, the "$i=0$" is the starting point, but what about the iterations. all of them seem to have to do with $i$ being the current index of the sequence.

How would I write something that has iterations that are greater than one?

Meaning:

Is there a way of writing this:

for (int i = 1; i <= 50; i += 3) {} 

in summation form?

  • 0
    @TMM - I was referring to to each interval of interval of the sequence, not the actual final summation. but if you want it to be the summation, I would just add `int v = 0;` above the loop, `v +=1` inside of the loop, and and `system.out.println(v);` after the loop. But the point of my question is still the same.2012-05-04

1 Answers 1

2

To get a sequence of integers with a gap of $d$ and a starting point of $a$, you want $kd+a$ for $k=0,1,2,\dots$. In your example that would be $3k+1$ for $k=0,1,2,\dots$. Thus, you your summation is $\sum_{k=0}^nx_{3k+1}=x_1+x_4+x_7+\ldots+x_{3n+1}\;.$ (I'm using $x$ as the generic name of whatever you're summing.)

Now you want to find the right $n$. Here you want $3n+1$ to be as large as possible while not exceeding $50$. $3n+1\le 50$ if and only if $3n\le 49$ if and only if $n\le\frac{49}3$, but you want $n$ to be an integer, so $n=16$: $\sum_{k=0}^{16}x_{3k+1}\;.$

In general, if the upper limit on the subscript is $L$, you'll want $n=\left\lfloor\frac{L}d\right\rfloor$.