Top Mathematics discussions

NishMath

@github.com //
Modulo arithmetic is being explored as a technique for rotation, inspired by a post from Greg Egan on Mastodon. Jonathan Carroll implemented Egan's formula in various languages. The formula, rot(n, j, k) = (n * 10^k) mod (10^j-1), efficiently rotates a j-digit number n by k digits. Carroll highlighted the importance of understanding fundamental operators across different languages while implementing this seemingly simple mathematical concept.

This exploration is beneficial for fortifying knowledge of basic functionalities in different programming languages. As Carroll wrote in his blog, implementing this simple math "means I’ll be working with some basic functionality and I believe it’s very important to have that locked in comfortably." Carroll demonstrated the implementation in R, utilizing the power operator '^' and the modulo operator '%%'. He also showed how to determine the number of digits in a number using the 'nchar()' function, showcasing practical application of these operators.

The R implementation allows for the cyclic rotation of digits within a number. While R doesn't have a built-in function for this specific purpose, Carroll demonstrated the usage of his '{vec}' package for a similar ring-buffer effect on vectors. This involved using modulo on the indices to achieve the desired rotation. The formula subtracts (10^j-1) times the leftmost k digits of n*10^k, removing them from the left and adding them to the right.
Original img attribution: https://jcarroll.com.au/img/logo.png
ImgSrc: jcarroll.com.au

Share: bluesky twitterx--v2 facebook--v1 threads


References :
Classification: