The problem is symmetric in the sense that you could do the same weighing by flipping the weights at the two sides.

Solutions such as (16, 16, 6, 2) and (17, 15, 6, 2) and 42 others provided by Filip and David seem incorrect.

0 -1 -1 -1 -1 -40

1 -1 -1 -1 0 -39

2 -1 -1 -1 1 -38

3 -1 -1 0 -1 -37

4 -1 -1 0 0 -36

5 -1 -1 0 1 -35

6 -1 -1 1 -1 -34

7 -1 -1 1 0 -33

8 -1 -1 1 1 -32

9 -1 0 -1 -1 -31

10 -1 0 -1 0 -30

11 -1 0 -1 1 -29

12 -1 0 0 -1 -28

13 -1 0 0 0 -27

14 -1 0 0 1 -26

15 -1 0 1 -1 -25

16 -1 0 1 0 -24

17 -1 0 1 1 -23

18 -1 1 -1 -1 -22

19 -1 1 -1 0 -21

20 -1 1 -1 1 -20

21 -1 1 0 -1 -19

22 -1 1 0 0 -18

23 -1 1 0 1 -17

24 -1 1 1 -1 -16

25 -1 1 1 0 -15

26 -1 1 1 1 -14

27 0 -1 -1 -1 -13

28 0 -1 -1 0 -12

29 0 -1 -1 1 -11

30 0 -1 0 -1 -10

31 0 -1 0 0 -9

32 0 -1 0 1 -8

33 0 -1 1 -1 -7

34 0 -1 1 0 -6

35 0 -1 1 1 -5

36 0 0 -1 -1 -4

37 0 0 -1 0 -3

38 0 0 -1 1 -2

39 0 0 0 -1 -1

40 0 0 0 0 0

41 0 0 0 1 1

42 0 0 1 -1 2

43 0 0 1 0 3

44 0 0 1 1 4

45 0 1 -1 -1 5

46 0 1 -1 0 6

47 0 1 -1 1 7

48 0 1 0 -1 8

49 0 1 0 0 9

50 0 1 0 1 10

51 0 1 1 -1 11

52 0 1 1 0 12

53 0 1 1 1 13

54 1 -1 -1 -1 14

55 1 -1 -1 0 15

56 1 -1 -1 1 16

57 1 -1 0 -1 17

58 1 -1 0 0 18

59 1 -1 0 1 19

60 1 -1 1 -1 20

61 1 -1 1 0 21

62 1 -1 1 1 22

63 1 0 -1 -1 23

64 1 0 -1 0 24

65 1 0 -1 1 25

66 1 0 0 -1 26

67 1 0 0 0 27

68 1 0 0 1 28

69 1 0 1 -1 29

70 1 0 1 0 30

71 1 0 1 1 31

72 1 1 -1 -1 32

73 1 1 -1 0 33

74 1 1 -1 1 34

75 1 1 0 -1 35

76 1 1 0 0 36

77 1 1 0 1 37

78 1 1 1 -1 38

79 1 1 1 0 39

80 1 1 1 1 40 ]]>

https://gist.github.com/1890672

Pasting here for easy reference, though the format may not be as nice:

import scala.math._

def stone_weights(w: Int) : List[Int] = w match {

case 1 => List(1)

case _ =>

val one_third : Int = ceil((w-1).toDouble/3).toInt

w – one_third :: stone_weights (one_third)

}

println(stone_weights(40));

This function should work for any given positive parameter.

The solution relies on the function stone_weights to return a list of the minimum number of stone weights, that can make up to all weights between 1 and w.

The function itself calculates the list using the following logic:

1. if w = 1, the answer is the list [1]

2. otherwise, the answer is the number at 2 thirds of w, appended to a list of the stone_weights of (one third of w)

Think of the solution as dividing the target weight into 3 portions. If we had a set of stone pieces that represent all the weights up to a third of that weight, then we can combine this set with a stone weighing two thirds of the target weight, to be able to represent all the wights up to the desired weight.

The solution requires a bit of rounding and +/- 1, to provide the exact coverage.

Feedback welcome. ]]>

Well… yes, that’s a big cheat. What if the total is 41? Might as well “return new int[] { the solution }” ðŸ™‚

]]>int total = 40

def result = []

int next = 1

int sum = next

while( sum <= 40 ) {

result << next

next = 2 * sum + 1

sum += next

}

println result ]]>