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

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


-20% If your answer outputs a simplified version of the formula; for example, it prints


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.

  • 1
    \$\begingroup\$ Near-duplicate (not quite, though): codegolf.stackexchange.com/q/37928/3808 \$\endgroup\$
    – Doorknob
    Jan 7, 2016 at 2:03
  • \$\begingroup\$ Will the input always have four terms? \$\endgroup\$
    – Doorknob
    Jan 7, 2016 at 2:12
  • \$\begingroup\$ Not necessarily, I'll make that clearer in the question. \$\endgroup\$ Jan 7, 2016 at 2:12
  • 1
    \$\begingroup\$ Related: codegolf.stackexchange.com/q/65664/42545 \$\endgroup\$ Jan 7, 2016 at 2:34
  • 1
    \$\begingroup\$ Should "The input will be either a geometric sequence or an arithmetic sequence, nothing else" be taken to mean that the input is never both a geometric and an arithmetic sequence (i.e. a constant sequence)? What type are the input elements? The test cases imply that they should be a built-in rational type, but for languages which don't have (i.e. most of them), can they substitute e.g. a two-element array of [numerator denominator]? \$\endgroup\$ Jan 7, 2016 at 12:26

3 Answers 3


Mathematica, 75 * 0.8 - 30 = 30 bytes


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 *)

𝔼𝕊𝕄𝕚𝕟 2, 38 chars / 57 bytes


Try it here (Firefox only).

First answer! I'm still working on bonuses.


î+(ì-í≔í-î?`+⟮(n-1)`⟯+(í-î):`(⦃í/î})^Ⅰ // implicit: î=input1, í=input2, ì=input3
î+                                    // Prepend a1
  (ì-í≔í-î                            // Is rate of change constant from term to term?
          ?`+⟮(n-1)`⟯+(í-î)            // arithmetic mean template
                          :`(⦃í/î})^Ⅰ // geometric mean template
                                      // implicit output

NOTE: and are copy-paste blocks; anything within those blocks is stored to be called for later pasting (using ).

  • \$\begingroup\$ It should be ^(n-1). \$\endgroup\$ Jan 7, 2016 at 13:02
  • \$\begingroup\$ @LegionMammal978 Fixed. \$\endgroup\$ 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 t exists prior to calling this? You must either take it as input or use a function (lambda). \$\endgroup\$ 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
    Apr 28, 2018 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.