Anonymous user
Own digits power sum: Difference between revisions
m
→{{header|Pascal}}: extend to 19 digits one more than ruby,hoping overflow will not generate the needed digits.
(→Combinations with repetitions: Separated Ruby and Raku examples.) |
m (→{{header|Pascal}}: extend to 19 digits one more than ruby,hoping overflow will not generate the needed digits.) |
||
Line 682:
uses
SysUtils;
const
MAXBASE = 10
MaxDgtVal = MAXBASE - 1;
MaxDgtCount = 19;
type
tPower = array[tValues] of Uint64;
var
PowerDgt: array[
CombIdx: array of Int8;
Numbers : array of
rec_cnt : NativeInt;
function InitCombIdx(ElemCount: Byte): pbyte;
begin
Line 703 ⟶ 705:
Result := @CombIdx[0];
end;
function Init(ElemCount:byte):pByte;
var
pP1,Pp2 : pUint64;
i, j: Int32;
begin
pP1 := @PowerDgt[low(tDgtCnt)];
for i in tValues do
pP1[i] := 1;
pP1[0] := 0;
for j := low(tDgtCnt) + 1 to High(tDgtCnt) do
Begin
pP2 := @PowerDgt[j];
for i in tValues do
pP2[i] := pP1[i]*i;
pP1 := pP2;
end;
result := InitCombIdx(ElemCount);
end;
function NextCombWithRep(pComb: pByte; MaxVal, ElemCount: UInt32): boolean;
var
i,
begin
i := -1;
Line 715 ⟶ 736:
break;
until i > ElemCount;
Result := i >= ElemCount;
dgt +=1;
Line 721 ⟶ 743:
i -= 1;
until i < 0;
end;
function GetPowerSum(minpot:nativeInt;digits:pbyte;var UD_tmp :tUsedDigits):NativeInt;
var
pPower : pUint64;
res,r
dgt :Int32;
begin
dgt := minpot-1;
res := 0;
repeat
res += pPower[digits[dgt]];
until dgt<0;
//convert res into digits
result := minPot;
repeat
dec(result);
r := res DIV MAXBASE;
UD_tmp[res-r*MAXBASE]-= 1;
res := r;
until r = 0;
end;
procedure calcNum(minPot:Int32;digits:pbyte);
var
begin
fillchar(UD,SizeOf(UD),#0);
For i := minpot-1 downto 0 do
i := GetPowerSum(minpot,digits,UD);
//powersum to small
if i
EXIT;
if i
begin
while (i <= minPot) and (UD[digits[i]] = 0) do
// all digits are in sum then solution found.
if i > minPot then
begin
setlength(Numbers, Length(Numbers) + 1);
end;
end;
end;
var
digits : pByte;
Line 810 ⟶ 804:
tmp: Uint64;
i, j : Int32;
begin
digits := Init(
T0 := GetTickCount64;
rec_cnt := 0;
For i :=
Begin
digits := InitCombIdx(MaxDgtCount);
repeat
calcnum(i,digits);
inc(rec_cnt);
until NextCombWithRep(digits,MaxDgtVal,
end;
T0 := GetTickCount64-T0;
writeln(rec_cnt DIV rounds,' recursions in runtime ',T0/rounds:5:2,' ms');
Line 847 ⟶ 830:
Numbers[j] := tmp;
end;
setlength(Numbers, j + 1);
for i := 0 to High(Numbers) do
writeln(i+1:3,Numbers[i]:
{$IFDEF WINDOWS}
readln;
{$ENDIF}
end.
</lang>
{{out}}
<pre style="height:180px">
TIO.RUN CPU share: 99.49 %
20029944 recursions in runtime 1755.00 ms
found 41
1 153
10
17 24678051
18 88593477
19 146511208
20 472335975
21 534494836
22 912985153
23 4679307774
24 32164049650
25 32164049651
26 40028394225
27 42678290603
28 44708635679
29 49388550606
30 82693916578
31 94204591914
32 28116440335967
33 4338281769391370
34 4338281769391371
35 21897142587612075
36 35641594208964132
37 35875699062250035
38 1517841543307505039
39 3289582984443187032
40 4498128791164624869
41 4929273885928088826</pre>
=={{header|Perl}}==
|