September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,66 @@
begin
% calculate the Multiplicative Digital Root (mdr) and Multiplicative Persistence (mp) of n %
procedure getMDR ( integer value n
; integer result mdr, mp
) ;
begin
mp := 0;
mdr := abs n;
while mdr > 9 do begin
integer v;
v := mdr;
mdr := 1;
while begin
mdr := mdr * ( v rem 10 );
v := v div 10;
v > 0
end do begin end;
mp := mp + 1;
end while_mdr_gt_9 ;
end getMDR ;
% task test cases %
write( " N MDR MP" );
for n := 123321, 7739, 893, 899998 do begin
integer mdr, mp;
getMDR( n, mdr, mp );
write( s_w := 1, i_w := 8, n, i_w := 3, mdr, i_w := 2, mp )
end for_n ;
begin % find the first 5 numbers with each possible MDR %
integer requiredMdrs;
requiredMdrs := 5;
begin
integer array firstFew ( 0 :: 9, 1 :: requiredMdrs );
integer array mdrFOund ( 0 :: 9 );
integer totalFound, requiredTotal, n;
for i := 0 until 9 do mdrFound( i ) := 0;
totalFound := 0;
requiredTotal := 10 * requiredMdrs;
n := -1;
while totalFound < requiredTotal do begin
integer mdr, mp;
n := n + 1;
getMDR( n, mdr, mp );
if mdrFound( mdr ) < requiredMdrs then begin
% found another number with this MDR and haven't found enough yet %
totalFound := totalFound + 1;
mdrFound( mdr ) := mdrFound( mdr ) + 1;
firstFew( mdr, mdrFound( mdr ) ) := n
end if_found_another_MDR
end while_totalFound_lt_requiredTotal ;
% print the table of MDRs andnumbers %
write( "MDR: [n0..n4]" );
write( "=== ========" );
for v := 0 until 9 do begin
write( i_w := 3, s_w := 0, v, ": [" );
for foundPos := 1 until requiredMdrs do begin
if foundPos > 1 then writeon( s_w := 0, ", " );
writeon( i_w := 1, s_w := 0, firstFew( v, foundPos ) )
end for_foundPos ;
writeon( s_w := 0, "]" )
end for_v
end
end
end.

View file

@ -0,0 +1,69 @@
MODULE MDR;
IMPORT StdLog, Strings, TextMappers, DevCommanders;
PROCEDURE CalcMDR(x: LONGINT; OUT mdr, mp: LONGINT);
VAR
str: ARRAY 64 OF CHAR;
i: INTEGER;
BEGIN
mdr := 1; mp := 0;
LOOP
Strings.IntToString(x,str);
IF LEN(str$) = 1 THEN mdr := x; EXIT END;
i := 0;mdr := 1;
WHILE i < LEN(str$) DO
mdr := mdr * (ORD(str[i]) - ORD('0'));
INC(i)
END;
INC(mp);
x := mdr
END
END CalcMDR;
PROCEDURE Do*;
VAR
mdr,mp: LONGINT;
s: TextMappers.Scanner;
BEGIN
s.ConnectTo(DevCommanders.par.text);
s.SetPos(DevCommanders.par.beg);
REPEAT
s.Scan;
IF (s.type = TextMappers.int) OR (s.type = TextMappers.lint) THEN
CalcMDR(s.int,mdr,mp);
StdLog.Int(s.int);
StdLog.String(" MDR: ");StdLog.Int(mdr);
StdLog.String(" MP: ");StdLog.Int(mp);StdLog.Ln
END
UNTIL s.rider.eot;
END Do;
PROCEDURE Show(i: INTEGER; x: ARRAY OF LONGINT);
VAR
k: INTEGER;
BEGIN
StdLog.Int(i);StdLog.String(": ");
FOR k := 0 TO LEN(x) - 1 DO
StdLog.Int(x[k])
END;
StdLog.Ln
END Show;
PROCEDURE FirstFive*;
VAR
i,j: INTEGER;
five: ARRAY 5 OF LONGINT;
x,mdr,mp: LONGINT;
BEGIN
FOR i := 0 TO 9 DO
j := 0;x := 0;
WHILE (j < LEN(five)) DO
CalcMDR(x,mdr,mp);
IF mdr = i THEN five[j] := x; INC(j) END;
INC(x)
END;
Show(i,five)
END
END FirstFive;
END MDR.

