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 # |
|||
INT rest := number; |
|||
WHILE result TIMESAB ( rest MOD 10 ); |
|||
BEGIN |
|||
rest OVERAB 10; |
|||
rest > 0 |
|||
DO SKIP OD; |
|||
result |
|||
END; # DIGITPRODUCT # |
|||
# calculates the multiplicative digital root and persistence of number # |
|||
INT result := 1; |
|||
INT |
OP MDROOT = ( INT number )DR: |
||
BEGIN |
|||
INT mp := 0; |
|||
INT mdr := ABS number; |
|||
WHILE mdr > 9 DO |
|||
mp +:= 1; |
|||
mdr := DIGITPRODUCT mdr |
|||
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 |
|||
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; |
|||
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 |
|||
IF mdr counts[ root OF mdr ] < number of values THEN |
|||
# need more values with this multiplicative digital root # |
|||
print( ( whole( number, -6 ) |
|||
values found +:= 1; |
|||
mdr counts[ root OF mdr ] +:= 1; |
|||
mdr values[ root OF mdr, mdr counts[ root OF mdr ] ] := value |
|||
, newline |
|||
FI |
|||
OD; |
|||
# 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 # |
|||
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 |
|||
print( ( separator + whole( mdr values[ mdr pos, val pos ], 0 ) ) ); |
|||
separator := ", " |
|||
OD; |
|||
print( ( "]", newline ) ) |
|||
OD |
|||
# find the first few numbers with each possible mdr # |
|||
END; # tabulate mdr # |
|||
# 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 ) |
||
END</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |