r/learnprogramming • • 3h ago

Is there a way to do this problem faster using computer programming code?

For the song "Twelve Days of Christmas", if you tallied up all the items you received over 12 days- for example, on the 5th day you received 5 gold rings, and thus would need to add that from day 5 to 12 repeatedly- you would end with a large number of each item by the end of the twelfth day. The first instinct would be to put all this data into Excel, and use SUM functions. With new AI technology, or even computer programming coding in general, is there a faster way to get to the final answer?

0 Upvotes

29 comments sorted by

21

u/Inevitable_Dance1191 3h ago

I don't want to be mean but "new AI technology" is the last thing you need for a basic math problem. An easy way to solve this is with nested for loops.

•

u/Adventurous-Rabbit52 30m ago

It wouldn't be too bad to solve it with Excel; I was just wondering how a coder would solve it, and if it could be done significantly faster than Excel. Heck, if it was just the solution, somebody might have already done it, and Googling it might be the fastest of all...

4

u/DrShocker 3h ago

If all we want is the answer, it's actually O(1) with Gauss Summation.

3

u/johlae 3h ago

In python:

>>> sum = 0
... total = 0
... for a in range(1,13):
...     sum = sum + a
...     total = total + sum
...     print(f"{a} something")
... print(f"total = {sum}")
... print(f"cumul = {total}")
... 
1 something
2 something
3 something
4 something
5 something
6 something
7 something
8 something
9 something
10 something
11 something
12 something
total = 78
cumul = 364

2

u/zeekar 3h ago edited 3h ago
>>> [n * (13 - n) for n in range(1,13)]
[12, 22, 30, 36, 40, 42, 42, 40, 36, 30, 22, 12]
>>> sum(_)
364

But if you want a nice legible report:

Gifts = [
    'partridges in pear trees', 'turtle doves', 'French hens', 
    'calling birds', 'gold rings', 'geese a-laying', 'swans a-swimming', 
    'maids a-milking', 'ladies dancing', 'lords-a-leaping', 'pipers piping',
    'drummers drumming'
]

total = 0
for i, gift in enumerate(Gifts):
    count = (i + 1) * (12 - i)
    print(f'{count} {gift}')
    total += count

print(f'Total: {total} gifts')

•

u/Adventurous-Rabbit52 24m ago

Upvote for you. You went all out describing the gifts.

•

u/zeekar 13m ago edited 5m ago

Sure. The fun part is coding up a program to print all the lyrics with all the repetitions without repeating yourself in the code. :)

Anyway, in math terms the formula is simply this:

12
∑ i(13-i)
i=1

You can take advantage of the fact that the sum of the integers from 1 to n is n(n+1)/2 and the sum of squares from 1 to n is n(n+1)(2n+1)/6 to simplify into a flat formula with no loop required: the sum from 1 to k of i(k+1-i) is equal to k(k+1)(k+2)/6. In this case k=12 and you get 12·13·14/6 = 2·13·14 = 364.

9

u/SirCarboy 3h ago

12 x 1

11 x 2

10 x 3

9 x 4

8 x 5

7 x 6

6 x 7

5 x 8

4 x 9

3 x 10

2 x 11

1 x 12

you‘re welcome

3

u/No_Report_4781 3h ago

Could you do it the other way? The song starts at the 1st Day of Christmas 😉

3

u/johlae 3h ago

That's where on Linux the tac command comes into play.

1

u/NationalOperations 2h ago

And you can use toe command to come to terms with the results

1

u/SirCarboy 1h ago

absolutely. I just wrote it as it came out of my head... "12 days of 1 partridge in a pear tree. 11 days of 2 turtle doves. etc."

6

u/TimelyTraffic8003 2h ago

You don't need "new AI technology" this is a basic summation.

https://en.wikipedia.org/wiki/Summation

2

u/Kiro0613 1h ago

Whoa, slow down, that's too advanced for me. Can you reframe the concept in terms of how many apples I have?

9

u/No_Report_4781 3h ago

One form of writing a program to handle solving the math uses recursion

9

u/tmtowtdi 3h ago

One form of writing a program to handle solving the math uses recursion

1