View file

@ -0,0 +1,52 @@
' FB 1.05.0 Win64
Function multDigitalRoot(n As UInteger, ByRef mp As Integer, base_ As Integer = 10) As Integer
Dim mdr As Integer
mp = 0
Do
mdr = IIf(n > 0, 1, 0)
While n > 0
mdr *= n Mod base_
n = n \ base_
Wend
mp += 1
n = mdr
Loop until mdr < base_
Return mdr
End Function
Dim As Integer mdr, mp
Dim a(3) As UInteger = {123321, 7739, 893, 899998}
For i As UInteger = 0 To 3
mp = 0
mdr = multDigitalRoot(a(i), mp)
Print a(i); Tab(10); "MDR ="; mdr; Tab(20); "MP ="; mp
Print
Next
Print
Print "MDR 1 2 3 4 5"
Print "=== ==========================="
Print
Dim num(0 To 9, 0 To 5) As UInteger '' all zero by default
Dim As UInteger n = 0, count = 0
Do
mdr = multDigitalRoot(n, mp)
If num(mdr, 0) < 5 Then
num(mdr, 0) += 1
num(mdr, num(mdr, 0)) = n
count += 1
End If
n += 1
Loop Until count = 50
For i As UInteger = 0 To 9
Print i; ":" ;
For j As UInteger = 1 To 5
Print Using "######"; num(i, j);
Next j
Print
Next i
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,51 @@
// version 1.1.2
fun multDigitalRoot(n: Int): Pair<Int, Int> = when {
n < 0 -> throw IllegalArgumentException("Negative numbers not allowed")
else -> {
var mdr: Int
var mp = 0
var nn = n
do {
mdr = if (nn > 0) 1 else 0
while (nn > 0) {
mdr *= nn % 10
nn /= 10
}
mp++
nn = mdr
}
while (mdr >= 10)
Pair(mdr, mp)
}
}
fun main(args: Array<String>) {
val ia = intArrayOf(123321, 7739, 893, 899998)
for (i in ia) {
val (mdr, mp) = multDigitalRoot(i)
println("${i.toString().padEnd(9)} MDR = $mdr MP = $mp")
}
println()
println("MDR n0 n1 n2 n3 n4")
println("=== ===========================")
val ia2 = Array(10) { IntArray(6) } // all zero by default
var n = 0
var count = 0
do {
val (mdr, _) = multDigitalRoot(n)
if (ia2[mdr][0] < 5) {
ia2[mdr][0]++
ia2[mdr][ia2[mdr][0]] = n
count++
}
n++
}
while (count < 50)
for (i in 0..9) {
print("$i:")
for (j in 1..5) print("%6d".format(ia2[i][j]))
println()
}
}

View file

