08-16-2024, 12:24 AM
I wrote my program to check all combinations of the above and discovered that the .1 LB could get close, but not exact:
Code: (Select All)
'1 four-pound weight.
'1 two-pound weight.
'1 one-pound weight.
'1 half-pound weight.
'1 quarter-pound weight.
'1 1/8-pound weight.
'And so on to give you one of each weight from 4 pounds to 1/256 pounds.
'
'Now the task is to figure out what weights are required to properly and perfectly weigh the following:
'
'A 3 pound gold nugget
'A 3/8 pound gold nugget
'A 11/32 pound gold nugget
'A 1/10 pound gold nugget
Dim mask$(2500)
Dim w##(20)
'w = weights
w##(1) = 4
w##(2) = 2
w##(3) = 1
w##(4) = .5
w##(5) = .25
w##(6) = .125
w##(7) = .0625
w##(8) = .03125
w##(9) = .015625
w##(10) = .0078125
w##(11) = .00390625
'si = sale items
si##(1) = 3
si##(2) = .375
si##(3) = 11 / 32
si##(4) = .1
'load binary masks
For x = 1 To 2047
mask$(x) = Right$("000000" + Decimal2Binary$(x), 11)
Next x
For itemTOsell% = 1 To 4
For combinationOfWeights% = 1 To 2047
totalWeight## = 0
For bit = 1 To 11
If Mid$(mask$(combinationOfWeights%), bit, 1) = "1" Then
totalWeight## = totalWeight## + w##(bit)
End If
Next bit
If totalWeight## = si##(itemTOsell%) Then
Cls
Print "MATCH##"
Print " itemTOsell%="; itemTOsell%
Print " Total of weights used="; totalWeight##
Print " si##(itemTOsell%)="; si##(itemTOsell%)
Print "Weights to use:"
For z = 1 To 11
If Mid$(mask$(combinationOfWeights%), z, 1) = "1" Then
Print w##(z)
End If
Next z
i$ = "": While i$ = "": i$ = InKey$: Wend
End If
Next combinationOfWeights%
Next itemTOsell%
Function Decimal2Binary$ (number&)
If number& = 0 Then Exit Function
Do
remain% = Abs(number& Mod 2) ' remainder is used to create binary number
number& = number& \ 2 ' move up one exponent of 2 with integer division
BinStr$ = LTrim$(Str$(remain%)) ' make remainder a string number
Binary$ = BinStr$ + Binary$ ' add remainder to binary number
Loop Until number& = 0
Decimal2Binary$ = Binary$ 'binary result
End Function