Site icon Strange Quarks

On the N Days Of Christmas, part 6 (TL;DR)

This post summarises the results of earlier posts.

For each method, the following are given:

Where multiple functions were presented for a method in earlier posts, the most efficient and representative (closest to the mathematical notation) is given.

Subtle considerations regarding function parameter and variable types that allow larger values of n to yield correct results are omitted from the code here; assume very large (128 bit) integer types.

Headings link back to the post in which the method was first introduced.

Cumulative Row Addition

Worked example for 12 days

Adding all the results in bold gives: 1 + 3 + 6 + 10 + 15 + 21 + 28 + 36 + 45 + 55 + 66 + 78 = 364.

Mathematical notation

Julia function

function daysofxmas(n)
    total = 0
    for day in 1:n
        daysum = sum(1:day)
        total += daysum
    end
    total
end

Run-time for n=100,000: 0.0005 seconds

Run-time for n=1000,000,000: 8.5 seconds

Notes

Column Addition, i.e. Dot Product

Worked example for 12 days

Mathematical notation

Julia function

function daysofxmas(n)
    days = [1:n;]
    sum(days .* reverse(days))
end

Run-time for n=100,000: 0.0007 seconds

Run-time for n=1000,000,000: 33.7 seconds

Notes

Triangular Numbers

Worked example for 12 days and mathematical notation

Julia function

function triangular(n)
    div(n*(n+1), 2)
end

function daysofxmas(n)
    total = 0
    for n in 1:n
        total += triangular(n)
    end
    total
end

Run-time for n=100,000: 0.0003 seconds

Run-time for n=1000,000,000: 6.8 seconds

Notes

Polynomial Model

Worked example for 12 days and mathematical notation

Julia function

function daysofxmas(n)
    div(n^3, 6) + div(n^2, 2) + div(n, 3)
end

Run-time for n=100,000: 0.00001 seconds

Run-time for n=1000,000,000: 0.00001 seconds

Notes

Summary

Taking the results for n=100,000 and n=1,000,000,000 into account, we have the following ranking:

RankingMethod
1Polynomial Model
2Triangular Numbers
3Row Addition
4Dot Product
Ranking of methods and their functions

As n gets larger, the run-time difference between the methods due to these time complexity variations becomes more obvious, as can be seen from the results.

The polynomial model method computes the N Days of Christmas in constant time vs linear or quadratic time for the other methods.

It satisfies my initial goal of a simple expression with the lowest computational time complexity and shortest run-time, for any value of n. It is more than a million times faster than its nearest competitor for n = 1 billion.

If I was looking for the fastest possible function implementations, I’d use a language like C, but Julia is better than some other scientific computing languages in this regard.

“Simplest” or “most compact” may be in the eye of the beholder when it comes to mathematical notation (they’re all fairly compact) but shortest function run-time is harder to argue with.

The final post, part 7, briefly explores a possible link between triangular numbers and the polynomial model.

Exit mobile version