@ -1,28 +1,31 @@
/*REXX pgm finds persistence and multiplicative digital root of some #'s*/
numeric digits 100 /*increase the number of digits. */
parse arg x /*get some numbers from the C.L. */
if x='' then x=123321 7739 893 899998 /*use defaults if none specified.*/
say center('number',8) ' persistence multiplicative digital root'
say copies('' ,8) ' '
/* [↑] title and separator. */
do j=1 for words(x); n=word(x,j) /*process each number in the list*/
parse value mdr(n) with mp mdr /*obtain the persistence and MDR.*/
say right(n,8) center(mp,13) center(mdr,30) /*display #, mp, mdr.*/
end /*j*/ /* [↑] show MP and MDR for each #*/
say; target=5
/*REXX program finds the persistence and multiplicative digital root of some numbers.*/
numeric digits 100 /*increase the number of decimal digits*/
parse arg x /*obtain optional arguments from the CL*/
if x='' | x="," then x=123321 7739 893 899998 /*Not specified? Then use the default.*/
say center('number', 8) ' persistence multiplicative digital root'
say copies('' , 8) ' '
/* [↑] the title and separator. */
do j=1 for words(x); n=word(x, j) /*process each number in the X list.*/
parse value MDR(n) with mp mdr /*obtain the persistence and the MDR. */
say right(n,8) center(mp,13) center(mdr,30) /*display a number, persistence, MDR.*/
end /*j*/ /* [↑] show MP & MDR for each number. */
say; target=5
say 'MDR first ' target " numbers that have a matching MDR"
say ' '
do k=0 for 10; hits=0; _= /*show #'s that have an MDR of K.*/
do m=k until hits==target /*find target #s with an MDR of K*/
if word(mdr(m),2)\==k then iterate /*is the MDR what's wanted? */
hits=hits+1; _=space(_ m',') /*yes, we got a hit, add to list.*/
end /*m*/ /* [↑] built a list of MDRs = k */
say " "k': ['strip(_,,',')"]" /*display the K (mdr) and list.*/
end /*k*/ /* [↑] done with the K mdr list.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────MDR subroutine──────────────────────*/
mdr: procedure; parse arg y; y=abs(y) /*get the number and find the MDR*/
do p=1 until y<10 /*find multiplicative digRoot (Y)*/
parse var y 1 r 2; do k=2 to length(y); r=r*substr(y,k,1); end; y=r
end /*p*/ /*wash, rinse, repeat ··· */
return p r /*return the persistence and MDR.*/
do k=0 for 10; hits=0; _= /*show numbers that have an MDR of K. */
do m=k until hits==target /*find target numbers with an MDR of K.*/
if word( MDR(m), 2)\==k then iterate /*is this the MDR that's wanted? */
hits=hits + 1; _=space(_ m',') /*yes, we got a hit, add to the list. */
end /*m*/ /* [↑] built a list of MDRs that = K. */
say " "k': ['strip(_, , ',')"]" /*display the K (MDR) and the list. */
end /*k*/ /* [↑] done with the K MDR list. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
MDR: procedure; parse arg y; y=abs(y) /*get the number and determine the MDR.*/
do p=1 until y<10; parse var y r 2
do k=2 to length(y); r=r * substr(y, k, 1)
end /*k*/
y=r
end /*p*/ /* [↑] wash, rinse, and repeat ··· */
return p r /*return the persistence and the MDR. */

View file

