Narcissistic decimal number: Difference between revisions
Content added Content deleted
(add RPL) |
No edit summary |
||
Line 2,069: | Line 2,069: | ||
length 16: 4338281769391371 4338281769391370</pre> |
length 16: 4338281769391371 4338281769391370</pre> |
||
With LDC2 compiler and maxLength=16 the run-time is about 0.64 seconds. |
With LDC2 compiler and maxLength=16 the run-time is about 0.64 seconds. |
||
=={{header|Delphi}}== |
|||
{{works with|Delphi|6.0}} |
|||
{{libheader|SysUtils,StdCtrls}} |
|||
<syntaxhighlight lang="Delphi"> |
|||
function IntPower(N,P: integer): integer; |
|||
var I: integer; |
|||
begin |
|||
Result:=N; |
|||
for I:=1 to P-1 do Result:=Result * N; |
|||
end; |
|||
function IsNarcisNumber(N: integer): boolean; |
|||
{Test if this a narcisstic number} |
|||
{i.e. the sum of each digit raised to power length = N} |
|||
var S: string; |
|||
var I,Sum,B,P: integer; |
|||
begin |
|||
S:=IntToStr(N); |
|||
Sum:=0; |
|||
P:=Length(S); |
|||
for I:=1 to Length(S) do |
|||
begin |
|||
B:=byte(S[I])-$30; |
|||
Sum:=Sum+IntPower(B,P); |
|||
end; |
|||
Result:=Sum=N; |
|||
end; |
|||
procedure ShowNarcisNumber(Memo: TMemo); |
|||
{Show first 25 narcisstic number} |
|||
var I,Cnt: integer; |
|||
var S: string; |
|||
begin |
|||
Cnt:=0; |
|||
S:=''; |
|||
for I:=0 to High(Integer) do |
|||
if IsNarcisNumber(I) then |
|||
begin |
|||
S:=S+Format('%10d',[I]); |
|||
Inc(Cnt); |
|||
if (Cnt mod 5)=0 then S:=S+#$0D#$0A; |
|||
if Cnt>=25 then break; |
|||
end; |
|||
Memo.Lines.Add(S); |
|||
end; |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0 1 2 3 4 |
|||
5 6 7 8 9 |
|||
153 370 371 407 1634 |
|||
8208 9474 54748 92727 93084 |
|||
548834 1741725 4210818 9800817 9926315 |
|||
</pre> |
|||
=={{header|Elixir}}== |
=={{header|Elixir}}== |