Input:
A list/array of integers for which each item is in the range of 2-36.
Output:
The sum of the integers (as base 10), where each next integer is in the base of the previous value (starting with a regular base 10).
Example:
Let's say we have an input like this: [4, 12, 34, 20, 14, 6, 25, 13, 33]
Then we have a sum like this:
4 (4 in base-10) + 6 (12 in base-4 ) + 40 (34 in base-12) + 68 (20 in base-34) + 24 (14 in base-20) + 6 (6 in base-14) + 17 (25 in base-6 ) + 28 (13 in base-26) + 42 (33 in base-13) = 235 Mathematical base explained:
I considered assuming everyone knows how base works, but I'll give a brief example of how it works anyway, just in case. Let's take the 34 in base-12 for example, how did we get 40?
1-34 in regular base-10: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34 So, from 1 to 34 is 34 steps in base-10 1-34 in base-12: 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1A, 1B, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 2A, 2B, 30, 31, 32, 33, 34 So, from 1 to 34 is 40 steps in base-12 Here is perhaps a useful calculator.
Challenge rules:
- Array size will be in reasonable range (like
1-100/ see test cases). - The test cases will never contain integers of which the current value is invalid for it's previous base (i.e. You will never have something like
19 in base-6or6 in base-6, because base-6 only contains the digits0-5). - You can take the input any way you'd like. Can be as an int-array, as a comma/space-separated string, etc. Your call. (You are also allowed to take the int-array reversed, which could be useful for stack-based programming languages.)
General Rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-golfing languages. Try to come up with as short an answer as possible for any programming language. - Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code.
- Also, please add an explanation if necessary.
Test cases:
[4, 12, 34, 20, 14, 6, 25, 13, 33] -> 235 4+ 6+ 40+ 68+ 24+ 6+ 17+ 28+ 42 [5, 14, 2, 11, 30, 18] -> 90 5+ 9+ 2+ 3+ 33+ 38 [12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 12, 2, 11, 3, 10, 2, 10] -> 98 12+ 13+ 11+ 9+ 8+ 7+ 6+ 5+ 4+ 3+ 5+ 2+ 3+ 3+ 3+ 2+ 2 [36, 36] -> 150 36+ 114