[Answer and winners] Mathematics × Programming Competition #1

1st Mathematics × Programming Competition


Question

Difficulty index: 4 / 10

Kenchung has 90 SBD and goes to a store to buy stationery. A pencil costs 9 SBD and a pen costs 10 SBD. How many different possible combinations are there for the stationery he purchased? (Buying nothing at all is not considered as a combination) (He does not have to use all the money)


Photo source: www.matthewdeevers.com


Answer: 55


Mathematical approach

Method 1

One obvious method is to do the counting case by case. For example, fix the number of pencils purchased to be 0 first, and then the number of pens can be 1 to 9. Similarly, we can obtain the following table:

No. of pencilsPossible no. of pensNumber of combinations
01 to 99
10 to 89
20 to 78
30 to 67
40 to 56
50 to 45
60 to 34
70 to 23
80 to 12
901
1001

Finally we just sum all the numbers up: 9 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 1 = 55.

Method 2 (Recommended)

Consider the maximum number of stationery that can be purchased, which should be 10, occurring when we buy 10 pencils. However if we exclude this extreme case, we can only buy 9 pieces of stationery at most.

So now we can consider the situation as 9 decisions have to be made, where each of these decisions consists of 3 choices: whether to buy a pencil, a pen or nothing.

We can further transform this problem as 3 pockets are given, labelled as 'pencil', 'pen' and 'nothing'. We also have 9 balls. Now we want to find the number of possible ways of assigning these balls into the pockets.

Not yet finished, one more transformation. Denote O as the balls, | as the separations between pockets. For example, OO|OOOO|OOO means 2 pencils and 4 pens. |OOOOO|OOOO means 5 pens. So now the problem becomes finding the number of patterns that can be formed.

There are 11 slots and we need to pick 2 slots to fill in with |. The others will be automatically be O. So by definition the answer is 11C2 = 55.

But we have excluded the extreme case at the beginning. On the other hand, we have counted the case ||OOOOOOOOO which means nothing has been purchased. Plus one and minus one, the answer is still 55.

I love this method the most; the beauty of maths, isn't it?

Programming approach

The following JavaScript snippet is provided by @adnanrahic. You can visit his blog to learn JavaScript!

The JavaScript Journey 1 - Values
The JavaScript Journey 2 - Arithmetic and Operators


function calculatePensAndPencils() {
  var numberOfCombinations = 0;
  for (var pen = 0; pen < 10; pen++){
    for (var pencil = 0; pencil < 11; pencil++) {
      if (9 * pen + 10 * pencil <= 90) numberOfCombinations++;
    }
  }
  return numberOfCombinations;
}
console.log("The number of combinations is: " + calculatePensAndPencils());
document.write("The number of combinations is: " + calculatePensAndPencils());

Output: The number of combinations is: 55


Winners

In 24 hours' time, we have received 21 responses! Thank you every one for participating! I am so happy to see so many people joining this competition!! :)

The following is a list of answers submitted together with their submission timestamps:

Untitled.png

Congrats to @kelkoo for getting the 1st prize!


He provided the correct answer in 9 minutes after the question is released! Amazing!

And also congrats to the following steemians for getting the 2nd prizes!


@binbin88
@rycharde
@frenchredrum
@kryzsec
@happychau123

We have 12 correct answers received but unfortunately we only have 5 places for the 2nd prize :( So make sure you resteem my post to maximize your chance!

Prize will be given out after the rewards of the announcement post has been redeemed, which is approximately 5 days later.

Don't get upset if you can't get a prize this time :) The next competition is going to start next week! Keep punching! ;)


Upvote, resteem and follow me at @kenchung if you like my posts!

H2
H3
H4
3 columns
2 columns
1 column
18 Comments