Numbers with equal rises and falls

func isok(arr) {
    var diffs = arr.map_cons(2, {|a,b| a - b })
    diffs.count { .is_pos } == diffs.count { .is_neg }
}

var base = 10

with (200) {|n|
    say "First #{n} terms (base #{base}):"
    n.by { isok(.digits(base)) && .is_pos }.each_slice(20, {|*a|
        say a.map { '%3s' % _ }.join(' ')
    })
}

with (1e7) {|n|     # takes a very long time
    say "\nThe #{n.commify}-th term (base #{base}): #{
            n.th { isok(.digits(base)) && .is_pos }.commify}"
}

Output:

Last updated

Was this helpful?