June 22nd, 2024

Identifying Leap Years (2020)

David Turner explores optimizing leap year calculations for performance gains by using bitwise operations and integer bounds. He presents efficient methods, mathematical proofs, and considerations for signed integers, highlighting limitations pre-Gregorian calendar.

Read original articleLink Icon
Identifying Leap Years (2020)

David Turner discusses the optimization of leap year calculations for performance gains. He explores different methods to determine leap years without using division, focusing on bitwise operations and integer bounds. Turner presents three versions of leap year calculations, emphasizing the efficiency of bitwise operations and single multiplications. He also delves into the mathematical proofs behind these calculations, showcasing the use of coprime numbers and modular arithmetic. Turner provides constants for different integer widths and discusses considerations for signed integers. He concludes by mentioning the limitations of applying these calculations to dates before the Gregorian calendar. Turner's analysis offers insights into optimizing leap year checks for computational efficiency.

Related

Mrs Perkins's Quilt (2017)

Mrs Perkins's Quilt (2017)

The Mrs. Perkins's Quilt puzzle involves dividing a 13x13 square quilt into 11 specific squares. Originating in the early 1900s, researchers like Richard Guy and Ed Pegg Jr continue to advance this mathematical challenge.

Wc2: Investigates optimizing 'wc', the Unix word count program

Wc2: Investigates optimizing 'wc', the Unix word count program

The GitHub project "wc2" presents an innovative algorithm for the `wc` program, focusing on asynchronous state-machine parsing in C and JavaScript. It enhances efficiency, scalability, and speed compared to traditional `wc` implementations.

Flambda2 Ep. 2: Loopifying Tail-Recursive Functions

Flambda2 Ep. 2: Loopifying Tail-Recursive Functions

Flambda2's Episode 2 explores Loopify, an optimization algorithm for tail-recursive functions in OCaml. It transforms recursion into loops, enhancing memory efficiency without compromising functional programming principles.

Interface Upgrades in Go (2014)

Interface Upgrades in Go (2014)

The article delves into Go's interface upgrades, showcasing their role in encapsulation and decoupling. It emphasizes optimizing performance through wider interface casting, with examples from io and net/http libraries. It warns about complexities and advises cautious usage.

Show HN: Field report with Claude 3.5 – Making a screen time goal tracker

Show HN: Field report with Claude 3.5 – Making a screen time goal tracker

The author shares their positive experience using Claude 3.5 Sonnet to track screen time goals. Claude proved reliable, fast, and auditable, aiding in reducing screen time through visualizations and goal setting. Despite design flaws, Claude improved performance with accurate metrics and visualizations, benefiting the author's screen time tracking.

Link Icon 6 comments
By @squirrel - 4 months
Fun maths, especially computing the inverse of 25 in Z/65536Z!

As the article mentions, you need a slightly different calculation in the period between 45 BC and 1582 AD -- the rule about 400 didn't exist in the Julian calendar in force then. Before 45 BC, and in non-Roman-based calendars, it gets even more complicated with intercalary months and postponing New Year's and such.

But surely in all but the tiniest fraction of cases, you only care about years after 1582, and years that are no more than a few centuries in the future. There are only 223 leap years from 1582-2500 (I checked with ChatGPT so that number must be right!) A binary search of an ordered 223-item list would require at most 8 comparisons, and if you don't mind a little more space, you could just store all 919 of those years in a list and look up the answer directly. Wouldn't either be faster than any of these methods (and clearer)?

By @tppiotrowski - 4 months
Does a compiler use special cases for division by two (and other integers as well) that it implements as single bitwise operations? This seems like a lot of work to scan through all mathematical operations. Is that why compiling code takes so long sometimes?
By @omoikane - 4 months
I tried implementing the four versions:

16bit: https://gcc.godbolt.org/z/6G38YxcGr

32bit: https://gcc.godbolt.org/z/vEzf9h1jo

I thought it's interesting how "div" is used in 16bit versions, whereas for 32bit versions the compiler optimized all of them to use only imul.

By @bradfitz - 4 months
We need more languages with "moreover" as a keyword.