Persistent reduction
Weekly challenge 359 — 2 February 2026
Week 359: 2 Feb 2026
You are given a positive integer, $int.
Write a function that calculates the additive persistence
of $int and also returns the digital root.
The digital root is the recursive sum of all digits in a number until a single digit is obtained.
The additive persistence is the number of times you need to sum the digits to reach a single digit.
Example 1 Input: $int = 38 Output: Persistence = 2 Digital Root = 2 38 => 3 + 8 => 11 11 => 1 + 1 => 2 Example 2 Input: $int = 7 Output: Persistence = 0 Digital Root = 7 Example 3 Input: $int = 999 Output: Persistence = 2 Digital Root = 9 999 => 9 + 9 + 9 => 27 27 => 2 + 7 => 9 Example 4 Input: $int = 1999999999 Output: Persistence = 3 Digital Root = 1 1999999999 => 1 + 9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 => 82 82 => 8 + 2 => 10 10 => 1 + 0 => 1 Example 5 Input: $int = 101010 Output: Persistence = 1 Digital Root = 3 101010 => 1 + 0 + 1 + 0 + 1 + 0 => 3
This is easily coded as a loop which performs the digit summation repeatedly until the result is <=9, and my code completes in microseconds for any positive integer that (my version of) Perl can represent as an integer, ie from 1 to 18446744073709551615 (which is my 4th example).
But is that what Mohammad meant? In Perl, a scalar can be a string of any length, so
$int = '999999999999999999999999999999999999999999999999999999'
is valid Perl, and the number is mathematically an integer.
In fact, my code can handle any integer which is presented as a string, provided that the initial sum of its digits is less than around 10**19. As the digits can't exceed 9, my code will work up to an integer with around 10**18 digits, which I think is adequate for a Monday morning.
Note that in all my examples the additive persistence is never more than 4, and in fact my last example is the smallest number with persistence 4.
According to Wikipedia, the smallest number with persistence of 5 is 1 followed by 2,222,222,222,222,222,222,222 nines which - see above - is more than my code could handle.
#!/usr/bin/perl # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge use v5.26; # The Weekly Challenge - 2026-02-02 use utf8; # Week 359 - task 1 - Digital root use warnings; # Peter Campbell Smith binmode STDOUT, ':utf8'; use Encode; digital_root(1999999999); digital_root(31415926535); digital_root(10101010101010101); digital_root(~0); digital_root(1999999999999999999); digital_root('99999999999999999999999999999999999999999999999999999999'); digital_root('19999999999999999999999'); sub digital_root { my ($root, $persist); # initialise $root = $_[0]; $persist = 0; # add the digits ... do { $root = eval(join('+', split('', $root))); $persist ++; # ... until only one digit is left } until $root <= 9; say qq[\nInput: $_[0]]; say qq[Output: additive persistence = $persist, ] . qq[digital root = $root]; }
Input: 1999999999 Output: additive persistence = 3, digital root = 1 Input: 31415926535 Output: additive persistence = 2, digital root = 8 Input: 10101010101010101 Output: additive persistence = 1, digital root = 9 Input: 18446744073709551615 Output: additive persistence = 3, digital root = 6 Input: 1999999999999999999 Output: additive persistence = 3, digital root = 1 Input: 999999999999999999999999999999999999999999999999 99999999 Output: additive persistence = 2, digital root = 9 Input: 19999999999999999999999 Output: additive persistence = 4, digital root = 1
Any content of this website which has been created by Peter Campbell Smith is in the public domain