Jump to content

Digital root/Multiplicative digital root: Difference between revisions

Added an Algol 68 sample
(Updated D entries)
(Added an Algol 68 sample)
Line 101:
</pre>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<lang algol68># Multiplicative Digital Roots #
 
# structure to hold the results of calculating the digital root & persistence #
MODE DR = STRUCT( INT root, INT persistence );
 
# calculate the multiplicative digital root and persistence of a number #
PROC md root = ( INT number )DR:
BEGIN
 
# calculate the product of the digits of a number #
PROC digit product = ( INT number )INT:
BEGIN
 
INT result := 1;
INT rest := number;
 
WHILE
result TIMESAB ( rest MOD 10 );
rest OVERAB 10;
rest > 0
DO
SKIP
OD;
 
result
END; # digit product #
 
INT mp := 0;
INT mdr := ABS number;
 
WHILE mdr > 9
DO
mp +:= 1;
mdr := digit product( mdr )
OD;
 
( mdr, mp )
END; # md root #
 
# prints a number and its MDR and MP #
PROC print md root = ( INT number )VOID:
BEGIN
DR mdr = md root( number );
print( ( whole( number, -6 )
, ": MDR: ", whole( root OF mdr, 0 )
, ", MP: ", whole( persistence OF mdr, -2 )
, newline
)
)
END; # print md root #
 
# prints the first few numbers with each possible Multiplicative Digital #
# Root. The number of values to print is specified as a parameter #
PROC tabulate mdr = ( INT number of values )VOID:
BEGIN
 
[ 0 : 9, 1 : number of values ]INT mdr values;
[ 0 : 9 ]INT mdr counts;
mdr counts[ AT 1 ] := ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
 
# find the first few numbers with each possible mdr #
 
INT values found := 0;
INT required values := 10 * number of values;
 
FOR value FROM 0 WHILE values found < required values
DO
DR mdr = md root( value );
IF mdr counts[ root OF mdr ] < number of values
THEN
# need more values with this multiplicative digital root #
values found +:= 1;
mdr counts[ root OF mdr ] +:= 1;
mdr values[ root OF mdr, mdr counts[ root OF mdr ] ] := value
FI
OD;
 
# print the values #
 
print( ( "MDR: [n0..n" + whole( number of values - 1, 0 ) + "]", newline ) );
print( ( "=== ========", newline ) );
FOR mdr pos FROM 1 LWB mdr values TO 1 UPB mdr values
DO
STRING separator := ": [";
print( ( whole( mdr pos, -3 ) ) );
FOR val pos FROM 2 LWB mdr values TO 2 UPB mdr values
DO
print( ( separator + whole( mdr values[ mdr pos, val pos ], 0 ) ) );
separator := ", "
OD;
print( ( "]", newline ) )
OD
 
END; # tabulate mdr #
 
main:(
print md root( 123321 );
print md root( 7739 );
print md root( 893 );
print md root( 899998 );
tabulate mdr( 5 )
)</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}}==
<lang bracmat>(
3,049

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.