Disarium numbers: Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) (Add Miranda) |
No edit summary |
||
Line 994: | Line 994: | ||
2427 |
2427 |
||
2646798</pre> |
2646798</pre> |
||
=={{header|Delphi}}== |
|||
{{works with|Delphi|6.0}} |
|||
{{libheader|SysUtils,StdCtrls}} |
|||
Finds the first 19 numbers in 425 miliseconds. It uses a look up table for the powers and tests about 5 million numbers per second. However, this is not fast enough to find the 20th number. By my calculation, at this speed, it would only take 77,000 years. In other words, the brute force method can't be used to find the 20th number. |
|||
<syntaxhighlight lang="Delphi"> |
|||
{Table to speed up calculating powers. Contains all the powers |
|||
of the digits 0..9 raised to the 0..21 power} |
|||
const PowersTable: array [0..21,0..9] of int64 = ( |
|||
($01,$01,$01,$01,$01,$01,$01,$01,$01,$01), |
|||
($00,$01,$02,$03,$04,$05,$06,$07,$08,$09), |
|||
($00,$01,$04,$09,$10,$19,$24,$31,$40,$51), |
|||
($00,$01,$08,$1B,$40,$7D,$D8,$157,$200,$2D9), |
|||
($00,$01,$10,$51,$100,$271,$510,$961,$1000,$19A1), |
|||
($00,$01,$20,$F3,$400,$C35,$1E60,$41A7,$8000,$E6A9), |
|||
($00,$01,$40,$2D9,$1000,$3D09,$B640,$1CB91,$40000,$81BF1), |
|||
($00,$01,$80,$88B,$4000,$1312D,$44580,$C90F7,$200000,$48FB79), |
|||
($00,$01,$100,$19A1,$10000,$5F5E1,$19A100,$57F6C1,$1000000,$290D741), |
|||
($00,$01,$200,$4CE3,$40000,$1DCD65,$99C600,$267BF47,$8000000,$17179149), |
|||
($00,$01,$400,$E6A9,$100000,$9502F9,$39AA400,$10D63AF1,$40000000,$CFD41B91), |
|||
($00,$01,$800,$2B3FB,$400000,$2E90EDD,$159FD800,$75DB9C97,$200000000,$74E74F819), |
|||
($00,$01,$1000,$81BF1,$1000000,$E8D4A51,$81BF1000,$339014821,$1000000000,$41C21CB8E1), |
|||
($00,$01,$2000,$1853D3,$4000000,$48C27395,$30A7A6000,$168F08F8E7,$8000000000,$24FD3027FE9), |
|||
($00,$01,$4000,$48FB79,$10000000,$16BCC41E9,$123EDE4000,$9DE93ECE51,$40000000000,$14CE6B167F31), |
|||
($00,$01,$8000,$DAF26B,$40000000,$71AFD498D,$6D79358000,$45160B7A437,$200000000000,$BB41C3CA78B9), |
|||
($00,$01,$10000,$290D741,$100000000,$2386F26FC1,$290D7410000,$1E39A5057D81,$1000000000000,$6954FE21E3E81), |
|||
($00,$01,$20000,$7B285C3,$400000000,$B1A2BC2EC5,$F650B860000,$D39383266E87,$8000000000000,$3B3FCEF3103289), |
|||
($00,$01,$40000,$17179149,$1000000000,$3782DACE9D9,$5C5E45240000,$5C908960D05B1,$40000000000000,$2153E468B91C6D1), |
|||
($00,$01,$80000,$4546B3DB,$4000000000,$1158E460913D,$22A359ED80000,$287F3C1A5B27D7,$200000000000000,$12BF307AE81FFD59), |
|||
($00,$01,$100000,$CFD41B91,$10000000000,$56BC75E2D631,$CFD41B9100000,$11B7AA4B87E16E1,$1000000000000000,$A8B8B452291FE821), |
|||
($00,$01,$200000,$26F7C52B3,$40000000000,$1B1AE4D6E2EF5,$4DEF8A56600000,$7C05A810B72A027,$8000000000000000,$EE7E56E3721F2929)); |
|||
function GetPower(X,Y: integer): int64; |
|||
{Extract power from table} |
|||
begin |
|||
Result:=PowersTable[Y,X]; |
|||
end; |
|||
function IsDisarium(N: integer): boolean; |
|||
{Sum all powers of the digits raised to position power} |
|||
var S: string; |
|||
var I,J: integer; |
|||
var Sum: int64; |
|||
begin |
|||
Sum:=0; |
|||
S:=IntToStr(N); |
|||
for I:=1 to Length(S) do |
|||
begin |
|||
Sum:=Sum+GetPower(byte(S[I])-$30,I); |
|||
end; |
|||
Result:=Sum=N; |
|||
end; |
|||
procedure ShowDisariumNumbers(Memo: TMemo); |
|||
{Show Disarium numbers up to specified limit} |
|||
{Processes about 5 million numbers per second} |
|||
var I,Cnt: int64; |
|||
begin |
|||
Cnt:=0; |
|||
I:=0; |
|||
while I<High(int64) do |
|||
begin |
|||
if IsDisarium(I) then |
|||
begin |
|||
Inc(Cnt); |
|||
Memo.Lines.Add(IntToStr(Cnt)+': '+IntToStr(I)); |
|||
if Cnt>=19 then break; |
|||
end; |
|||
Inc(I); |
|||
end; |
|||
end; |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
1: 0 |
|||
2: 1 |
|||
3: 2 |
|||
4: 3 |
|||
5: 4 |
|||
6: 5 |
|||
7: 6 |
|||
8: 7 |
|||
9: 8 |
|||
10: 9 |
|||
11: 89 |
|||
12: 135 |
|||
13: 175 |
|||
14: 518 |
|||
15: 598 |
|||
16: 1306 |
|||
17: 1676 |
|||
18: 2427 |
|||
19: 2646798 |
|||
</pre> |
|||
=={{header|D}}== |
=={{header|D}}== |