An arithmetic series adds a fixed constant d to the previous term to get the next term.
a1+a2+a3+⋯+an=a1+(a1+d)+(a1+2d)+⋯+(a1+(n−1)d)
The sum of a1 through an is
Sn=2n(2a1+(n−1)d)
A geometric series multiplies the previous term by a fixed constant r to get the next term.
a1+a2+a3+⋯+an=a1+ra1+r2a1+⋯+rn−1a1
The sum of a1 through an is
Sn=a1r−1rn−1
You are given the first three terms of a series that is either arithmetic or geometric. Compute the sum of its first N terms. The first term has index 1, and the sum includes the N-th term.
The input holds several data sets, two lines each. The first line is N, the number of terms to sum. The second line is the first three terms of the series, integers separated by spaces. The series is either arithmetic or geometric. A line whose N is zero ends the input, and no series line follows it. If the series is geometric, the ratio r is an integer.
For each data set print Sn on its own line. The sum can be larger than a 64-bit integer holds.