Digital root/Multiplicative digital root: Difference between revisions

Added Easylang
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
(Added Easylang)
 
(5 intermediate revisions by 4 users not shown)
Line 1,085:
}</syntaxhighlight>
The output is similar.
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
proc _mdr n . md mp .
if n > 0
r = 1
.
while n > 0
r *= n mod 10
n = n div 10
.
mp += 1
if r >= 10
_mdr r md mp
else
md = r
.
.
proc mdr n . md mp .
mp = 0
_mdr n md mp
.
numfmt 0 6
print "Number MDR MP"
for v in [ 123321 7739 893 899998 ]
mdr v md mp
print v & md & mp
.
width = 5
len table[] 10 * width
arrbase table[] 0
len tfill[] 10
arrbase tfill[] 0
numfmt 0 0
while total < 10 * width
mdr i md mp
if tfill[md] < width
table[md * width + tfill[md]] = i
tfill[md] += 1
total += 1
.
i += 1
.
print "\nMDR: [n0..n4]"
for i = 0 to 9
write i & ": ["
for j = 0 to width - 1
write table[i * width + j]
if j < width - 1
write ","
.
.
print "]"
.
</syntaxhighlight>
{{out}}
<pre>
Number MDR MP
123321 8 3
7739 8 3
893 2 3
899998 0 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|Elixir}}==
Line 3,193 ⟶ 3,269:
54 63 72 81 0
done...
</pre>
 
=={{header|RPL}}==
≪ 1 SWAP
'''DO''' 10 / LAST MOD ROT * RND SWAP FLOOR
'''UNTIL''' DUP NOT '''END''' DROP
≫ ''''MDGIT'''' STO
≪ 0 '''WHILE''' OVER 9 > '''REPEAT'''
1 + SWAP '''MDGIT''' SWAP '''END''' SWAP R→C
≫ ''''MDPR'''' STO
≪ { 123321 7739 893 899998 } → cases
≪ {} 1 cases SIZE '''FOR''' j cases j GET '''MDPR''' + '''NEXT'''
≫ ≫ ''''TASK1'''' STO
≪ 1 10 '''START''' { 0 0 0 0 0 } '''NEXT''' 10 →LIST 'tab' STO 50 'cnt' STO
1 99999 '''FOR''' j
j '''MDPR''' IM 1 + tab OVER GET
'''IF''' DUP 0 POS '''THEN'''
LAST j PUT 'tab' ROT ROT PUT cnt 1 -
'''IF''' DUP '''THEN''' 'cnt' STO '''ELSE''' 99999 'j' STO '''END'''
'''ELSE''' DROP2 '''END'''
'''NEXT''' tab
≫ ''''TASK2'''' STO
{{out}}
<pre>
2: { (3,8) (3,8) (3,2) (2,0) }
1: { { 10 20 25 30 40 }
{ 1 11 111 1111 11111 }
{ 2 12 21 26 34 }
{ 3 13 31 113 131 }
{ 4 14 22 27 39 }
{ 5 15 35 51 53 }
{ 6 16 23 28 32 }
{ 7 17 71 117 171 }
{ 8 18 24 29 36 }
{ 9 19 33 91 119 } }
</pre>
 
Line 3,237 ⟶ 3,351:
8: [8, 18, 24, 29, 36]
9: [9, 19, 33, 91, 119]
</pre>
 
=={{header|Rust}}==
{{trans|D}}
<syntaxhighlight lang="rust>
// Multiplicative digital root
fn mdroot(n: u32) -> (u32, u32) {
let mut count = 0;
let mut mdr = n;
while mdr > 9 {
let mut m = mdr;
let mut digits_mul = 1;
while m > 0 {
digits_mul *= m % 10;
m /= 10;
}
mdr = digits_mul;
count += 1;
}
return (count, mdr);
}
 
fn main() {
println!("Number: (MP, MDR)\n====== =========");
for n in [123321, 7739, 893, 899998] {
println!("{:6}: {:?}", n, mdroot(n));
}
let mut table = vec![vec![0_u32; 0]; 10];
let mut n = 0;
while table.iter().map(|row| row.len()).min().unwrap() < 5 {
let (_, mdr) = mdroot(n);
table[mdr as usize].push(n);
n += 1;
}
println!("\nMDR First 5 with matching MDR\n=== =========================");
table.sort();
for a in table {
println!("{:2}: {:5}{:6}{:6}{:6}{:6}", a[0], a[0], a[1], a[2], a[3], a[4]);
}
}
</syntaxhighlight>{{out}}
<pre>
Number: (MP, MDR)
====== =========
123321: (3, 8)
7739: (3, 8)
893: (3, 2)
899998: (2, 0)
 
MDR First 5 with matching MDR
=== =========================
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>
 
Line 3,561 ⟶ 3,736:
{{libheader|Wren-fmt}}
The size of some of the numbers here is such that we need to use BigInt.
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt
import "./fmt" for Fmt
 
// Only valid for n > 0 && base >= 2
Line 3,645 ⟶ 3,820:
8: [8, 18, 24, 29, 36]
9: [9, 19, 33, 91, 119]
</pre>
 
=={{header|XPL0}}==
{{trans|ALGOL W}}
<syntaxhighlight lang "XPL0"> \Calculate the Multiplicative Digital Root (MDR) and
\ Multiplicative Persistence (MP) of N
procedure GetMDR ( N, MDR, MP );
integer N, MDR, MP, V;
begin
MP(0) := 0;
MDR(0) := abs( N );
while MDR(0) > 9 do begin
V := MDR(0);
MDR(0) := 1;
repeat
MDR(0) := MDR(0) * rem( V / 10 );
V := V / 10;
until V = 0;
MP(0) := MP(0) + 1;
end; \while_mdr_gt_9
end; \GetMDR
 
define RequiredMDRs = 5;
integer FirstFew ( 9+1, 1+RequiredMDRs );
integer MDRFound ( 9+1 );
integer TotalFound, FoundPos, RequiredTotal, N, I, V, L;
integer MDR, MP;
begin
\task test cases
Text(0, " N MDR MP^m^j" );
L := [ 123321, 7739, 893, 899998 ];
for N := 0 to 3 do begin
GetMDR( L(N), @MDR, @MP );
Format(8, 0); RlOut(0, float(L(N)));
Format(4, 0); RlOut(0, float(MDR));
Format(3, 0); RlOut(0, float(MP));
CrLf(0)
end; \for_N
\find the first 5 numbers with each possible MDR
begin
for I := 0 to 9 do MDRFound( I ) := 0;
TotalFound := 0;
RequiredTotal := 10 * RequiredMDRs;
N := -1;
while TotalFound < RequiredTotal do begin
N := N + 1;
GetMDR( N, @MDR, @MP );
if MDRFound( MDR ) < RequiredMDRs then begin
\Found another number with this MDR and haven't found enough
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 and numbers
Text(0, "MDR: [N0..N4]^m^j" );
Text(0, "=== ========^m^j" );
for V := 0 to 9 do begin
ChOut(0, ^ ); IntOut(0, V); Text(0, ": [");
for FoundPos := 1 to RequiredMDRs do begin
if FoundPos > 1 then Text( 0, ", " );
IntOut( 0, FirstFew( V, FoundPos ) )
end; \for_FoundPos
Text(0, "]^m^j")
end \for_v
end
end</syntaxhighlight>
{{out}}
<pre>
N MDR MP
123321 8 3
7739 8 3
893 2 3
899998 0 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>
 
1,978

edits