Perl, 43 bytes
map{say if$_==eval s/./+$&**$+[0]/gr}<>..<>
Try it online!
Regex is really powerful, you guys.
Explanation
The first thing the code does is read two integers as input via <>
, and creates a range from the first to the second with ..
. It then uses the standard map
function to iterate through this range, and applies the following code to each value: say if$_==eval s/./+$&**$+[0]/gr
. This looks like gibberish, and it kind of is, but here's what's really happening.
map
implicitly stores its current value in the variable $_
. Many perl functions and operations use this value when none is given. This includes regular expressions, such as the s///
substitution operator.
There are four parts to a substitution regex:
- String to be manipulated. Ordinarily, the operator
=~
is used to apply a regex to a string, but if this operator is absent, then the regex is applied to the implicit variable $_
, which contains our current number via the map
function.
- String to search for. In this case, we're searching for any single non-newline character, denoted by the wildcard
.
. In effect, we're capturing each individual digit.
- String to replace with. We're substituting a plus sign
+
followed by a mathematical expression, mixed in with some magical Perl variables that make everything significantly easier.
The special scalar variable $&
always contains the entirety of the last successful regex capture, which in this case is a single digit. The special array variable @+
always contains a list of postmatch offsets for the last successful match, i.e. the index of the text after the match. $+[0]
is the index in $_
of the text immediately following $&
. In the case of 135
, we capture the digit 1
, and the index in 135
of the text immediately afterwards (namely, 35
) is 1, which is our exponent. So, we want to raise $&
(1) to the power of $+[0]
(1) and get 1. We want to raise 3 to the power of 2 and get 9. We want to raise 5 to the power of 3 and get 125.
If the input was 135
, the resulting string is +1**1+3**2+5**3
.
- Regex-modifying flags. Here we're using two regex flags --
/g
and /r
. /g
tells the interpreter to continue replacements after the first is found (otherwise we'd end up with +1**135
). /r
tells the interpreter not to modify the original string, and instead return what the string would be after the replacements. This is important, because otherwise, it would overwrite $_
, and we need it for comparison purposes.
Once the entire substitution is done, we get a mathematical expression, which is evaluated with the eval
function. +1**1+3**2+5**3
is evaluated into 1 + 9 + 125 = 135
, which is compared to the original number 135
. Since these two are equal, the code prints the number.
n
andm
? There’s a very big disarium (12157692622039623539), should answers be able to identify it? \$\endgroup\$