arrays - find a list of integers for a checksum -
i need list of n positive integers l has properties:
- for each possible subset s of l, if sum items of s, sum not in l
- for each possible subset s of l, if sum items of s, sum unique (each subset can identified sum)
working example 1:
n = 4 l = [1, 5, 7, 9] check: 1+5 = 6 ok 5+7 = 12 ok 7+9 = 16 ok 9+1 = 10 ok 1+7 = 8 ok 5+9 = 14 ok 1+5+7 = 13 ok 5+7+9 = 21 ok 1+5+9 = 15 ok 1+7+9 = 17 ok 1+5+7+9 = 22 ok sums unique -> l ok n = 4
as easy construct sequence, suggest using power series, e.g.
1, 2, 4, 8, ..., 2**k, ... 1, 3, 9, 27, ..., 3**k, ... 1, 4, 16, 64, ..., 4**k, ... ... 1, n, n**2, n**3,..., n**k, ... n >= 2
take, instance, 2
: neither power of 2
sum of other 2
powers; given sum
(number) can find out subset converting sum
binary representation:
23 = 10111 (binary) = 2**0 + 2**1 + 2**2 + 2**4 = 1 + 2 + 4 + 16
in general case, simple greedy algorithm do: given sum
subtract largest item less or equal sum
; continue subtracting zero:
n = 3 sum = 273 273 - 243 (3**5) = 30 30 - 27 (3**3) = 3 3 - 3 (3**1) = 0 273 = 3**5 + 3**3 + 3**1 = 243 + 27 + 3
Comments
Post a Comment