How AI is applied across API Evangelist and APIs.io. Read my AI disclosure →
API Evangelist API Evangelist
Discovery
Learnings
Guidance
Toolbox
Alignment
API Evangelist LLC

Avoiding having to calculate the gcd when doing cycle decomposition -- Raymond Chen

calendar_today August 3, 2026 person Blog Staff domain cplusplus

Last time, we looked at how clang’s libcxx implementation of std:: rotate uses cycle decomposition to minimize the number of swaps. Doing so requires calculating the greatest common divisor, but I noted that the OpenJDK implementation of the java standard library uses a trick to avoid doing the gcd calculation . Rotation revisited: Avoiding having to calculate the gcd when doing cycle decomposition by Raymond Chen From the article: The trick is realizing that the total number of elements is equal to the sum of the lengths of each of its cycles, and each of the initial elements belongs to a dif

open_in_new Read original post