@ -1,72 +1,81 @@
/*REXX pgm finds persistence and multiplicative digital root of some #'s*/
numeric digits 2000 /*increase the number of digits. */
parse arg target x; if \datatype(target,'W') then target=25 /*default?*/
if x='' then x=123321 7739 893 899998 /*use the defaults for X ? */
/*REXX program finds the persistence and multiplicative digital root of some numbers.*/
numeric digits 2000 /*increase the number of decimal digits*/
parse arg target x /*obtain optional arguments from the CL*/
if \datatype(target, 'W') then target=25 /*Not specified? Then use the default.*/
if x='' | x="," then x=123321 7739 893 899998 /* " " " " " " */
say center('number',8) ' persistence multiplicative digital root'
say copies('' ,8) ' '
/* [↑] title and separator. */
do j=1 for words(x); n=abs(word(x,j)) /*process each # in list.*/
parse value mdr(n) with mp mdr /*obtain the persistence and MDR.*/
say right(n,8) center(mp,13) center(mdr,30) /*display #, mp, mdr.*/
end /*j*/ /* [↑] show MP and MDR for each #*/
say /* [↓] show a blank & title line.*/
/* [↑] the title and the separator. */
do j=1 for words(x); n=abs( word(x, j) ) /*process each number in the list. */
parse value MDR(n) with mp mdr /*obtain the persistence and the MDR. */
say right(n,8) center(mp,13) center(mdr,30) /*display the number, persistence, MDR.*/
end /*j*/ /* [↑] show MP and MDR for each number.*/
say /* [↓] show a blank and the title line.*/
say 'MDR first ' target " numbers that have a matching MDR"
say ' ' copies("",(target+(target+1)**2)%2) /*display a sep line.*/
say ' ' copies("",(target+(target+1)**2)%2) /*display a separator line (for title).*/
do k=0 for 9; hits=0; _= /*show #'s that have an MDR of K.*/
if k==7 then _=@7; else /*handle special seven case. */
do k=0 for 9; hits=0 /*show numbers that have an MDR of K. */
_=
if k==7 then _=@7 /*handle the special case of seven. */
else do m=k until hits==target /*find target numbers with an MDR of K.*/
parse var m '' -1 ? /*obtain the right─most digit of M. */
if k\==0 then if ?==0 then iterate
if k==5 then if ?//2==0 then iterate
if k==1 then m=copies(1, hits+1)
else if MDR(m, 1)\==k then iterate
hits=hits+1 /*got a hit, add to the list*/
_=space(_ m) /*elide superfluous blanks. */
if k==3 then do; o=strip(m, 'T', 1) /*strip trailing ones from M*/
if o==3 then m=copies(1, length(m))3 /*make a new M.*/
else do; t=pos(3, m) - 1 /*position of 3 */
m=overlay(3, translate(m, 1, 3), t)
end /* [↑] shift the "3" 1 place left.*/
m=m - 1 /*adjust for DO index increment.*/
end /* [↑] a shortcut to adj DO index*/
end /*m*/ /* [↑] built a list of MDRs = K */
do m=k until hits==target /*find target #s with an MDR of K*/
?=right(m,1) /*obtain right-most digit of M. */
if k\==0 then if ?==0 then iterate
if k==5 then if ?//2==0 then iterate
if k==1 then m=copies(1,hits+1)
else if mdr(m,1)\==k then iterate
hits=hits+1; _=space(_ m) /*yes, we got a hit, add to list.*/
say " "k': ['_"]" /*display the K (MDR) and the list. */
if k==3 then @7=translate(_, 7, k) /*save for later, a special "7" case.*/
end /*k*/ /* [↑] done with the K MDR list. */
if k==3 then do; o=strip(m,'T',1) /*strip trailing ones*/
if o==3 then m=copies(1,length(m))3 /*make new M. */
else do; t=pos(3,m)-1 /*position of 3*/
m=overlay(3,translate(m,1,3),t)
end /* [↑] shift the "3" 1 place left*/
m=m-1 /*adjust for DO index advancement*/
end /* [↑] a shortcut to do DO index*/
end /*m*/ /* [↑] built a list of MDRs = k */
@.= /* [↓] handle MDR of "9" special. */
_=translate(@7, 9, 7) /*translate string for MDR of nine. */
@9=translate(_, , ',') /*remove trailing commas from numbers. */
@3= /*assign null string before building. */
say " "k': ['_"]" /*display the K (mdr) and list.*/
if k==3 then @7=translate(_,7,k) /*save for later, special 7 case.*/
end /*k*/ /* [↑] done with the K mdr list.*/
@.= /* [↓] handle MDR of 9 special. */
_=translate(@7,9,7) /*translate a string for MDR 9. */
@9=translate(_,,',') /*remove trailing commas from #'s*/
@3= /*assine null string before build*/
do j=1 for words(@9) /*process each number for MDR 9. */
_=space(translate(word(@9,j),,9),0) /*remove "9"s using SPACE(x,0)*/
L=length(_)+1 /*use a "fudged" length of the #.*/
new= /*this is the new numbers so far.*/
do k=0 for L; q=insert(3,_,k) /*insert the 1st "3" into the #*/
do i=k to L; z=insert(3,q,i) /* " " 2nd "3" " " "*/
if @.z\=='' then iterate /*if already define, ignore the #*/
@.z=z; new=z new /*define it, and then add to list*/
end /*i*/ /* [↑] end of 2nd insertion of 3*/
end /*k*/ /* [↑] " " 1st " " "*/
@3=space(@3 new) /*remove blanks, then add to list*/
end /*j*/ /* [↑] end of insertion of "3"s.*/
do j=1 for words(@9) /*process each number for MDR 9 case.*/
_=space( translate( word(@9, j), , 9), 0) /*elide all "9"s using SPACE(x,0).*/
L=length(_) + 1 /*use a "fudged" length of the number. */
new= /*these are the new numbers (so far). */
a1=@9; a2=@3; @= /*define three strings for merge.*/
/* [↓] merge two lists, 3s & 9s.*/
do while a1\=='' & a2\=='' /*process while the lists ¬empty.*/
x=word(a1,1); y=word(a2,1); if x=='' | y=='' then leave /*empty?*/
if x<y then do; @=@ x; a1=delword(a1,1,1); end /*add X.*/
else do; @=@ y; a2=delword(a2,1,1); end /*add Y.*/
end /*while ···*/ /* [+] only process just 'nuff. */
@=subword(@,1,target) /*elide the last trailing comma. */
say " "9': ['@"]" /*display the 9 (mdr) and list.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────MDR subroutine──────────────────────*/
mdr: procedure; parse arg y,s /*get the number and find the MDR*/
do p=1 until y<10 /*find multiplicative digRoot (Y)*/
parse var y 1 r 2; do k=2 to length(y); r=r*substr(y,k,1); end; y=r
end /*p*/ /*wash, rinse, repeat ··· */
if s==1 then return r /*return multiplicative dig root.*/
return p r /*return the persistence and MDR.*/
do k=0 for L; q=insert(3, _, k) /*insert the 1st "3" into the number*/
do i=k to L; z=insert(3, q, i) /* " " 2nd "3" " " " */
if @.z\=='' then iterate /*if already define, ignore the number.*/
@.z=z; new=z new /*define it, and then add to the list.*/
end /*i*/ /* [↑] end of 2nd insertion of "3".*/
end /*k*/ /* [↑] " " 1st " " " */
@3=space(@3 new) /*remove blanks, then add to the list.*/
end /*j*/ /* [↑] end of insertion of the "3"s. */
a1=@9; a2=@3 /*define some strings for the merge. */
@= /* [↓] merge two lists, 3s and 9s. */
do while a1\=='' & a2\=='' /*process while the lists aren't empty.*/
x=word(a1, 1); y=word(a2, 1) /*obtain the 1st word in A1 & A2 lists.*/
if x=='' | y=='' then leave /*are X or Y empty? */
if x<y then do; @=@ x; a1=delword(a1, 1, 1); end /*add X to the @ list.*/
else do; @=@ y; a2=delword(a2, 1, 1); end /* " Y " " " " */
end /*while*/ /* [↑] only process just enough nums. */
@=subword(@, 1, target) /*elide the last trailing comma in list*/
say " "9': ['@"]" /*display the "9" (MDR) and the list.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
MDR: procedure; parse arg y,s; y=abs(y) /*get the number and determine the MDR.*/
do p=1 until y<10; parse var y r 2
do k=2 to length(y); r=r * substr(y, k, 1)
end /*k*/
y=r
end /*p*/ /* [↑] wash, rinse, and repeat ··· */
if s==1 then return r /*return multiplicative digital root. */
return p r /*return the persistence and the MDR. */

