123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #!/usr/bin/ruby
- # Generate a nice sequence of numbers, generated by an irragular triangle, based on the logical binary equation:
- # n AND k = k, where k are values in the range [1, n]
- # See also:
- # https://oeis.org/A038573
- # https://oeis.org/A335063
- # https://oeis.org/A048967
- # Reading these irregular triangles by rows in a flat list,
- # and plotting the list, generates the Sierpiński triangle.
- # See also:
- # https://en.wikipedia.org/wiki/Sierpiński_triangle
- say ":: Irregular triangle of numbers n such that n AND k = k:\n"
- var seq_lens = []
- var seq_sums = []
- for n in (1..20) {
- var arr = (1..n -> grep {|k| n&k == k })
- seq_lens << arr.len
- seq_sums << arr.sum
- say ("#{'%2d' % n}: ", arr)
- }
- say ''
- say "Lens: #{seq_lens}"
- say "Sums: #{seq_sums}"
- say "\n#{'-' * 80}\n"
- say ":: Irregular triangle of numbers n such that n AND k != k:\n"
- var seq_neg_lens = []
- var seq_neg_sums = []
- for n in (1..20) {
- var arr = (1..n -> grep {|k| n&k != k })
- seq_neg_lens << arr.len
- seq_neg_sums << arr.sum
- say ("#{'%2d' % n}: ", arr)
- }
- say ''
- say "Lens: #{seq_neg_lens}"
- say "Sums: #{seq_neg_sums}"
- __END__
- :: Irregular triangle of numbers n such that n AND k = k:
- 1: [1]
- 2: [2]
- 3: [1, 2, 3]
- 4: [4]
- 5: [1, 4, 5]
- 6: [2, 4, 6]
- 7: [1, 2, 3, 4, 5, 6, 7]
- 8: [8]
- 9: [1, 8, 9]
- 10: [2, 8, 10]
- 11: [1, 2, 3, 8, 9, 10, 11]
- 12: [4, 8, 12]
- 13: [1, 4, 5, 8, 9, 12, 13]
- 14: [2, 4, 6, 8, 10, 12, 14]
- 15: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
- 16: [16]
- 17: [1, 16, 17]
- 18: [2, 16, 18]
- 19: [1, 2, 3, 16, 17, 18, 19]
- 20: [4, 16, 20]
- Lens: [1, 1, 3, 1, 3, 3, 7, 1, 3, 3, 7, 3, 7, 7, 15, 1, 3, 3, 7, 3]
- Sums: [1, 2, 6, 4, 10, 12, 28, 8, 18, 20, 44, 24, 52, 56, 120, 16, 34, 36, 76, 40]
- --------------------------------------------------------------------------------
- :: Irregular triangle of numbers n such that n AND k != k:
- 1: []
- 2: [1]
- 3: []
- 4: [1, 2, 3]
- 5: [2, 3]
- 6: [1, 3, 5]
- 7: []
- 8: [1, 2, 3, 4, 5, 6, 7]
- 9: [2, 3, 4, 5, 6, 7]
- 10: [1, 3, 4, 5, 6, 7, 9]
- 11: [4, 5, 6, 7]
- 12: [1, 2, 3, 5, 6, 7, 9, 10, 11]
- 13: [2, 3, 6, 7, 10, 11]
- 14: [1, 3, 5, 7, 9, 11, 13]
- 15: []
- 16: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
- 17: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
- 18: [1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17]
- 19: [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
- 20: [1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19]
- Lens: [0, 1, 0, 3, 2, 3, 0, 7, 6, 7, 4, 9, 6, 7, 0, 15, 14, 15, 12, 17]
- Sums: [0, 1, 0, 6, 5, 9, 0, 28, 27, 35, 22, 54, 39, 49, 0, 120, 119, 135, 114, 170]
|