The Challenge
Given 3 numbers X
, Y
and Z
in base B
, find a B
ase in which the addition of X
and Y
yields Z
. The inputs x = 20
, Y = 12
and Z = 32
could yield 5
because 20 + 12 = 32
in base 5.
- You may assume that there will always be a base in which the addition is correct (there are cases where no base exists, thanks to @MasonWheeler and @Not that Charles for some examples of that).
- The lowest possible base is 1. You may use 1s or 0s as the digits in unary, but you may not mix those.
I/O
- The digits of the input numbers will be non-negative integers.
- You may assume that the input numbers contain leading zeros, so the have a specific (or all the same) length.
- You may take the numbers in the most convenient format, as long as it's not preprocessed. This includes the overall format of the three input numbers and the format of the digits of each of those numbers. Please make it clear which format you use.
- If there are multiple possible bases, you can output all or just one of them.
- You may assume that the base and the input numbers will be within the numerical limits of your language.
Rules
- Function or full program allowed.
- Default rules for input/output.
- Standard loopholes apply.
- This is code-golf, so lowest byte-count wins. Tiebreaker is earlier submission.
Test cases
Input format here is a list of integers to represent each number. The three lists are separated by commas.
Note that there are sometimes multiple bases possible. Only one (random) solution is outputted here.
[12, 103],[4, 101],[16, 204] -> 349 [4, 21, 25],[5, 1, 20],[9, 23, 17] -> 28 [16, 11],[25, 94],[41, 105] -> 147 [2, 140],[21, 183],[24, 100] -> 223 [8, 157],[1, 28],[9, 185] -> 227 [2, 158],[88],[3, 12] -> 234 [8, 199],[1, 34],[9, 233] -> 408 [3, 247],[7, 438],[11, 221] -> 464 [3, 122],[3, 2],[6, 124] -> 480 [6, 328],[3, 31],[9, 359] -> 465 [2, 1, 0, 0, 0, 0],[1, 2, 0, 0, 1, 0, 1, 0],[1, 2, 2, 1, 1, 0, 1, 0] -> 3 [16, 105],[16, 120],[33, 84] -> 141 [15, 60],[9, 30],[24, 90] -> 268 [2, 0],[1, 2],[3, 2] -> 5 [1, 3, 3, 7],[1, 2, 3],[1, 4, 6, 0] -> 10 [0],[1, 12, 8],[1, 12, 8] -> 16 [1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1],[1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1],[1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0] -> 2 [1],[1],[1,1] -> 1
You can generate additional test cases with this Pyth program. Enter a base on the first line and the decimal values for X
and Y
on the following two lines.
Also you can use this Pyth program to create multiple test cases at once by using random values. Just enter the desired amount of test cases in the input.
Happy Coding!