Digital Root

Published: May 24, 2016

What is digital root? Suppose you are given some digits, say 123456, the digital root will be calculated like this:

1 + 2 + 3 + 4 + 5 + 6 = 21
2 + 1 = 3

Summing each digit repeatedly to get a single digit is how we get the digital root.

How to effectively find the digital root

An intuitive solution would be to recurse steps: 1. get singles digits from the given number. 2. sum up all. However, if the given number is really big, this is not an effective solution.

Luckily, there’s a formula to calculate the digital root. This wikipedia page explains what the formula and how it works: Digital root.

The formula is:

// n is a given number
int digital_root = n - 9*(int)Math.floor((double)(n-1)/9);

Why this is the answer? The digital root sees how many numbers are there after the closest of multiple of 9 which is less than the given number. If the given number is 123456, its closest, multiple of 9 is 123453. So, the digital root of 123456 is 3.

Latest Posts

Vite + Vue + Bun on Rails

Vue.js is one of frontend frameworks gaining popularity among rapidly emerging JavaScript technologies. The combination of Vue.js and Rails is becoming more popular as well, however, Vue.js development on Rails is not so straightforward. The reason would be that Vue.js relies on Vite for a development environment such as HMR (Hot Module Replacement) and bundling.

Bun + React on Rails

In the frontend world, new technologies keep emerging rapidly these years. Still, React is a well-established and very popular frontend framework, Vue.js, Svelte, Astro and more frameworks are gaining popularity. Not just the frameworks, tools for a transpiler/bundler or sort are also under a rapid development. In JavaScript domain, esbuild, rollup, vite and some more are out.

Ruby on Rails Secrets Management

A web application needs various kinds of values, params and etc which should not be revealed to the public, say GitHub repo. For example, API keys, tokens, passwords, and endpoints, all those should be kept secret. Another important factor is that such secrets should be shared among the team members. Additionally, all those secrets should come along with the deployment.