Given a sequence of three integers, determine if the sequence is arithmetic (of the form [a, a+d, a+2*d]) or geometric (of the form [a, a*r, a*r^2]) by outputting a fourth term that completes it (a+3*d for arithmetic, a*r^3 for geometric).
Examples:
[1, 2, 3] -> 4 (This is an arithmetic sequence with a difference of 1) [2, 4, 8] -> 16 (This is a geometric sequence with a ratio 2) [20, 15, 10] -> 5 (arithmetic sequence, d=-5) [6, 6, 6] -> 6 (arithmetic with d=0 OR geometric with r=1) [3, -9, 27] -> -81 (geometric with r=-3) - The input is guaranteed to be a valid arithmetic and/or geometric sequence (so you won't have to handle something like
[10, 4, 99]) - None of the inputted terms will be
0(both[2, 0, 0]and[1, 0, -1]would not be given) - For geometric sequences, the ratio between terms is guaranteed to be an integer (ie.
[4, 6, 9]would be an invalid input, as the ratio would be 1.5) - If a sequence could be either arithmetic or geometric, you may output either term that completes it
- You may take input as a 3-term array (or whatever equivalent) or as three separate inputs
- This is code golf, so aim for the lowest byte count possible!