Digital root/Multiplicative digital root: Difference between revisions

→‎{{header|ALGOL 68}}: Revised formatting, use operators instead of procedures
(Added Quackery.)
(→‎{{header|ALGOL 68}}: Revised formatting, use operators instead of procedures)
Line 167:
 
=={{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 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;
OP MDROOT = ( INT number rest )DR:= number;
BEGIN
 
WHILE INT mp := 0;
resultINT TIMESABmdr (:= restABS MOD 10 )number;
restWHILE mdr > OVERAB9 10;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 : DR mdr = 0MDROOT( 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 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 values found OF mdr, 0 ) +:= 1;
, ", MP: ", whole( persistence mdr counts[ root OF mdr, -2] +:= )1;
mdr values[ root OF mdr, mdr counts[ root OF mdr ] ] := value
, newline
) FI
) OD;
END; # print mdthe rootvalues #
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 parameterFOR 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 print( ( separator + whole( mdr values[ mdr pos, val pos ], 0 ) ) );
[ 0 : 9 separator := ", ]INT mdr counts;"
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# valuestask foundtest cases := 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 );
Line 269 ⟶ 236:
print md root( 899998 );
tabulate mdr( 5 )
)END</lang>
{{out}}
<pre>
3,043

edits