-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirregular_triangle_of_n_AND_k.sf
106 lines (84 loc) · 2.5 KB
/
irregular_triangle_of_n_AND_k.sf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/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]