RetinaRetina 0.8.2, 151134 bytes
.+
,$&,$&*$&$*
+`,(.)*,((_+1*?)((?=\3+$=\2+$)(?<=(?=(?<-1>(?=((_*1*)(?=\3\5+$=\2\5+$)_1)(\4*$))\6)+_$+1$)\3\2).*|(?<-1>(_+))(?<-1>\71>\2)+|(?<-1>_1>1)+(_*))$(?(1)_1)
_1,$.($3$7$8)2,$3$7$8$2
_1
Try it online!Try it online! Link includes faster test cases. Explanation:
.+
,$&,$&*$&$*
Create a working area consisting of the result (initially 0), the input, and the input converted to unary.
+`
Repeat until the input has been reduced to zero ...
,(.)*,(1*?)(
count the number of digits d
in the value n
, and then matchfind the smallest value that satisfies one of ...
(_+)(?=\3+$=\2+$)(?<=(?=(?<-1>(?=((_*1*)(?=\3\5+$=\2\5+$)_1)(\4*$))\6)+_$+1$)\3\2).*|
... theits d
th root ofpower is n
, if it exists, or ...
(?<-1>(_+))(?<-1>\71>\2)+|
... its product with n/d
, if it exists is n
, or ...
(?<-1>_1>1)+(_*))
... its sum with n-d
is n
, and ...
$(?(1)_1)
ensure that it was in fact d
and not some smaller integer, and ...
_1,$.($3$7$8)2,$3$7$8$2
increment the output and replace n
and its unary with whichever match produced athe result.
_1
Convert the output to decimal.