fusc_function.sf 533 B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/ruby
  2. # Simple (and efficient) implementation of the "fusc" function.
  3. # See also:
  4. # https://oeis.org/A002487
  5. # https://en.wikipedia.org/wiki/Calkin–Wilf_tree
  6. func fusc(n) {
  7. var (a=1, b=0)
  8. if (n.is_even) {
  9. n.remove!(2)
  10. }
  11. for bit in (n.as_bin.chars) {
  12. if (bit) {
  13. b += a
  14. }
  15. else {
  16. a += b
  17. }
  18. }
  19. return b
  20. }
  21. say 30.of(fusc)
  22. __END__
  23. [0, 1, 1, 2, 1, 3, 2, 3, 1, 4, 3, 5, 2, 5, 3, 4, 1, 5, 4, 7, 3, 8, 5, 7, 2, 7, 5, 8, 3, 7]