View file

@ -1,23 +1,22 @@
func mdroot(n) {
var (mdr, persist) = (n, 0)
while (mdr >= 10) {
mdr = mdr.digits.product
mdr = mdr.digits.prod
++persist
}
[mdr, persist]
}
say "Number: MDR MP\n====== === =="
[123321, 7739, 893, 899998].each{|n| "%6d: %3d %3d\n" \
[123321, 7739, 893, 899998].each{|n| "%6d: %3d %3d\n" \
.printf(n, mdroot(n)...) }
var counter = Hash()
Inf.times { |i|
var j = i-1
counter{mdroot(j).first} := [] << j
Inf.times { |j|
counter{mdroot(j).first} := [] << j
break if counter.values.all {|v| v.len >= 5 }
}
 
say "\nMDR: [n0..n4]\n=== ========"
10.times {|i| "%3d: %s\n".printf(i-1, counter{i-1}.first(5)) }
10.times {|i| "%3d: %s\n".printf(i, counter{i}.first(5)) }

View file

@ -0,0 +1,7 @@
fcn mdroot(n){ // Multiplicative digital root
mdr := List(n);
while (mdr[-1] > 9){
mdr.append(mdr[-1].split().reduce('*,1));
}
return(mdr.len() - 1, mdr[-1]);
}

View file

@ -0,0 +1,14 @@
fcn mdroot(n){
count:=0; mdr:=n;
while(mdr > 9){
m:=mdr; digitsMul:=1;
while(m){
reg md;
m,md=m.divr(10);
digitsMul *= md;
}
mdr = digitsMul;
count += 1;
}
return(count, mdr);
}

View file

@ -0,0 +1,15 @@
println("Number: (MP, MDR)\n======= =========");
foreach n in (T(123321, 7739, 893, 899998))
{ println("%7,d: %s".fmt(n, mdroot(n))) }
table:=D([0..9].zip(fcn{List()}).walk()); // dictionary(0:List, 1:List, ...)
n :=0;
while(table.values.filter(fcn(r){r.len()<5})){ // until each entry has >=5 values
mpersistence, mdr := mdroot(n);
table[mdr].append(n);
n += 1;
}
println("\nMP: [n0..n4]\n== ========");
foreach mp in (table.keys.sort()){
println("%2d: %s".fmt(mp, table[mp][0,5])); //print first five values
}