Determine the Base where a Given Equation is True
APL (Dyalog Unicode), 30 bytesSBCS
⊢{3⊃e=×/2↑e←⍵⊥⍺:⍵⋄⍺∇⍵+1}1+⌈/∘,
Try it online!
Thanks to Adám for the help.
Explanation:
⊢{3⊃e=×/2↑e←⍵⊥⍺:⍵⋄⍺∇⍵+1}1+⌈/∘, ⍝
⊢ ⍝ left argument ⍺: the vector (do nothing)
1+⌈/∘, ⍝ right argument ⍵: our starting base.
, ⍝ start by flattening the matrix of arguments ⌈/ ⍝ reduce by max (find the highest number)
∘ ⍝ compose both of these together
1+ ⍝ increment by one
{ ⍵⊥⍺ } ⍝ convert inputs to the current base
{ e← } ⍝ store the converted values in 3
{ 2↑ } ⍝ take the first 2 values
{ ×/ } ⍝ multiply them together (reduce-multiply)
{ e= } ⍝ compare with e (the converted inputs)
{3⊃ } ⍝ only keep the result of the comparison with the 3rd element (expected result)
{ :⍵ } ⍝ if truthy, return the current base.
{ ⋄ } ⍝ otherwise...
{ ⍺∇⍵+1} ⍝ ...recurse with the base incremented
We use a helper function, In
, to receive the input into a more palatable format. Otherwise the input is received a matrix of 3 columns.
'3 9 42'
would give, for example (read top-down then left-to-right):
0 0 4
3 9 2
And for 'aA bB 36jk'
(same here. a
is 10, b
is 11, A
is 36, etc)
0 0 3
0 0 6
10 11 19
36 37 20
CJam, 52 51 48 bytes
63,{_ea{i32b~\([G-35-9]=-_Xe>:X;}f%fbW%~*=\X>*}#
Test it here. The online tester doesn't support input via ARGV. The closest alternative is to put put the input like 6 9 42
into STDIN and use:
lS/:E;
63,{_E{i32b~\([G-35-9]=-_Xe>:X;}f%fbW%~*=\X>*}#
This prints -1
if no valid base up to 62 can be found.
Many thanks to Peter for the digit parsing code!
I fixed a lot of problems which added 14 bytes to the count. The following explanation is still for my original submission, and I'll update it some time tomorrow.
63,{_ea{i32b~\([G-35-9]=-_Xe>:X;}f%fbW%~*=\X>*}#
63, "Push the array [0 1 .. 62].";
{ }# "Find the first index for which the block returns
a truthy value.";
_ "Duplicate the current base.";
ea "Read ARGV into an array of strings.";
{ }f% "Apply this block to each character.";
i32b "Convert to code point, and then to base-32. The
most significant digit now identifies the 'type'
of digit.";
~\( "Unwrap the array. Swap the digits. Decrement.";
[G-35-9] "Push array [16 -35 -9] of digit offsets.";
=- "Select the relevant offset and subtract it from
the least significant digit.";
_ "Duplicate the current digit D.";
Xe>:X; "X := max(X,D). X is predefined as 1.";
fb "Convert all numbers to the current base.";
W% "Reverse the list of numbers.";
~ "Unwrap the array.";
*= "Multiply factors. Check equality with product.";
\ "Swap result with current base.";
X> "Ensure base is greater than X.";
* "Multiply boolean results.";
The index is printed automatically at the end of the program.
CJam, 53 bytes
lA,s'{,97>+'[,65>+f#_$W=1e>)63,>_@Wa/W%f{fb~*=}1#\0+=
Takes the three input from STDIN like
6 9 42
Prints 0
if product in any base is not possible
Will try to golf it further.
Try it here