PHP Continuous Digital Sums

AceInfinity

Emeritus, Contributor
Joined
Feb 21, 2012
Posts
1,728
Location
Canada
Here's my implementation of a repretative digital sum in C++:
Code:
[plain]#include <iostream>
#include <algorithm>
#include <cstring>

template <typename T>
T digital_root(T n)
{
	return n - 9 * ((n - 1) / 9);
}

template <typename T>
T digital_root(char *arr)
{
	T n = 0;
	std::for_each(arr, arr + strlen(arr), [&n](char c){ n += c; });
	return digital_root<T>(n);
}

int main ()
{
	// 593478:
	// 5 + 9 + 3 + 4 + 7 + 8 = 36
	// 3 + 6 = 9
	int x1 = 593478;
	std::cout << digital_root<int>(x1) << std::endl;
	char x2[] = "593478";
	std::cout << digital_root<int>(x2) << std::endl;
	return 0;
}[/plain]

The idea as shown in the comment above is to take a number, and sum its digits getting a new number, and repeat until the number of digits in that number is equal to 1. I've implemented a way for this to work with c-strings as well using.

Here is another method, but it is less efficient:
Code:
[plain]template <typename T>
T digital_root(char *arr)
{
	size_t i = 0;
	while (i < strlen(arr) - 1)
	{
		while (arr[i] != '0')
		{
			int n = arr[i] + arr[i+1] - ('0' * 2);
			int div = n / 10;
			arr[i] = div + '0';
			arr[i+1] = n - (div * 10) + '0';
		}
		i++;
	}
	return arr[i] - '0';
}[/plain]

It doesn't rely on the datatype to be large enough for the initial sum. However, it would take an extremely large one to exceed the int datatype upper limit. :)

Note: Something like this, my original function would still work on, because it's not the initial value itself, it's the sum of all the digits in the char array of the initial value that gets passed to my first function that validates an integral itself:

Code:
[NO-PARSE]char arr[] = "98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735"
"98749587239482739876594872393478239847609438720938749286735956732984672938674908​735[/plain]

This is why I mention -- It would take a really large array of chars for my first method to become ineffective. The sum of all of these digits is still only 17064... Much less than the max value of a 32 bit signed integer.

:beerchug2:
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top