Find The Sum of the First n Bouncy Numbers
Jelly, 10 8 bytes
ṢeṚƬ¬µ#S
Try it online!
How it works
ṢeṚƬ¬µ#S Main link. No arguments.
# Read an integer n from STDIN and call the chain to the left with argument
k = 0, 1, 2, ... until n of them return a truthy result.
Yield the array of successful values of k.
µ Monadic chain. Argument: k (integer)
Ṣ Sort, after promoting k to its digit array.
ṚƬ Reverse 'til the results are no longer unique and yield unique results.
Calling Ṛ on k promotes it to its digit array. If k = 14235, the
result is [14235, [5,3,2,4,1], [1,4,2,3,5]].
e Check if the result to the left appears in the result to the right.
¬ Negate the resulting Boolean.
S Take the sum.
Pyth, 10 bytes
s.f!SI#_B`
Try it here!
How it works?
s.f!SI#_B` – Full program. Takes an integer Q from STDIN and outputs to STDOUT. .f – Find the first Q positive integers that satisfy a certain condition. !SI#_B – The condition. Returns true for bouncy numbers only. _B` – Cast the number to a string and bifurcate (pair) it with its reverse. # – Filter-keep those... I – That are invariant under... S – Sorting. – To clarify, I (invariant) is a Pyth operator that takes two inputs, a function and a value and checks whether function(value) == value, so this is technically not a built-in. ! – Logical not. The empty list gets mapped to true, other values to false. s – Sum.
K (ngn/k), 37 bytes
{+/x{{(a~&\a)|a~|\a:10\x}(1+)/x+1}\0}
Try it online!
{
}
is a function with argument x
x{
}\0
applies the {}
on 0
x
times, preserving the intermediate results
(1+)
is the successor function
{
}(1+)/x+1
applies the successor function starting from x+1
until the {}
returns true
10\x
are the decimal digits of x
a:
assign to a
|\
is the max-scan (partial maxima) of a
&\
analogously, is the min-scan
a~|\a
does a
match its max-scan?
|
or
a~&\a
its min-scan?
+/
sum