How to resolve the algorithm Digital root/Multiplicative digital root step by step in the C programming language
How to resolve the algorithm Digital root/Multiplicative digital root step by step in the C programming language
Table of Contents
Problem Statement
The multiplicative digital root (MDR) and multiplicative persistence (MP) of a number,
n
{\displaystyle n}
, is calculated rather like the Digital root except digits are multiplied instead of being added:
Show all output on this page. The Product of decimal digits of n page was redirected here, and had the following description The three existing entries for Phix, REXX, and Ring have been moved here, under ===Similar=== headings, feel free to match or ignore them.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Digital root/Multiplicative digital root step by step in the C programming language
==> Notes for the code and the AI responses...
- The first response of the AI assistant was ok and its explanation was not completely wrong but it missed few details.
- The second response of the AI assistant was ok and its explanation was not completely wrong but it missed few details.
Here I will explain the code myself.
The C program is designed to find the MDR (multiplication of digits) and MP (multiplication of prime digits) of a given number. It starts with predefined values, builds a table of MDR values, and then prints the table.
-
MDR (Multiplication of Digits): The MDR of a number is the product of all its digits. For example, the MDR of 1234 is 1 * 2 * 3 * 4 = 24.
-
MP (Multiplication of Prime Digits): The MP of a number is the product of all its prime digits. For example, the MP of 1234 is 2 * 3 = 6, since 2 and 3 are the only prime digits in 1234.
--> The Code Structure:
-
Import the stdio.h header for input and output operations.
-
Define a constant twidth to specify the width or number of columns in the MDR table.
-
Define a macro mdr to calculate the MDR and MP of a number. It takes three parameters:
- rmdr: A pointer to store the MDR.
- rmp: A pointer to store the MP.
- n: The input number.
-
Implement the internal function _mdr to recursively calculate the MDR and MP. It adjusts for the 0 case and calculates the result.
-
In the main function:
-
Initialize variables for loop counters, MDR, and MP.
-
Set up an array values with predefined test values.
-
Print the initial test results.
-
Calculate and store MDR values in a 2D table table and count how many values have been filled for each MDR value in an array tfill.
-
Print the calculated MDR table.
-
--> The Execution Flow:
- The program starts by displaying the MDR and MP for the provided test values.
- It then calculates the MDR values for each number from 0 to 99 and fills up the MDR table.
- Finally, it prints the MDR table, showing the MDR values for each MDR from 0 to 9, along with the numbers that produce those MDR values. This table can be useful for quickly finding MDR values for different numbers or for understanding the distribution of MDR values.
Source code in the c programming language
#include <stdio.h>
#define twidth 5
#define mdr(rmdr, rmp, n)\
do { *rmp = 0; _mdr(rmdr, rmp, n); } while (0)
void _mdr(int *rmdr, int *rmp, long long n)
{
/* Adjust r if 0 case, so we don't return 1 */
int r = n ? 1 : 0;
while (n) {
r *= (n % 10);
n /= 10;
}
(*rmp)++;
if (r >= 10)
_mdr(rmdr, rmp, r);
else
*rmdr = r;
}
int main(void)
{
int i, j, vmdr, vmp;
const int values[] = { 123321, 7739, 893, 899998 };
const int vsize = sizeof(values) / sizeof(values[0]);
/* Initial test values */
printf("Number MDR MP\n");
for (i = 0; i < vsize; ++i) {
mdr(&vmdr, &vmp, values[i]);
printf("%6d %3d %3d\n", values[i], vmdr, vmp);
}
/* Determine table values */
int table[10][twidth] = { 0 };
int tfill[10] = { 0 };
int total = 0;
for (i = 0; total < 10 * twidth; ++i) {
mdr(&vmdr, &vmp, i);
if (tfill[vmdr] < twidth) {
table[vmdr][tfill[vmdr]++] = i;
total++;
}
}
/* Print calculated table values */
printf("\nMDR: [n0..n4]\n");
for (i = 0; i < 10; ++i) {
printf("%3d: [", i);
for (j = 0; j < twidth; ++j)
printf("%d%s", table[i][j], j != twidth - 1 ? ", " : "");
printf("]\n");
}
return 0;
}
You may also check:How to resolve the algorithm Sierpinski triangle/Graphical step by step in the Haskell programming language
You may also check:How to resolve the algorithm Draw a rotating cube step by step in the Perl programming language
You may also check:How to resolve the algorithm Hostname step by step in the REXX programming language
You may also check:How to resolve the algorithm Balanced brackets step by step in the Batch File programming language
You may also check:How to resolve the algorithm Narcissist step by step in the Ada programming language