The Final Number
Javascript (ES6), 44 42 44 bytes (fixed)
(a,b,c,d)=>a-b+d-c?d/(a<b?a/b:a/b|0)|0:d+c-b
Saved 2 bytes, following IsmaelMiguel's advice.
Fixed version for [2,1,0,0]
and [1,0,0,0]
as suggested by edc65
30 bytes version
For the record, my first attempt was 32 30 bytes but was lacking floor() support for the division. It also fails for special cases such as [2,1,0,0]
and [1,0,0,0]
.
(a,b,c,d)=>c-2*b+a?d*c/b:d+c-b
Demo
var f =
(a,b,c,d)=>a-b+d-c?d/(a<b?a/b:a/b|0)|0:d+c-b
var test = [
[ 14, 24, 34, 44 ], // should return 54 (addition Algorithm)
[ 105, 45, -15, -75 ], // should return -135 (subtraction algorithm)
[ 5, 25, 125, 625 ], // should return 3125 (multiplicative algorithm)
[ 256, 64, 16, 4 ], // should return 1 (division algorithm)
[ 260, 65, 16, 4 ], // should return 1 (division algorithm with floor())
[ 2, 1, 0, 0 ], // should return 0 (special case of division algorithm)
[ 1, 0, 0, 0 ] // should return 0 (special case of division algorithm)
];
test.forEach(l => console.log('[' + l.join`, `+ '] => ' + f(...l)));
Brachylog, 37 33 27 bytes
b:[E]cL,?:Iz{:+a|:*a|:/a}Lt
Try it online! or verify all test cases.
Saved 10 bytes thanks to @LeakyNun.
Explanation
Input = [A:B:C:D]
b:[E]cL, L = [B:C:D:E]
?:Iz Create the list [[B:I]:[C:I]:[D:I]:[E:I]]
{ Either…
:+a Sum all couples of that list
| or…
:*a Multiply all couples of that list
| or…
:/a Integer divide all couples of that list
}L The result is L
t Output is the last element of L
As LeakyNun pointed out, we don't need the subtraction case because I
can be any integer.
Haskell, 65 bytes
f l@[a,b,c,d]|[a,b..d]==l=d+b-a|z<-b+0^b=div(d*b)$a-mod(max b a)z