# [Red], 108 bytes func[n][i: s: 0 until[t: form i if(t > u: sort copy t)and(t < reverse u)[s: s + i n: n - 1]i: i + 1 0 = n]s] [Try it online!][TIO-jjb35768] [Red]: http://www.red-lang.org [TIO-jjb35768]: https://tio.run/##PYzBCgIhFEX3fsVdNkSgMBBI9RFtxUWMCg/qOTyfQV9vrmZ3OJdzJafxzClEU/wonbfAMZBH87DorPQO6lGqfECGyknxQJ9zFcVW9x90eXGa@gbJ3ywtoy9h1g1nENiDcYGL85KmccbiDo4tjl2IFWWqg@yB63q1bvwB "Red – Try It Online" More readable: - f: func [ n ] [ i: s: 0 until [ t: form i if ( t > u: sort copy t ) and ( t < reverse u ) [ s: s + i n: n - 1 ] i: i + 1 0 = n ] s ] A good opportunity to use `form` - `form i` is 5 bytes shorter than `to-string i`