'The Bag of Marbles Phenomena [closed]

QUESTION:

I have a bag of marbles with a total volume of 33. There are two kinds of marbles in my bag. Blue marbles that have a volume of 3 and red marbles that have a volume of 6. If I have 8 marbles in the bag, how many blue and red marbles do I have?

I have been trying to create a code that tells me how many red and blue marbles there are but I cant seem to solve this. Any Ideas?

For this specific problem there should be 5 Blue and 3 Red.



Solution 1:[1]

You could use sympy to find formulas for this type of equations (and even much more complicated ones). Just write down all the equations and call solve.

Here is an example.

from sympy import var, Eq, solve

var('total_volume volume_blue volume_red num_marbles num_blue num_red')
sol = solve([Eq(num_blue + num_red, num_marbles),
             Eq(num_blue * volume_blue + num_red * volume_red, total_volume)],
            [num_blue, num_red])
values = {total_volume: 33, volume_blue: 3, volume_red: 6, num_marbles: 8}
print(f'Formula for num_blue: {sol[num_blue]} ({sol[num_blue].subs(values)} for the given values)')
print(f'Formula for num_red: {sol[num_red]} ({sol[num_red].subs(values)} for the given values)')

Output:

Formula for num_blue: (-num_marbles*volume_red + total_volume)/(volume_blue - volume_red)
   (5 for the given values)
Formula for num_red: (num_marbles*volume_blue - total_volume)/(volume_blue - volume_red)
   (3 for the given values)

Solution 2:[2]

s_3 = 63333663
s_6 = 66333663

s_3_list = [int(x) for x in str(s_3)]
s_6_list = [int(x) for x in str(s_6)]
sct_3_chng = sum(s_3_list)
sct_6_chng = sum(s_6_list)
print("INPUT QUADRANT\n")
print("SCT 3 CHANGE: ",sct_3_chng)
print("SCT 6 CHANGE: ",sct_6_chng)

time = 0
for x in str(s_3):
    time = time + 1
print("TIME: ",time)



#NEXT is retrieving the quantity of 3's and 6's in each sector.

s_3_q3 = (time * 6 - sct_3_chng)/(6 - 3)
s_3_q6 = (time - s_3_q3)
print("SECT 3 THREES: ",s_3_q3)
print("SECT 3 SIXES: ",s_3_q6)

This is how to solve the problem, can be values other than 3 and 6 and should still work! Thanks @JohanC

INPUT QUADRANT

SCT 3 CHANGE:  33
SCT 6 CHANGE:  36
TIME:  8
SECT 3 THREES:  5.0
SECT 3 SIXES:  3.0

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JohanC
Solution 2 Aspen Grey