Sum of the digits of n is substring of n: Difference between revisions
Content added Content deleted
(RPL: add section) |
No edit summary |
||
Line 810: | Line 810: | ||
{{out}} |
{{out}} |
||
<pre>0 1 2 3 4 5 6 7 8 9 10 20 30 40 50 60 70 80 90 100 109 119 129 139 149 159 169 179 189 199 200 300 400 500 600 700 800 900 910 911 912 913 914 915 916 917 918 919</pre> |
<pre>0 1 2 3 4 5 6 7 8 9 10 20 30 40 50 60 70 80 90 100 109 119 129 139 149 159 169 179 189 199 200 300 400 500 600 700 800 900 910 911 912 913 914 915 916 917 918 919</pre> |
||
=={{header|Delphi}}== |
|||
{{works with|Delphi|6.0}} |
|||
{{libheader|SysUtils,StdCtrls}} |
|||
<syntaxhighlight lang="Delphi"> |
|||
{This code would normally be in a library, but is included here for clarity} |
|||
procedure GetDigits(N: integer; var IA: TIntegerDynArray); |
|||
{Get an array of the integers in a number} |
|||
{Numbers returned from least to most significant} |
|||
var T,I,DC: integer; |
|||
begin |
|||
DC:=Trunc(Log10(N))+1; |
|||
SetLength(IA,DC); |
|||
for I:=0 to DC-1 do |
|||
begin |
|||
T:=N mod 10; |
|||
N:=N div 10; |
|||
IA[I]:=T; |
|||
end; |
|||
end; |
|||
procedure SumDigitsSubstring(Memo: TMemo); |
|||
var N,J,Cnt,Sum: integer; |
|||
var Dg: TIntegerDynArray; |
|||
var NS,SS,S: string; |
|||
begin |
|||
S:=''; |
|||
Cnt:=0; |
|||
for N:=0 to 1000-1 do |
|||
begin |
|||
GetDigits(N,Dg); |
|||
Sum:=0; |
|||
for J:=0 to High(Dg) do |
|||
Sum:=Sum+Dg[J]; |
|||
NS:=IntToStr(N); |
|||
SS:=IntToStr(Sum); |
|||
if Pos(SS,NS)>0 then |
|||
begin |
|||
Inc(Cnt); |
|||
S:=S+Format('%4d',[N]); |
|||
if (Cnt mod 10)=0 then S:=S+CRLF; |
|||
end; |
|||
end; |
|||
Memo.Lines.Add(S); |
|||
end; |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0 1 2 3 4 5 6 7 8 9 |
|||
10 20 30 40 50 60 70 80 90 100 |
|||
109 119 129 139 149 159 169 179 189 199 |
|||
200 300 400 500 600 700 800 900 910 911 |
|||
912 913 914 915 916 917 918 919 |
|||
Elapsed Time: 1.433 ms. |
|||
</pre> |
|||
=={{header|Draco}}== |
=={{header|Draco}}== |