Digital root/Multiplicative digital root: Difference between revisions
Content added Content deleted
(Added an Algol 68 sample) |
(Added an AWK sample) |
||
Line 224: | Line 224: | ||
9: [9, 19, 33, 91, 119] |
9: [9, 19, 33, 91, 119] |
||
</pre> |
</pre> |
||
=={{header|AWK}}== |
|||
<lang AWK># Multiplicative Digital Roots |
|||
BEGIN { |
|||
printMdrAndMp( 123321 ); |
|||
printMdrAndMp( 7739 ); |
|||
printMdrAndMp( 893 ); |
|||
printMdrAndMp( 899998 ); |
|||
tabulateMdr( 5 ); |
|||
} # BEGIN |
|||
function printMdrAndMp( n ) |
|||
{ |
|||
calculateMdrAndMp( n ); |
|||
printf( "%6d: MDR: %d, MP: %2d\n", n, MDR, MP ); |
|||
} # printMdrAndMp |
|||
function calculateMdrAndMp( n, mdrStr, digit ) |
|||
{ |
|||
MP = 0; # global Multiplicative Persistence |
|||
MDR = ( n < 0 ? -n : n ); # global Multiplicative Digital Root |
|||
while( MDR > 9 ) |
|||
{ |
|||
MP ++; |
|||
mdrStr = "" MDR; |
|||
MDR = 1; |
|||
for( digit = 1; digit <= length( mdrStr ); digit ++ ) |
|||
{ |
|||
MDR *= ( substr( mdrStr, digit, 1 ) * 1 ); |
|||
} # for digit |
|||
} # while MDR > 9 |
|||
} # calculateMdrAndMp |
|||
function tabulateMdr( n, rqdValues, valueCount, value, pos ) |
|||
{ |
|||
# generate a table of the first n numbers with each possible MDR |
|||
rqdValues = n * 10; |
|||
valueCount = 0; |
|||
for( value = 0; valueCount < rqdValues; value ++ ) |
|||
{ |
|||
calculateMdrAndMp( value ); |
|||
if( mdrCount[ MDR ] < n ) |
|||
{ |
|||
# still need another value with this MDR |
|||
valueCount ++; |
|||
mdrCount[ MDR ] ++; |
|||
mdrValues[ MDR ":" mdrCount[ MDR ] ] = value; |
|||
} # if mdrCount[ MDR ] < n |
|||
} # for value |
|||
# print the table |
|||
printf( "MDR: [n0..n%d]\n", n - 1 ); |
|||
printf( "=== ========\n" ); |
|||
for( pos = 0; pos < 10; pos ++ ) |
|||
{ |
|||
printf( "%3d:", pos ); |
|||
separator = " ["; |
|||
for( value = 1; value <= n; value ++ ) |
|||
{ |
|||
printf( "%s%d", separator, mdrValues[ pos ":" value ] ); |
|||
separator = ", " |
|||
} # for value |
|||
printf( "]\n" ); |
|||
} # for pos |
|||
} # tabulateMdr</lang> |
|||
{{out}} |
|||
<pre> |
|||
123321: MDR: 8, MP: 3 |
|||
7739: MDR: 8, MP: 3 |
|||
893: MDR: 2, MP: 3 |
|||
899998: MDR: 0, MP: 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] |
|||
</pre> |
|||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
<lang bracmat>( |
<lang bracmat>( |