Jump to content

Digital root/Multiplicative digital root: Difference between revisions

m
→‎{{header|Free Pascal}}: first occurence of persistence 0..11 .Inline GetMulDigits
(→‎{{header|PARI/GP}}: append =={header|Pascal}}== find the first 9 brute force)
m (→‎{{header|Free Pascal}}: first occurence of persistence 0..11 .Inline GetMulDigits)
Line 2,042:
inspired by [[Worthwhile_task_shaving]] :-)<BR>
Brute force speed up GetMulDigits.
<lang pascal>program MultRoot;
program MultRoot;
{$IFDEF FPC}
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}{$CODEALIGN proc=16}
Line 2,060 ⟶ 2,059:
end;
const
Testnumbers : array[0..1416] of Uint64 =(123321,7739,893,899998,
18446743999999999999,
//first occurence of persistence 0..11
// From http://mathworld.wolfram.com/MultiplicativePersistence.html
290, 4710, 27725, 76939,77,679, 88676788, 18688968889, 26787892677889,
2689988926888999, 3778888999, 277777788888899);
 
var
Line 2,083 ⟶ 2,082:
end;
 
function GetMulDigits(n:Uint64):UInt64;inline;
var
pMul3Dgt :^tMul3Dgt;
i,q :Uint64;
begin
i := 1;
pMul3Dgt := @Mul3Dgt[0];
iresult := 1;
while n >= 1000 do
begin
q := n div 1000;
iresult *= pMul3Dgt^[n-1000*q];
n := q;
end;
If n>=100 then
iresult *= Mul3DgtpMul3Dgt^[n]
else
if n>=10 then
iresult *= pMul3Dgt^[n+100]
else
iresult *= n;//pMul3Dgt^Mul3Dgt[n+110]
GetMulDigits := i;
end;
 
Line 2,123 ⟶ 2,121:
 
const
MaxCountMaxDgtCount = 9;
var
//all initiated with 0
MulRoot:tMulRoot;
Sol : array[0..9,0..MaxCountMaxDgtCount-1] of tMulRoot;
SolIds : array[0..9] of Int32;
i,idx,mr,AlreadyDone : Int32;
 
BEGIN
InitMulDgt;
 
AlreadyDone := 10;//0..9
MulRoot.mrNum := 0;
repeat
Line 2,139 ⟶ 2,138:
mr := MulRoot.mrMul;
idx := SolIds[mr];
If idx<MaxCountMaxDgtCount then
begin
Sol[mr,idx]:= MulRoot;
inc(idx);
SolIds[mr]:= idx;
if idx =MaxCountMaxDgtCount then
dec(AlreadyDone);
end;
Line 2,153 ⟶ 2,152:
begin
write(i:3,':');
For idx := 0 to MaxCountMaxDgtCount-1 do
write(Sol[i,idx].mrNum:MaxCountMaxDgtCount+1);
writeln;
end;
Line 2,172 ⟶ 2,171:
{{out|@TIO.RUN}}
<pre>
Real time: 21.086580 s CPU share: 99.4759 % testinline tilGetMulDigits 111,111,111->runtime 100%->76%
MDR: First
0: 0 10 20 25 30 40 45 50 52
Line 2,185 ⟶ 2,184:
9: 9 19 33 91 119 133 191 313 331
 
number mulroot persitancepersistence
123321 8 3
7739 8 3
Line 2,191 ⟶ 2,190:
899998 0 2
18446743999999999999 0 2
29 0 8 0 2 0
4710 60 31
277 25 4 0 4 2
769 39 6 4 5 3
8867 77 0 8 6 4
186889 679 0 6 7 5
2678789 6788 0 86
26899889 68889 0 97
2677889 0 8
26888999 0 9
3778888999 0 10
277777788888899 0 11</pre>
</pre>
 
=={{header|Perl}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.