> ## Content Index
> Fetch the complete content index at: https://nolongerset.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Calculating Gift Counts for "The Twelve Days of Christmas": Looping Edition
- URL: https://nolongerset.com/gift-counts-looping-solution/
- Published: 2022-12-23T03:02:39.000Z
- Updated: 2026-05-08T12:52:45.000Z
- Description: In the first solution to my recent reader challenge, I use a loop to calculate the cumulative number of gifts given for each day of the "12 Days of Christmas."
- Author: Mike Wolfe
- Tags: Reader Challenge, #Import 2026-05-20 02:59

In [yesterday's article](https://nolongerset.com/challenge-calc-gift-count-by-day/), I posted a reader challenge:

> How many total gifts does one's true love deliver by the nth day of Christmas?

***SPOILER ALERT***: If you would like to try figuring it out on your own, follow the link above to read the original article before scrolling down to see one of my solutions.

---

The goal is to write a function that takes as input the first column (**Day of Christmas**) and returns as output the last column (**Cumulative gifts**):

![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2022/12/image-47.png)

## Using a Loop to Calculate the Total

This solution is based on the following pattern that emerges when one studies the above matrix:

```
1: (1 × 1)
2: (1 × 2) + (2 × 1)
3: (1 × 3) + (2 × 2) + (3 × 1)
4: (1 × 4) + (2 × 3) + (3 × 2) + (4 × 1)
5: (1 × 5) + (2 × 4) + (3 × 3) + (4 × 2) + (5 × 1)
6: (1 × 6) + (2 × 5) + (3 × 4) + (4 × 3) + (5 × 2) + (6 × 1)
```

If you notice, the first number in each multiplication couplet increments from `1` to `n` and the second number in each couplet decrements from `n` to `1`.

Based on the above pattern, the solution below goes through a loop `n` times and accumulates the total number of gifts given up to and including on day `n`.

```vba
'--== Looping Version ==--
Function GiftsByXmasDay(DayOfXmas As Long) As Long
    
    'Initialize x and y
    Dim x As Long: x = DayOfXmas
    Dim y As Long: y = 1
    
    Dim i As Integer
    For i = 1 To DayOfXmas
        GiftsByXmasDay = GiftsByXmasDay + x * y
        x = x - 1  'Decrement x
        y = y + 1  'Increment y
    Next i
End Function
```

### Referenced articles

[Reader Challenge: Write a Formula to Calculate Gift Totals in “The Twelve Days of Christmas”How many total gifts does one’s true love deliver by the nth day of Christmas? It’s a deceptively tricky problem to solve.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2022/12/Twelve-Days-of-Christmas--1-.jpeg)](https://nolongerset.com/challenge-calc-gift-count-by-day/)

*Cover image created with [Microsoft Designer](https://designer.microsoft.com/)*