C++: Multiplicative Digital Root

 

The multiplicative digital root (MDR) and multiplicative persistence (MP) of a number, n, is calculated rather like the Digital root except digits are multiplied instead of being added:

  1. Set m to n and i to 0.
  2. While m has more than one digit:
    • Find a replacement m as the multiplication of the digits of the current value of m.
    • Increment i.
  3. Return i (= MP) and m (= MDR)
Task
  • Tabulate the MP and MDR of the numbers 123321, 7739, 893, 899998
  • Tabulate MDR versus the first five numbers having that MDR, something like:
MDR: [n0..n4]
===  ========
  0: [0, 10, 20, 25, 30]
  1: [1, 11, 111, 1111, 11111]
  2: [2, 12, 21, 26, 34]
  3: [3, 13, 31, 113, 131]
  4: [4, 14, 22, 27, 39]
  5: [5, 15, 35, 51, 53]
  6: [6, 16, 23, 28, 32]
  7: [7, 17, 71, 117, 171]
  8: [8, 18, 24, 29, 36]
  9: [9, 19, 33, 91, 119]

#include <iomanip>
#include <map>
#include <vector>
#include <iostream>
using namespace std;

void calcMDR( int n, int c, int& a, int& b )
{
	int m = n % 10; n /= 10;
	while( n )
	{
		m *= ( n % 10 );
		n /= 10;
	}
	if( m >= 10 ) calcMDR( m, ++c, a, b );
	else { a = m; b = c; }
}

void table()
{
	map<int, vector<int> > mp;
	int n = 0, a, b;
	bool f = true;
	while( f )
	{
		f = false;
		calcMDR( n, 1, a, b );
		mp[a].push_back( n );
		n++;
		for( int x = 0; x < 10; x++ )
		if( mp[x].size() < 5 )
		{ f = true; break; }
	}

	cout << "|  MDR  |  [n0..n4]\n+-------+------------------------------------+\n";
	for( int x = 0; x < 10; x++ )
	{
		cout << right << "| " << setw( 6 ) << x << "| ";
		for( vector<int>::iterator i = mp[x].begin(); i != mp[x].begin() + 5; i++ )
		cout << setw( 6 ) << *i << " ";
		cout << "|\n";
	}
	cout << "+-------+------------------------------------+\n\n";
}

int main( int argc, char* argv[] )
{
	cout << "|  NUMBER  |   MDR    |    MP    |\n+----------+----------+----------+\n";
	int numbers[] = { 123321, 7739, 893, 899998 }, a, b;
	for( int x = 0; x < 4; x++ )
	{
		cout << right << "| "  << setw( 9 ) << numbers[x] << "| ";
		calcMDR( numbers[x], 1, a, b );
		cout << setw( 9 ) << a  << "| " << setw( 9 ) << b << "|\n";
	}
	cout << "+----------+----------+----------+\n\n";
	table();
	return system( "pause" );
}
Output:
|  NUMBER  |   MDR    |    MP    |
+----------+----------+----------+
|    123321|         8|         3|
|      7739|         8|         3|
|       893|         2|         3|
|    899998|         0|         2|
+----------+----------+----------+

|  MDR  |  [n0..n4]
+-------+------------------------------------+
|      0|      0     10     20     25     30 |
|      1|      1     11    111   1111  11111 |
|      2|      2     12     21     26     34 |
|      3|      3     13     31    113    131 |
|      4|      4     14     22     27     39 |
|      5|      5     15     35     51     53 |
|      6|      6     16     23     28     32 |
|      7|      7     17     71    117    171 |
|      8|      8     18     24     29     36 |
|      9|      9     19     33     91    119 |
+-------+------------------------------------+

SOURCE

Content is available under GNU Free Documentation License 1.2.