Calkin-Wilf sequence

func calkin_wilf(n) is cached {
    return 1 if (n == 1)
    1/(2*floor(__FUNC__(n-1)) + 1 - __FUNC__(n-1))
}

func r2cw(r) {

    var cfrac = r.as_cfrac
    cfrac.len.is_odd || return nil

    Num(cfrac.flip.map_kv {|k,v| (k.is_odd ? '0' : '1') * v }.join, 2)
}

with (20) {|n|
    say "First #{n} terms of the Calkin-Wilf sequence:"
    say calkin_wilf.map(1..n)
}

with (83116/51639) {|r|
    say ("\n#{r.as_rat} is at index: ", r2cw(r))
}

Output:

Last updated

Was this helpful?