prog.sf 848 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/ruby
  2. # Number of 0's minus number of 1's among the edge truncated binary representations of the first n positive integers.
  3. # Cf. A037861, A301336, A308430.
  4. # Formula:
  5. # a(n) = Sum_{k=2..n} (A037861(n) + (1 - (-1)^n))
  6. # (PARI) a(n) = sum(k=2, n, #binary(k) - 2*hammingweight(k) + (1 - (-1)^k))
  7. # See also:
  8. # https://oeis.org/A037861
  9. # https://oeis.org/A301336
  10. # https://oeis.org/A308430
  11. func A037861(n) {
  12. var t = n.as_bin
  13. (t.count('0') - t.count('1'))
  14. }
  15. func f(n) {
  16. # var t = n.as_bin
  17. # (t.count('0') - t.count('1')) #+ (n.is_odd ? 2 : 0)
  18. if (n <= 1) {
  19. return 0
  20. }
  21. # n.is_odd ? 2+A037861(n) : A037861(n)
  22. #A037861(n) + (1 - (-1)**n)
  23. A037861(n) + (1 - (-1)**n)
  24. }
  25. 101.of(f).accumulate.each{.say}
  26. # Program for A301336:
  27. # a(n) = sum(k=2, n, 2*hammingweight(k) - #binary(k)); \\ for n >0