41
4
Input:
A positive integer n consisting of digits in the range 0-9.
Challenge:
If d is the highest digit in the integer, assume the base of the number is d+1. E.g. if the integer is 1256 then you shall assume it's in base-7, if it's 10110 then you shall assume it's base-2 (binary), and if it's 159 then it's decimal.
Now, do the following until you either, 1: reach a base-10 integer, or 2: reach a single digit integer.
- Convert the integer from base-(d+1) to base-10
- Find the base of this new integer (again, base-(d+1) where d is the highest digit in the new number)
- Go to step 1.
Examples:
Assume the input is n = 413574. The highest digit d=7, so this is base-8 (octal). Convert this to decimal and get 137084. The highest digit d=8, so this is base-9. Convert this to decimal and get 83911. The highest digit is 9, so this is a decimal number and we stop. The output shall be 83911.
Assume the input is n = 13552. The highest digit is d=5, so this is base-6. Convert this to decimal and get 2156. The highest digit d=6, so this is base-7. Convert this to decimal and get 776. The highest digit is d=7, so this is base-8. Convert this to decimal and get 510. The highest digit is d=5 so this is base-6. Convert this to decimal and get 186. The highest digit is 8, so this is base-9. Convert this to decimal and get 159. The highest digit is 9, so this is a decimal number and we stop. The output shall be 159.
Assume the input is n=17. This will give us 15, then 11, then 3, which we will output since it's a single digit.
Test cases:
5
5
17
3
999
999
87654321 (base-9 -> 42374116 in decimal -> base-7 -> 90419978 in decimal)
9041998
41253 (5505 -> 1265 -> 488 -> 404 -> 104 -> 29)
29
Notes:
- Standard rules regarding I/O, loopholes etc. You may take the input as a string
- Explanations are encouraged
- You may use builtin base-conversion commands
- Solutions that don't use the language's builtin base-conversion functions (if they exist) are welcome, even if they end up being much longer than the obvious approach using builtin functions.
Apparently, this is OEIS A091047.
2Apparently I love matrices, so I thought I'd do a base-conversion challenge instead. – Stewie Griffin – 2017-06-28T07:27:33.247
35"The best base is 10"... "10" is written in which base? – Olivier Grégoire – 2017-06-28T08:23:49.610
5@OlivierGrégoire That's the clever thing... Whatever base we end up with, it will still be a valid statement! – Stewie Griffin – 2017-06-28T08:29:42.253
@StewieGriffin But how do you read it? "10" has a different name in each base. – user11153 – 2017-06-28T09:23:13.470
10Well, that depends on the base... or as Meghan would say it "it's all about that base, 'Bout that base, no trouble" ;-) – Stewie Griffin – 2017-06-28T09:38:53.980
Now tell me why there are infinitely many numbers which terminate in a single digit... – M.Herzkamp – 2017-06-28T14:40:26.043
-1. Clearly 13 is the best base!! ;) – Tom Carpenter – 2017-06-29T15:54:47.477
@StewieGriffin For a guy so into matrices, this must have been a change of base :) – Joe – 2017-10-11T21:05:38.807
"10" is written in decimal. – user75200 – 2017-12-28T16:02:11.600
@user11153 I'm pretty sure
10
interpreted in base $n$ is $n$: the0
represents $0n^0 = 0$, and the1
represents $1n^1 = n$. – Solomon Ucko – 2019-06-06T22:10:30.093@SolomonUcko "10" is named "ten" only in one base. – user11153 – 2019-06-07T10:38:17.677
@user11153 Exactly? I was saying that the name of
10
in each base is that base. For example,10
in base two is "two". – Solomon Ucko – 2019-06-07T10:44:12.093