Search by

anuar / digitalroot

alfetahe

This package is abandoned and no longer maintained. The author suggests using the alfetahe/digitalroot package instead.

Library for getting digital root from numbers and letters.

Package info

github.com/alfetahe/digitalroot

pkg:composer/anuar/digitalroot

Statistics

Installs: 13

Dependents: 0

Suggesters: 0

Stars: 2

Open Issues: 0

v2.0.0 2026-08-30 23:15 UTC

This package is auto-updated.

Last update: 2026-08-30 23:32:52 UTC


README

PHP library for calculating the digital root of numbers and letters.

The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This continues as long as necessary to obtain a single digit.

Letters are supported too. By default each letter carries its position value on the Pythagorean chart (A=1 … I=9, J=1 … R=9, S=1 … Z=8), and you can override that with your own map by passing an array as the second parameter to any of the exposed functions.

Requirements

  • PHP 8.2 or newer

Install

Via Composer:

composer require alfetahe/digitalroot

Usage examples

use \digitalRootSrc\digitalRootBuilder;

// Returns the digital root (single digit).
// Output: [
//     "client_input" => "23081996",
//     "digital_root" => 2
// ]
digitalRootBuilder::getDigitalRoot("23081996");


// Returns the digital root along with every intermediate step of the calculation.
// Output: [
//     "client_input"     => "299493218",
//     "digital_root"     => 2,
//     "full_calculation" => [2, 9, 11, 1, 1, 2, 9, 11, 1, 1, 2, 4, 6, 9, 15, 1, 5, 6, 3, 9, 2, 11, 1, 1, 2, 1, 3, 8, 11, 1, 1, 2]
// ]
digitalRootBuilder::getDigitalRootCompleteCalculation("299493218");


// Pass an array, get back a map of input => digital root.
// Output: [
//     "23081996" => 2,
//     "43434336" => 3
// ]
digitalRootBuilder::getdigitalRootBulk(["23081996", "43434336"]);


// Give letters your own numeric values with the optional second parameter.
// Output: [
//     "client_input" => "abc12",
//     "digital_root" => 9
// ]
digitalRootBuilder::getDigitalRoot("abc12", ["A" => 1, "B" => 2, "C" => 3]);

Default letter values

Value Letters
1 A J S
2 B K T
3 C L U
4 D M V
5 E N W
6 F O X
7 G P Y
8 H Q Z
9 I R

Behaviour notes

  • Input is taken as a string. Anything that is not 0-9 or a-zA-Z is stripped before the calculation, so "a,..,b" is treated as "ab".
  • Letters are case insensitive.
  • Empty input, and input that reduces to nothing, has a digital root of 0.
  • If you pass your own letter map and the input contains a letter the map does not define, an InvalidArgumentException is thrown rather than the letter silently counting as zero.
  • getdigitalRootBulk() keys its result by the input value. Because these are PHP array keys, duplicate inputs collapse into a single entry and numeric strings become integer keys. Use getDigitalRoot() in your own loop if you need one result per input, in order.

Testing

composer install
composer test

License

MIT. See LICENSE.