irregular_triangle_of_n_AND_k.sf 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/ruby
  2. # Generate a nice sequence of numbers, generated by an irragular triangle, based on the logical binary equation:
  3. # n AND k = k, where k are values in the range [1, n]
  4. # See also:
  5. # https://oeis.org/A038573
  6. # https://oeis.org/A335063
  7. # https://oeis.org/A048967
  8. # Reading these irregular triangles by rows in a flat list,
  9. # and plotting the list, generates the Sierpiński triangle.
  10. # See also:
  11. # https://en.wikipedia.org/wiki/Sierpiński_triangle
  12. say ":: Irregular triangle of numbers n such that n AND k = k:\n"
  13. var seq_lens = []
  14. var seq_sums = []
  15. for n in (1..20) {
  16. var arr = (1..n -> grep {|k| n&k == k })
  17. seq_lens << arr.len
  18. seq_sums << arr.sum
  19. say ("#{'%2d' % n}: ", arr)
  20. }
  21. say ''
  22. say "Lens: #{seq_lens}"
  23. say "Sums: #{seq_sums}"
  24. say "\n#{'-' * 80}\n"
  25. say ":: Irregular triangle of numbers n such that n AND k != k:\n"
  26. var seq_neg_lens = []
  27. var seq_neg_sums = []
  28. for n in (1..20) {
  29. var arr = (1..n -> grep {|k| n&k != k })
  30. seq_neg_lens << arr.len
  31. seq_neg_sums << arr.sum
  32. say ("#{'%2d' % n}: ", arr)
  33. }
  34. say ''
  35. say "Lens: #{seq_neg_lens}"
  36. say "Sums: #{seq_neg_sums}"
  37. __END__
  38. :: Irregular triangle of numbers n such that n AND k = k:
  39. 1: [1]
  40. 2: [2]
  41. 3: [1, 2, 3]
  42. 4: [4]
  43. 5: [1, 4, 5]
  44. 6: [2, 4, 6]
  45. 7: [1, 2, 3, 4, 5, 6, 7]
  46. 8: [8]
  47. 9: [1, 8, 9]
  48. 10: [2, 8, 10]
  49. 11: [1, 2, 3, 8, 9, 10, 11]
  50. 12: [4, 8, 12]
  51. 13: [1, 4, 5, 8, 9, 12, 13]
  52. 14: [2, 4, 6, 8, 10, 12, 14]
  53. 15: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
  54. 16: [16]
  55. 17: [1, 16, 17]
  56. 18: [2, 16, 18]
  57. 19: [1, 2, 3, 16, 17, 18, 19]
  58. 20: [4, 16, 20]
  59. Lens: [1, 1, 3, 1, 3, 3, 7, 1, 3, 3, 7, 3, 7, 7, 15, 1, 3, 3, 7, 3]
  60. Sums: [1, 2, 6, 4, 10, 12, 28, 8, 18, 20, 44, 24, 52, 56, 120, 16, 34, 36, 76, 40]
  61. --------------------------------------------------------------------------------
  62. :: Irregular triangle of numbers n such that n AND k != k:
  63. 1: []
  64. 2: [1]
  65. 3: []
  66. 4: [1, 2, 3]
  67. 5: [2, 3]
  68. 6: [1, 3, 5]
  69. 7: []
  70. 8: [1, 2, 3, 4, 5, 6, 7]
  71. 9: [2, 3, 4, 5, 6, 7]
  72. 10: [1, 3, 4, 5, 6, 7, 9]
  73. 11: [4, 5, 6, 7]
  74. 12: [1, 2, 3, 5, 6, 7, 9, 10, 11]
  75. 13: [2, 3, 6, 7, 10, 11]
  76. 14: [1, 3, 5, 7, 9, 11, 13]
  77. 15: []
  78. 16: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
  79. 17: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
  80. 18: [1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17]
  81. 19: [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
  82. 20: [1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19]
  83. Lens: [0, 1, 0, 3, 2, 3, 0, 7, 6, 7, 4, 9, 6, 7, 0, 15, 14, 15, 12, 17]
  84. Sums: [0, 1, 0, 6, 5, 9, 0, 28, 27, 35, 22, 54, 39, 49, 0, 120, 119, 135, 114, 170]