Home Profile Projects Articles

Answer: Christmas Counting Problem

Wednesday, 26 of December , 2007 at 10:15 am

Question: If we take the 12 days of Christmas literally, what is the total number of items you will have received at the end of the twelve days? For example, on the first day you have received one item (1 partridge), on the second day you will receive three items (1 partridge, and 2 doves), on the third day you would receive six items (1 partridge, 2 doves, and 3 french hens), etc.

Solution: From my analysis, I have come up with three different ways to count the total number of items you will have received at the end of the twelve days.

1) The first method to solve this problem is to use brute force. On the first day, we will receive 1 item. On the second day, we will receive 1 + 2 items. On the third day, we will receive 1 + 2 + 3 items. Therefore, on nth day, we will receive \sum_{i=1}^n i items. Since we want the number of items received for all 12 days, we need to calculate the following summation: \sum_{n=1}^{12} \sum_{i=1}^n i. We can easily calculate the answer with the following perl script:

$soln = 0;
 
for ($i = 1; $i < 13; $i++) {
   for ($j = 1; $j < $i+1; $j++) {
      $soln = $soln + $j;
   }
}
print $soln;

2) The second method to solve this problem is to use recursion. On the nth day, you are going to get n items plus the number of items you got the day before. Therefore, if S_n is the number of items you receive on the nth day, S_n will follow this recursive pattern:

S_n = S_{n-1} + n
S_0 = 0

In order to calculate the number of items received on all 12 days, we need to calculate the following summation: S = \sum_{n=1}^{12} S_{n-1} + n. The solution can be calculated with the following script:

$soln = 0;
$prevDay = 0;
 
for ($i = 1; $i < 13; $i++) {
   $prevDay = $prevDay + $i;
   $soln = $soln + $prevDay;
}
 
print $soln;

3) The third method to solve this problem is to group items by type instead of days. For example, you will receive 1 partridge for twelve days, 2 doves for eleven days, 3 french hens for ten days, etc. In general, this means that you will receive n items for (13 - n) days. This would mean that the total number items you would receive would be \sum_{n=1}^{12} n (13 - n). This summation can be calculated by the following script:

$soln = 0;
 
for ($i = 1; $i < 13; $i++) {
   $soln = $soln + $i*(13- $i);
}
print $soln;

In the end, however, we come back to the same solution which is 364.

Category: Answers, General Math

No Comments

No comments yet.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> [tex]