<s>[INST] Find the sum of n terms of sequence Given is the sequence $S[i]=(i-1)%9+1$, that is
$$1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9\dots$$
Also given are three numbers $a$, $d$ and $n$.
The task is to find the sum of first $n$ terms starting from $S[a]$, separated by $d$ terms in between.
For example, for $a=8$, $d=1$, $n=5$, the sum will be $8+1+3+5+7$.
 [/INST] This answer is a sketch of one approach.
For each $i^\text{th}$ number in your sequence ($i=0$ for the first term), define $g_i = \lfloor \frac{i}{9} \rfloor$.
Given a term (say $t_i$) in your original sequence, let $T_i = t_i + 9 g_i$. This produces the familiar sequence of positive integers, for which you can work out the modified sum (say $S$) in the usual manner.
Given the set of selected $i$, you can then calculate $G = \sum 9 g_i = 9 \sum g_i$, which I'll leave as an exercise for you.
Your original sum would then be $S - G$.

Here's an example with $a=3, d=1, n=5$ (first index is 2, last index is 10) :
$$
\begin{matrix}
i   & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10  \\
g_i & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1   \\
t_i & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 1 & 2   \\
T_i & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11
\end{matrix}
$$
We now calculate $S$:
$$S = \sum_{j=0}^{4} (3 + 2j) = 3 \cdot 5 + 2 \sum_{j=0}^{4} j = 15 + 2 \cdot \frac{4 \cdot 5}{2} = 35$$
We also have $i \in \{2,4,6,8,10\}$, and calculate $G = 9\sum g_i = 9 \cdot 1 = 9$.
So the required sum is $S-G = 35 - 9 = 26$.
(Check using the original $t_i$: $3 + 5 + 7 + 9 + 2 = 26$.)
</s>