The purpose of this challenge is to find the general term of a given arithmetic or geometric sequence.
The formula for an arithmetic sequence is as follows
an = a1 + (n-1)d
where a1 is the first term of the sequence, n is the index of the term, and d is the common difference between the terms. For a geometric sequence, the formula for the general term is
an = a1rn-1
where r is the common ratio between the terms of the sequence.
The objective of this challenge is to accept an input of a sequence of numbers, determine whether the sequence is arithmetic or geometric, and then output the formula for the general term. The rules are
- The input will be either a geometric sequence or an arithmetic sequence, nothing else
- The formula can either be returned from a function, or printed to STDOUT or an equivalent
- The input can be taken in any form that is convenient; for example, it could be an argument, taken from STDIN, or hardcoded if the language offers no other way of accepting input
- The input will always be in the format
[3, 4, 5, 6], with brackets surrounding comma-separated terms. The spaces separating the terms and the commas are optional. The input will always contain three or more terms.
- The shortest code in bytes wins
Input Output [-2, 0, 2, 4, 6, 8] -2 + 2(n-1) or an equivalent expression [27, 9, 3, 1, 1/3] 27(1/3)^n-1 or an equivalent expression [200, 240, 280, 320] 200 + 40(n-1) or an equivalent expression [2, 6, 18] 2(3)^n-1 or an equivalent expression
-20% If your answer outputs a simplified version of the formula; for example, it prints
4 + 6(n-1)
-30 bytes If your answer prints the 20th term of the sequence, in addition to the general term.
If your answer satisfies both of these conditions, the 20% is removed first, and then the 30 bytes are removed.