When we speak about arithmetic progression (or arithmetic sequence) we mean a series of numbers with a special property - each value is followed by the other, greater by predefined amount (step).

I.e. difference of (K+1)-th and K-th values is a constant. Here are examples of sequences

1 2 3 4 5 6 7 ...

4 6 8 10 12 14 16...

10 13 16 19 22 25 28...

Since so, arithmetic sequence is completely defined by the first member (A) and the increment value - step size - (B). First few members could be expressed as

A + (A + B) + (A + 2B) + (A + 3B) + ...

Here we could of course write loops and solve the problem that way, but it gets a lot simpler if we factor out our solution a bit, and get some method to the madnes,

since we have A, B ad N as shown up we can reduce this to A*N + (1+2+...+b-1) * b the only not so obvious part here is the parenthesized part but it turns out it's rather easy to get in oforth, since we have #seq the part then becomes A N * B 1 - seq sum B * + in proper reverse notation, we just need to get this to deal with the stack, and it doesn't need much stack juggling so that means we get to not use locals

- Code: Select all
`: getLn { System.Console askln }`

: getNumber { getLn asInteger }

: pprint { apply(#.) printcr }

: getInput { [] getNumber #[ getLn + ] times }

: parseLine { words map(#asInteger) }

: 3list>st { dup first swap dup second swap third } // [a,b,n] -- a b n

: arithseq { tuck 1 - seq sum * tor * + } // a * n + (1 + 2 + ... [b-1] )b

: solveline { parseLine 3list>st arithseq }

getInput map(#solveline) pprint