Find the sequence

2

1

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.

Chris Loonam

Posted 2016-01-07T02:00:26.907

Reputation: 585

1

Near-duplicate (not quite, though): http://codegolf.stackexchange.com/q/37928/3808

– Doorknob – 2016-01-07T02:03:53.760

Will the input always have four terms? – Doorknob – 2016-01-07T02:12:04.153

Not necessarily, I'll make that clearer in the question. – Chris Loonam – 2016-01-07T02:12:31.730

1

Related: http://codegolf.stackexchange.com/q/65664/42545

– ETHproductions – 2016-01-07T02:34:50.077

Also related (but much more complicated) – Peter Taylor – 2016-01-07T11:14:21.210

1Should "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]? – Peter Taylor – 2016-01-07T12:26:40.780

Answers

1

2, 38 chars / 57 bytes

î+(x=ì-í≔í-î?`+⟮(n-1)`⟯+(í-î):`(⦃í/î})^Ⅰ

Try it here (Firefox only).

First answer! I'm still working on bonuses.

Explanation

î+(ì-í≔í-î?`+⟮(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 ).

Mama Fun Roll

Posted 2016-01-07T02:00:26.907

Reputation: 7 234

It should be ^(n-1). – LegionMammal978 – 2016-01-07T13:02:26.613

@LegionMammal978 Fixed. – Mama Fun Roll – 2016-01-07T14:38:03.920

1

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

njpipeorgan

Posted 2016-01-07T02:00:26.907

Reputation: 2 992

0

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.

george

Posted 2016-01-07T02:00:26.907

Reputation: 1 495

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). – Stewie Griffin – 2018-04-28T20:41:54.853

@StewieGriffin python 3.6, its now a function that takes a list and returns the string – george – 2018-04-28T20:55:09.553