Esthetic numbers
func generate_esthetic(root, upto, callback, b=10) {
var v = root.digits2num(b)
return nil if (v > upto)
callback(v)
var t = root.head
__FUNC__([t+1, root...], upto, callback, b) if (t+1 < b)
__FUNC__([t-1, root...], upto, callback, b) if (t-1 >= 0)
}
func between_esthetic(from, upto, b=10) {
gather {
for k in (1..^b) {
generate_esthetic([k], upto, { take(_) if (_ >= from) }, b)
}
}.sort
}
func first_n_esthetic(n, b=10) {
for (var m = n**2; true ; m *= b) {
var list = between_esthetic(1, m, b)
return list.first(n) if (list.len >= n)
}
}
for b in (2..16) {
say "\n#{b}-esthetic numbers with indices #{4*b}..#{6*b}: "
say first_n_esthetic(6*b, b).last(6*b - 4*b + 1).map{.base(b)}.join(' ')
}
say "\nBase 10 esthetic numbers between 1,000 and 9,999:"
between_esthetic(1e3, 1e4).slices(20).each { .join(' ').say }
say "\nBase 10 esthetic numbers between 100,000,000 and 130,000,000:"
between_esthetic(1e8, 13e7).slices(9).each { .join(' ').say }Output:
Last updated
Was this helpful?