u/SuggestableFred 3h ago

One form of writing a program to handle solving the math uses one form of writing a program to handle solving the math uses recursion

1

u/Philluminati 2h ago

StackOverflowException

1

u/Fin_box 2h ago

python3 -c "print(sum(gift_count * (13 - gift_count) for gift_count in range(1, 13)))"

•

u/Adventurous-Rabbit52 31m ago

Upvote for you. This right here- coding really is faster, even over the already decently fast Excel Spreadsheet idea.

•

u/Fin_box 19m ago

danka. it's easy when you know how but it depends on your definition of faster. pre ai, if you'd never coded nor used excel before you'd probably get to 364 faster in excel i expect. today much of a muchness. any ai could have solved it for you.

3

u/captainAwesomePants 3h ago edited 3h ago

The fastest way is probably to type "how many total gifts in 12 days of christmas" into your search bar and you'll probably learn the answer immediately.

The second fastest way is probably a calculator. Excel's also a good choice. Make a triangle with a row of 12 1s, 11 2s, 10 3s, etc. Sum it all. Works great.

If we slow down and think about it, we can find patterns. Each day's gift follows a pattern. We can work out the pattern. 1 thing for 12 days, 2 things for 11 days, ..., 12 things for one day. You get N things, (13-N) times. So our answer will be the sum of N * (13-N) over the range of N=1 through 12.

We could then generalize this to N(N+1)(N+2)/6, where N is the total number of days.

Let's test that. First day, expected one gift: 123/6 = 1. Yep.

Second day: we get 234/6 = 4. That's the first gift twice, plus 2 turtle doves. Yep, it works.

So if there are N total days of Christmas, your formula's N * (N+1) * (N+2) / 6. Plug in 12 and you've got your answer.

We can also solve this with programming a simulation:

total_gifts = 0
for day in range(1, 12+1):
  print(f'On the {day}th day of Christmas, my true love gave to me')
  for gift_count in range(day, 0, -1):
    print(f'{gift_count} somethings somethinging')
    total_gifts += gift_count
print(f'I got {total_gift} gifts')

2

u/futur3_pr3s3nt 3h ago

with 'new AI technology', you just ask the AI to write this program for you, and it will.

2

u/BranchLatter4294 3h ago

It's a great programming exercise.

1

u/The-Phantom-Blot 3h ago

"Faster" depends on the context. In this case, you have fixed conditions. Excel is OK and pretty fast. But if you had to write a dynamic solver, or specifically needed it done inside a specific program, then you could use recursive functions to build out an array with the quantities.

1

u/mkhanwrites_ 3h ago

This is a very basic partial series sum question. The sum of the first n natural numbers 1 + 2 + ... + n is n*(n+1)/2.

For n = 12, you have 12*(12+1)/2 = 12*13/2 = 156/2 = 78.

To answer you question, to find a "faster way to get to the final answer," it might be worth learning mathematics.

1

u/SuperSathanas 2h ago

You could figure this out by hand in less time than it took you to make the post. It's a stupid simple problem, and even if you don't already know a way to solve this, the pattern becomes clear after you add up like 4 or 5 days worth of stuff.

The Solution: you find the sum of each day's gifts with Triangular Numbers. Triangular numbers are like factorials, except you're summing instead of getting the product. Literally just summing sequential numbers, which is what you want to do for each of the 12 days.

Luckily, there is a simple little formula we can use for each day: (n(n+1)) / 2, where n is the number of the day.

  • Day 1: (1 (2)) / 2 = 1
  • Day 2: (2 (3)) / 2 = 3
  • Day 3: (3 (4)) / 2 = 6
  • Day 4: (4 (5)) / 2 = 10
  • Day 5: (5 (6)) / 2 = 15

The pattern becomes obvious. Start with a zero sum, and for each day, add 1 more to the total sum than you did the previous day. On day 1, you add 1, because previously 0 was added. On day 2, you add 2, because on day 1 you added. Day 3 is 3, day 4 is 4, and on and on and on...

Do that for 12 days, then add all your daily sums together (which I think the sum of sequential triangular numbers is called a tetrahedral number, but don't quote me on that), and you get 364.