[Answer and winners] Mathematics × Programming Competition #1

in #chainbb-general7 years ago (edited)

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!

Sort:  

Wow this kind of competition is very interesting! I hope there are more competitions of this kind on the steemit forum. Can you organize more maths competitions? I think it's good to have more intellectual exchange :)

Sure! I will continue to organize these kinds of competition! I think I will organize once per week, make sure you join them next time! :)

I am the second one! So sad!!!!

Ya, very close! Great job man, keep it up! :)

Thank you sir~

You are welcome! :) hope to see you in the next competition again!

21 responses is pretty good. Do people prefer that the answers remain hidden?

I am not sure though, what's your opinion? I think answers hidden should be good to avoid copying of answers, seems fairer to me
hope to see even more responses next time!

I guess what he means is that their answers should remain hidden even after the competition has ended?

I'm keeping an open mind on this. I've done similar things before on other forums and like those Aha! moments from people who suddenly get it with a few nudges. I usually wait for such hints till there is an obvious good answer already so as not to skew the winner.

wow the second method is genius O_O
I just used excel to brute force it haha~

btw thank you for the prize :D//~~~

looking forward to your next question :)

you are welcome! :) Yes it is not that easy to come up with the second solution, but it is really beautiful :)
Hope that I can see you next time in the competitions as well!

Thanks for the detailed explanation! Was interesting counting those possible combinations (got them wrong though :S)
Will sure participate in future questions! Upvoted

Great that you like these types of questions! Hope to see you in the next competition! :)

Thanks, got one of the prizes! Glad I re-calculated it... using your method 2.

Yep! congrats :) hope to see you in the next competition again! :)

This was a fun challenge, cant wait to see the next one you have!

glad that you liked it! the next challenge will be coming soon, and I will distribute the prize soon as well. Thanks to HF 19, the prize pool was significantly increased!

Coin Marketplace

STEEM 0.16
TRX 0.16
JST 0.032
BTC 59304.77
ETH 2534.68
USDT 1.00
SBD 2.41