The purpose of this challenge is to find the general term of a given arithmetic or geometric sequence.
Explanation
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.
Challenge
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
Test cases
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
Bonus
-20% If your answer outputs a simplified version of the formula; for example, it prints
6n-2
rather than
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.
3 Answers 3
Mathematica, 75 * 0.8 - 30 = 30 bytes
f=Simplify[FindSequenceFunction@{If[2#2==#+#3,2#-#2,#^2/#2],##}/@{n+1,21}]&
Test cases:
f[-2, 0, 2, 4, 6, 8]
(* {2 (-2 + n), 36} *)
f[27, 9, 3, 1, 1/3]
(* {3^(4 - n), 1/43046721} *)
f[1, 1, 1]
(* {1, 1} *)
In Mathematica, FindSequenceFunction is a very interesting function.
FindSequenceFunction[{1, 1, 2, 3, 5, 8, 13}, n]
(* Fibonacci[n] *)
FindSequenceFunction[{1+a, 1+a^2, 1+a^3, 1+a^4}, n] (* symbolic! *)
(* 1 + a^n *)
ESMin 2, 38 chars / 57 bytes
î+(x=ì-í≔í-î?`+⟮(n-1)`⟯+(í-î):`(⦃í/î})^I
First answer! I'm still working on bonuses.
Explanation
î+(ì-í≔í-î?`+⟮(n-1)`⟯+(í-î):`(⦃í/î})^I // implicit: î=input1, í=input2, ì=input3
î+ // Prepend a1
(ì-í≔í-î // Is rate of change constant from term to term?
?`+⟮(n-1)`⟯+(í-î) // arithmetic mean template
:`(⦃í/î})^I // geometric mean template
// implicit output
NOTE: ⟮ and ⟯ are copy-paste blocks; anything within those blocks is stored to be called for later pasting (using I).
-
\$\begingroup\$ It should be
^(n-1). \$\endgroup\$LegionMammal978– LegionMammal9782016年01月07日 13:02:26 +00:00Commented Jan 7, 2016 at 13:02 -
\$\begingroup\$ @LegionMammal978 Fixed. \$\endgroup\$Mama Fun Roll– Mama Fun Roll2016年01月07日 14:38:03 +00:00Commented Jan 7, 2016 at 14:38
Python, 130* 0.8 - 30 = 74 bytes
Been a while since I last answer a question and this one has been sitting in my favourites for some time!
Started at 188
def m(t):s=str;a=t[0];b=t[1];r=b/a;q=b-a;return((s(q)+'n'+'+'*(0<a-q)+s(a-q),a+q*19),(s(b/r)+'('+s(r)+')^n-1',b*r**18))[t[2]/b==r])
Uses school maths to determine the coeffs and sticks them together and returns the 20th term.
Sample output: 3n-1 59, where 59 is the 20th term.
-
\$\begingroup\$ Which Python version is this? Also, it looks like you assume that the variable
texists prior to calling this? You must either take it asinputor use a function (lambda). \$\endgroup\$Stewie Griffin– Stewie Griffin2018年04月28日 20:41:54 +00:00Commented Apr 28, 2018 at 20:41 -
\$\begingroup\$ @StewieGriffin python 3.6, its now a function that takes a list and returns the string \$\endgroup\$george– george2018年04月28日 20:55:09 +00:00Commented Apr 28, 2018 at 20:55
[numerator denominator]? \$\endgroup\$