Digital root/Multiplicative digital root: Difference between revisions

Content added Content deleted
(Added Quackery.)
(→‎{{header|ALGOL 68}}: Revised formatting, use operators instead of procedures)
Line 167: Line 167:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<lang algol68>BEGIN # Multiplicative Digital Roots #
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
# structure to hold the results of calculating the digital root & persistence #
<lang algol68># Multiplicative Digital Roots #
MODE DR = STRUCT( INT root, INT persistence );

# returns the product of the digits of number #
# structure to hold the results of calculating the digital root & persistence #
OP DIGITPRODUCT = ( INT number )INT:
MODE DR = STRUCT( INT root, INT persistence );
BEGIN

INT result := 1;
# calculate the multiplicative digital root and persistence of a number #
PROC md root = ( INT number )DR:
INT rest := number;
WHILE result TIMESAB ( rest MOD 10 );
BEGIN
rest OVERAB 10;

# calculate the product of the digits of a number #
rest > 0
PROC digit product = ( INT number )INT:
DO SKIP OD;
BEGIN
result
END; # DIGITPRODUCT #

# calculates the multiplicative digital root and persistence of number #
INT result := 1;
INT rest := number;
OP MDROOT = ( INT number )DR:
BEGIN

WHILE
INT mp := 0;
result TIMESAB ( rest MOD 10 );
INT mdr := ABS number;
rest OVERAB 10;
WHILE mdr > 9 DO
rest > 0
mp +:= 1;
DO
mdr := DIGITPRODUCT mdr
SKIP
OD;
OD;
( mdr, mp )
END; # MDROOT #

# prints a number and its MDR and MP #
result
PROC print md root = ( INT number )VOID:
END; # digit product #
BEGIN

INT mp := 0;
DR mdr = MDROOT( number );
print( ( whole( number, -6 ), ": MDR: ", whole( root OF mdr, 0 ), ", MP: ", whole( persistence OF mdr, -2 ), newline ) )
INT mdr := ABS number;
END; # print md root #

# prints the first few numbers with each possible Multiplicative Digital #
WHILE mdr > 9
# Root. The number of values to print is specified as a parameter #
DO
PROC tabulate mdr = ( INT number of values )VOID:
mp +:= 1;
mdr := digit product( mdr )
BEGIN
[ 0 : 9, 1 : number of values ]INT mdr values;
OD;
[ 0 : 9 ]INT mdr counts;

mdr counts[ AT 1 ] := ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
( mdr, mp )
# find the first few numbers with each possible mdr #
END; # md root #
INT values found := 0;

INT required values := 10 * number of values;
# prints a number and its MDR and MP #
FOR value FROM 0 WHILE values found < required values DO
PROC print md root = ( INT number )VOID:
DR mdr = MDROOT value;
BEGIN
DR mdr = md root( number );
IF mdr counts[ root OF mdr ] < number of values THEN
# need more values with this multiplicative digital root #
print( ( whole( number, -6 )
, ": MDR: ", whole( root OF mdr, 0 )
values found +:= 1;
, ", MP: ", whole( persistence OF mdr, -2 )
mdr counts[ root OF mdr ] +:= 1;
mdr values[ root OF mdr, mdr counts[ root OF mdr ] ] := value
, newline
)
FI
)
OD;
END; # print md root #
# print the values #
print( ( "MDR: [n0..n" + whole( number of values - 1, 0 ) + "]", newline ) );

print( ( "=== ========", newline ) );
# prints the first few numbers with each possible Multiplicative Digital #
# Root. The number of values to print is specified as a parameter #
FOR mdr pos FROM 1 LWB mdr values TO 1 UPB mdr values DO
STRING separator := ": [";
PROC tabulate mdr = ( INT number of values )VOID:
print( ( whole( mdr pos, -3 ) ) );
BEGIN
FOR val pos FROM 2 LWB mdr values TO 2 UPB mdr values DO

[ 0 : 9, 1 : number of values ]INT mdr values;
print( ( separator + whole( mdr values[ mdr pos, val pos ], 0 ) ) );
[ 0 : 9 ]INT mdr counts;
separator := ", "
mdr counts[ AT 1 ] := ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
OD;
print( ( "]", newline ) )

OD
# find the first few numbers with each possible mdr #
END; # tabulate mdr #

INT values found := 0;
# task test cases #
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( 123321 );
print md root( 7739 );
print md root( 7739 );
Line 269: Line 236:
print md root( 899998 );
print md root( 899998 );
tabulate mdr( 5 )
tabulate mdr( 5 )
)</lang>
END</lang>
{{out}}
{{out}}
<pre>
<pre>