Sum of the digits of n is substring of n: Difference between revisions

Add Refal
(Added Go)
(Add Refal)
 
(65 intermediate revisions by 28 users not shown)
Line 2:
 
;Task:
Find and show numbers &nbsp; '''n''' &nbsp; with property that the sum of the decimal digits of &nbsp; '''n''' &nbsp; is substring of &nbsp; '''n''', &nbsp; where &nbsp; '''n <&nbsp; 1000&lt; &nbsp; 1,000'''
 
 
{{Template:Strings}}
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
{{trans|Nim}}
 
<syntaxhighlight lang="11l">V count = 0
L(n) 1000
I String(sum(String(n).map(Int))) C String(n)
count++
print(f:‘{n:3}’, end' I count % 8 == 0 {"\n"} E ‘ ’)</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
</pre>
 
=={{header|8080 Assembly}}==
<syntaxhighlight lang="8080asm">puts: equ 9
org 100h
lxi h,-1 ; Number
loop: inx h
push h ; Keep number
lxi d,-1000 ; Are we there yet?
dad d
pop d
rc ; If so, stop
push d ; Keep number
lxi h,buf0
call digits ; Get digits
push h ; Keep pointer to digits
call dgsum ; Sum digits
lxi h,buf1
call digits ; Get digits for sum
pop d ; Retrieve pointer to digits of original
push d
call find ; Does the original contain the sum of the digits?
pop d ; Retrieve digit pointer
pop h ; And number
jc loop ; If the sum of the digits is not found, try next
push h
call print ; Otherwise, print it
pop h
jmp loop
;;; Find digits of number in DE, store at HL.
;;; Beginning of string returned in HL.
digits: lxi b,-10 ; Divisor
mvi m,'$' ; String terminator
push h ; Output pointer on stack
digit: xchg ; Number in HL
lxi d,-1 ; Quotient
dgtdiv: inx d ; Trial subtaction
dad b
jc dgtdiv
mvi a,10 ; Calculate value of digit
add l
pop h ; Store digit
dcx h
mov m,a
push h
mov a,d ; Done?
ora e
jnz digit ; If not, find next digit
pop h ; Remove pointer from stack
ret
;;; Calculate sum of digits starting at HL
dgsum: lxi d,0
dgloop: mov a,m
cpi '$'
rz
add e
mov e,a
inx h
jmp dgloop
;;; See if the string at DE contains the string at HL
find: ldax d ; Load character from haystack
cpi '$' ; Reached the end?
stc ; Then it is not found
rz
push d ; Save pointers
push h
xchg ; Swap pointers
floop: ldax d ; Load character from needle
cpi '$' ; Reached the end?
jz found ; Then we found it
cmp m ; Compare to haystack
inx h ; Increment the pointers
inx d
jz floop ; If equal, keep going
pop h ; Restore pointers
pop d
inx d ; Try next position
jmp find
found: pop h ; Clean up stack
pop d
ret
;;; Print number
print: push d
ploop: ldax d
cpi '$'
jz pdone
adi '0'
stax d
inx d
jmp ploop
pdone: xchg
mvi m,13
inx h
mvi m,10
inx h
mvi m,'$'
pop d
mvi c,puts
jmp 5
buf0: equ $+32
buf1: equ $+64</syntaxhighlight>
{{out}}
<pre style='height:50ex;'>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|Action!}}==
<syntaxhighlight lang="action!">INT FUNC SumDigits(INT num)
INT res,a
 
res=0
WHILE num#0
DO
res==+num MOD 10
num=num/10
OD
RETURN (res)
 
BYTE Func IsValidNumber(INT num)
CHAR ARRAY s(5),sub(5)
INT sum,v,len,start
 
sum=SumDigits(num)
StrI(num,s)
FOR len=1 TO s(0)
DO
FOR start=1 TO s(0)-len+1
DO
SCopyS(sub,s,start,start+len-1)
IF ValI(sub)=sum THEN
RETURN (1)
FI
OD
OD
RETURN (0)
 
PROC Main()
INT i,count=[0]
 
FOR i=0 TO 999
DO
IF IsValidNumber(i) THEN
PrintI(i) Put(32)
count==+1
FI
OD
PrintF("%E%EThere are %I numbers",count)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sum_of_the_digits_of_n_is_substring_of_n.png Screenshot from Atari 8-bit computer]
<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
 
There are 48 numbers
</pre>
 
=={{header|ALGOL 68}}==
ALGOL 68G has the procedure "string in string" in the prelude, for other compilers, a version is available here: [[ALGOL_68/prelude]].
<syntaxhighlight lang="algol68">BEGIN # find n where the sum of the digits is a substring of the representaton of n #
INT max number = 1 000;
INT n count := 0;
FOR n FROM 0 TO max number - 1 DO
INT d sum := 0;
INT v := n;
WHILE v > 0 DO
d sum +:= v MOD 10;
v OVERAB 10
OD;
IF string in string( whole( d sum, 0 ), NIL, whole( n, 0 ) ) THEN
# the string representaton of the digit sum is contained in the representation of n #
print( ( " ", whole( n, -4 ) ) );
n count +:= 1;
IF n count MOD 8 = 0 THEN print( ( newline ) ) FI
FI
OD
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
</pre>
 
=={{header|ALGOL-M}}==
<syntaxhighlight lang="algolm">begin
integer function mod(a,b);
integer a,b;
mod := a-a/b*b;
 
integer function digitsum(n);
integer n;
digitsum :=
if n=0 then 0
else mod(n,10) + digitsum(n/10);
 
integer function chop(n);
integer n;
begin
integer i;
i := 1;
while i<n do i := i * 10;
i := i/10;
chop := if i=0 then 0 else mod(n, i);
end;
 
integer function infix(n,h);
integer n,h;
begin
integer pfx, sfx, r;
r := if n=h then 1 else 0;
pfx := h;
while pfx <> 0 do
begin
sfx := pfx;
while sfx <> 0 do
begin
if sfx = n then
begin
r := 1;
go to stop;
end;
sfx := chop(sfx);
end;
pfx := pfx/10;
end;
stop:
infix := r;
end;
 
integer i, n, d;
n := 0;
for i := 0 step 1 until 999 do
begin
d := digitsum(i);
if infix(d, i) = 1 then
begin
if (n-1)/10 <> n/10 then write(i)
else writeon(i);
n := n + 1;
end;
end;
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</pre>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">begin % find numbers n, where the sum of the digits is a substring of n %
% returns true if the digits of s contains the digits of t, false otherwise %
logical procedure containsDigits( integer value s, t ) ;
if s = t then true
else begin
integer tPower, v, u;
logical isContained;
% find the lowest power of 10 that is greater then t %
tPower := 10;
v := abs t;
while v > 9 do begin
tPower := tPower * 10;
v := v div 10
end while_v_gt_9 ;
isContained := false;
v := abs t;
u := abs s;
while not isContained and u > 0 do begin
isContained := ( u rem tPower ) = v;
u := u div 10
end while_not_isContained_and_u_gt_0 ;
isContained
end containsDigits ;
% find and show the matching numbers up to 1000 %
integer nCount;
nCount := 0;
for n := 0 until 999 do begin
integer dSum, v;
dSum := 0;
v := n;
while v > 0 do begin
dSum := dSum + ( v rem 10 );
v := v div 10
end while_v_gt_0 ;
if containsDigits( n, dSum ) then begin
writeon( i_w := 5, s_w := 0, n );
nCount := nCount + 1;
if nCount rem 8 = 0 then write()
end if_n_contains_dSum
end for_n
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
</pre>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">(⊢(/⍨)(∨/⍕∘(+/(⍎¨⍕))⍷⍕)¨)0,⍳999</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</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">print select 1..999 'num ->
contains? to :string num
to :string sum digits num</syntaxhighlight>
 
{{out}}
 
<pre>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|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">result := "", cntr := 1
loop 1000{
n := A_Index-1, sum := 0
for i, v in StrSplit(n)
sum += v
if InStr(n, sum){
result .= n (mod(cntr, 8)?"`t":"`n")
if (++cntr = 50)
break
}
}
MsgBox % result</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</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SUM_OF_THE_DIGITS_OF_N_IS_SUBSTRING_OF_N.AWK
BEGIN {
start = 0
stop = 999
for (i=start; i<=stop; i++) {
if (i ~ ""sum_digits(i)) { # TAWK needs the ""
printf("%4d%1s",i,++count%10?"":"\n")
}
}
printf("\nSum of the digits of n is substring of n %d-%d: %d\n",start,stop,count)
exit(0)
}
function sum_digits(n, i,sum) {
for (i=1; i<=length(n); i++) {
sum += substr(n,i,1)
}
return(sum)
}
</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
Sum of the digits of n is substring of n 0-999: 48
</pre>
 
=={{header|BASIC}}==
<syntaxhighlight lang="basic">10 DEFINT I,J,K
20 FOR I=0 TO 999
30 J=0: K=I
40 IF K>0 THEN J=J+K MOD 10: K=K\10: GOTO 40
41 I$=STR$(I): I$=RIGHT$(I$,LEN(I$)-1)
42 J$=STR$(J): J$=RIGHT$(J$,LEN(J$)-1)
50 IF INSTR(I$,J$) THEN PRINT I,
60 NEXT I</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
</pre>
 
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
 
let dsum(n) = n=0 -> 0, n rem 10 + dsum(n/10)
 
let chop(n) = valof
$( let i=1
while i<n do i := i * 10
i := i / 10
resultis i=0 -> 0, n rem i
$)
 
let infix(n,h) =
n = h -> true,
h = 0 -> false,
infix(n,h/10) -> true,
infix(n,chop(h)) -> true,
false
 
let start() be
$( let c=0
for i=0 to 999 do
$( if infix(dsum(i),i) then
$( writef("%I5",i)
c := c + 1
if c rem 10=0 then wrch('*N')
$)
$)
wrch('*N')
$)</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</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">DigitSum ← +´•Fmt-'0'˙
Contains ← (∨´⍷˜ )○•Fmt
∘‿6⥊ (⊢ Contains DigitSum)¨⊸/↕1000</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
┘</pre>
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
 
int digitSum(int n) {
int s = 0;
do {s += n % 10;} while (n /= 10);
return s;
}
 
int digitSumIsSubstring(int n) {
char s_n[32], s_ds[32];
sprintf(s_n, "%d", n);
sprintf(s_ds, "%d", digitSum(n));
return strstr(s_n, s_ds) != NULL;
}
 
int main() {
int i;
for (i=0; i<1000; i++)
if (digitSumIsSubstring(i))
printf("%d ",i);
printf("\n");
return 0;
}</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</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <iostream>
 
int digitSum(int n) {
int s = 0;
do {s += n % 10;} while (n /= 10);
return s;
}
 
int main() {
for (int i=0; i<1000; i++) {
auto s_i = std::to_string(i);
auto s_ds = std::to_string(digitSum(i));
if (s_i.find(s_ds) != std::string::npos) {
std::cout << i << " ";
}
}
std::cout << std::endl;
return 0;
}</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</pre>
=={{header|CLU}}==
<syntaxhighlight lang="clu">digit_sum = proc (n: int) returns (int)
sum: int := 0
while n > 0 do
sum := sum + n // 10
n := n / 10
end
return (sum)
end digit_sum
 
digit_sum_is_substring = proc (n: int) returns (bool)
n_str: string := int$unparse(n)
ds_str: string := int$unparse(digit_sum(n))
return (string$indexs(ds_str, n_str) ~= 0)
end digit_sum_is_substring
 
match_range = iter (from, to: int, p: proctype (int) returns (bool))
yields (int)
for i: int in int$from_to(from,to) do
if p(i) then yield(i) end
end
end match_range
 
start_up = proc ()
po: stream := stream$primary_output()
col: int := 0
for i: int in match_range(0, 999, digit_sum_is_substring) do
stream$putright(po, int$unparse(i), 4)
col := col + 1
if col // 10 = 0 then stream$putc(po, '\n') end
end
end start_up</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</pre>
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. SUM-SUBSTRING.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CALCULATION.
02 N PIC 9999.
02 X PIC 9.
02 DSUM PIC 99.
02 N-DIGITS REDEFINES N.
03 ND PIC 9 OCCURS 4 TIMES.
02 S-DIGITS REDEFINES DSUM.
03 SUMD PIC 9 OCCURS 2 TIMES.
01 OUTPUT-FORMAT.
02 N-OUT PIC ZZZ9.
PROCEDURE DIVISION.
BEGIN.
PERFORM TESTNUMBER VARYING N FROM 0 BY 1
UNTIL N IS EQUAL TO 1000.
STOP RUN.
TESTNUMBER SECTION.
BEGIN.
PERFORM SUM-DIGITS.
SET X TO 1.
IF DSUM IS LESS THAN 10 GO TO ONE-DIGIT-CHECK.
TWO-DIGIT-CHECK.
IF X IS GREATER THAN 3 GO TO DONE.
IF ND(X) = SUMD(1) AND ND(X + 1) = SUMD(2) GO TO SHOW.
ADD 1 TO X.
GO TO TWO-DIGIT-CHECK.
ONE-DIGIT-CHECK.
IF X IS GREATER THAN 4 GO TO DONE.
IF ND(X) = SUMD(2) GO TO SHOW.
ADD 1 TO X.
GO TO ONE-DIGIT-CHECK.
SHOW.
MOVE N TO N-OUT.
DISPLAY N-OUT.
DONE. EXIT.
SUM-DIGITS SECTION.
BEGIN.
SET DSUM TO 0.
SET X TO 1.
LOOP.
ADD ND(X) TO DSUM.
ADD 1 TO X.
IF X IS LESS THAN 5 GO TO LOOP.</syntaxhighlight>
{{out}}
<pre style='height:50ex;'> 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|Comal}}==
<syntaxhighlight lang="comal">0010 FUNC digit'sum#(n#) CLOSED
0020 sum#:=0
0030 WHILE n# DO sum#:+n# MOD 10;n#:=n# DIV 10
0040 RETURN sum#
0050 ENDFUNC digit'sum#
0060 //
0070 col#:=0
0080 ZONE 4
0090 FOR i#:=0 TO 999 DO
0100 IF STR$(digit'sum#(i#)) IN STR$(i#) THEN
0110 PRINT i#,
0120 col#:+1
0130 IF col# MOD 15=0 THEN PRINT
0140 ENDIF
0150 ENDFOR i#
0160 PRINT
0170 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</pre>
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub digitSum(n: uint16): (s: uint16) is
s := 0;
while n != 0 loop
s := s + n % 10;
n := n / 10;
end loop;
end sub;
 
sub contains(haystack: [uint8], needle: [uint8]): (r: uint8) is
r := 0;
while [haystack] != 0 loop
var h := haystack;
var n := needle;
while [h] == [n] and [h] != 0 and [n] != 0 loop
h := @next h;
n := @next n;
end loop;
if [n] == 0 then
r := 1;
return;
end if;
haystack := @next haystack;
end loop;
end sub;
 
sub digitSumIsSubstring(n: uint16): (r: uint8) is
var s1: uint8[6];
var s2: uint8[6];
var dummy := UIToA(n as uint32, 10, &s1[0]);
dummy := UIToA(digitSum(n) as uint32, 10, &s2[0]);
r := contains(&s1[0], &s2[0]);
end sub;
 
var i: uint16 := 0;
while i < 1000 loop
if digitSumIsSubstring(i) != 0 then
print_i16(i);
print_char(' ');
end if;
i := i + 1;
end loop;
print_nl();</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</pre>
 
=={{header|D}}==
{{trans|C++}}
<syntaxhighlight lang="d">import std.algorithm;
import std.conv;
import std.stdio;
 
int digitSum(int n) {
int s = 0;
do {
s += n % 10;
} while (n /= 10);
return s;
}
 
void main() {
foreach (i; 0 .. 1000) {
if (i.to!string.canFind(digitSum(i).to!string)) {
write(i, ' ');
}
}
writeln;
}</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</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}}==
<syntaxhighlight lang="draco">\util.g
 
proc nonrec digit_sum(word n) word:
word sum;
sum := 0;
while n ~= 0 do
sum := sum + n % 10;
n := n / 10;
od;
sum
corp
 
proc nonrec itoa(word n; *char buf) void:
channel output text ch;
open(ch, buf);
write(ch; n);
close(ch)
corp
 
proc nonrec digit_sum_is_substring(word n) bool:
[10] char dstr, dsub;
itoa(n, &dstr[0]);
itoa(digit_sum(n), &dsub[0]);
CharsIndex(&dstr[0], &dsub[0]) ~= -1
corp
 
proc nonrec main() void:
word i, seen;
seen := 0;
for i from 0 upto 999 do
if digit_sum_is_substring(i) then
write(i:4);
seen := seen + 1;
if seen % 20 = 0 then writeln() fi
fi
od
corp</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</pre>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Sum digits of n is substring of n: Nigel Galloway. April 16th., 2021
let rec fG n g=match (n/10,n%(if g<10 then 10 else 100)) with (_,n) when n=g->true |(0,_)->false |(n,_)->fG n g
let rec fN g=function n when n<10->n+g |n->fN(g+n%10)(n/10)
{1..999}|>Seq.filter(fun n->fG n (fN 0 n))|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
{{out}}
<pre>
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
Real: 00:00:00.003
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: grouping kernel math.text.utils present prettyprint
sequences ;
 
1000 <iota>
[ [ 1 digit-groups sum present ] [ present ] bi subseq? ] filter
8 group simple-table.</langsyntaxhighlight>
{{out}}
<pre>
Line 22 ⟶ 951:
912 913 914 915 916 917 918 919
</pre>
 
=={{header|Fermat}}==
No string conversion.
<syntaxhighlight lang="fermat">Func Digsum(n, b) =
ds:=0; {digital sum of n in base b}
while n>0 do
ds:+(n|b);
n:=n\b;
od;
ds.;
 
Func Numdig(n, b) =
nd:=0; {number of digits of n in base b}
while n > 0 do
nd:+;
n:=n\b;
od;
nd.;
 
for n = 1 to 999 do
ds:=Digsum(n, 10); {digital sum of n}
nd:=Numdig(ds, 10); {how many digits does the digital sum itself have?}
nt:=n; {temporary copy of n}
while nt>0 do
if ds=(nt|(10^(nd))) then
!!n; {if the last nt digits of n are the digital sum, print and exit the loop}
&>;
fi;
nt:=nt\10;
od;
od;</syntaxhighlight>
{{out}}<pre>
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|FOCAL}}==
<syntaxhighlight lang="focal">01.10 F N=0,999;D 2;D 4
01.20 Q
 
02.10 S A=0
02.20 S B=N
02.30 S C=FITR(B/10)
02.40 S A=A+(B-C*10)
02.50 S B=C
02.60 I (-B)2.3
 
03.10 S B=1
03.20 S B=B*10
03.30 I (B-M)3.2,3.2
03.40 S B=B/10
03.50 S M=M-FITR(M/B)*B
 
04.10 S P=N
04.20 S M=P
04.30 I (M-A)4.4,4.9,4.4
04.40 D 3
04.50 I (M)4.3,4.6,4.3
04.60 S P=FITR(P/10)
04.70 I (P)4.2,4.8,4.2
04.80 R
04.90 T %3,N,!</syntaxhighlight>
{{out}}
<pre style='height:50ex;'>= 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|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">function is_substring( s as string, j as string ) as boolean
dim as integer nj = len(j), ns = len(s)
for i as integer = 1 to ns - nj + 1
Line 43 ⟶ 1,128:
for i as uinteger = 0 to 999
if is_substring( str(i), str(sumdig(i))) then print i;" ";
next i : print : end</langsyntaxhighlight>
{{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>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Sum_of_the_digits_of_n_is_substring_of_n}}
 
'''Solution'''
 
[[File:Fōrmulæ - Sum of the digits of n is substring of n 01.png]]
 
[[File:Fōrmulæ - Sum of the digits of n is substring of n 02.png]]
 
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 71 ⟶ 1,165:
fmt.Println()
fmt.Println(len(numbers), "such numbers found.")
}</langsyntaxhighlight>
 
{{out}}
Line 84 ⟶ 1,178:
 
48 such numbers found.
</pre>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Data.Char (digitToInt)
import Data.List (isInfixOf)
import Data.List.Split (chunksOf)
 
-------- SUM OF THE DIGITS OF N IS A SUBSTRING OF N ------
 
digitSumIsSubString :: String -> Bool
digitSumIsSubString =
isInfixOf
=<< show . foldr ((+) . digitToInt) 0
 
 
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_ putStrLn $
showMatches digitSumIsSubString <$> [999, 10000]
 
showMatches :: (String -> Bool) -> Int -> String
showMatches p limit =
( show (length xs)
<> " matches in [0.."
<> show limit
<> "]\n"
)
<> unlines
( unwords
<$> chunksOf 10 (justifyRight w ' ' <$> xs)
)
<> "\n"
where
xs = filter p $ fmap show [0 .. limit]
w = length (last xs)
 
justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)</syntaxhighlight>
{{Out}}
<pre>48 matches in [0..999]
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
 
 
365 matches in [0..10000]
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 1000 1009
1018 1027 1036 1045 1054 1063 1072 1081 1090 1108
1109 1118 1127 1128 1136 1138 1145 1148 1154 1158
1163 1168 1172 1178 1181 1188 1190 1198 1209 1218
1227 1236 1245 1254 1263 1272 1281 1290 1309 1318
1327 1336 1345 1354 1363 1372 1381 1390 1409 1418
1427 1436 1445 1454 1463 1472 1481 1490 1509 1518
1527 1536 1545 1554 1563 1572 1581 1590 1609 1618
1627 1636 1645 1654 1663 1672 1681 1690 1709 1718
1727 1736 1745 1754 1763 1772 1781 1790 1809 1810
1811 1812 1813 1814 1815 1816 1817 1818 1819 1827
1836 1845 1854 1863 1872 1881 1890 1909 1918 1927
1936 1945 1954 1963 1972 1981 1990 2000 2099 2107
2117 2127 2137 2147 2157 2167 2177 2187 2197 2199
2299 2399 2499 2599 2699 2710 2711 2712 2713 2714
2715 2716 2717 2718 2719 2799 2899 2999 3000 3106
3116 3126 3136 3146 3156 3166 3176 3186 3196 3610
3611 3612 3613 3614 3615 3616 3617 3618 3619 4000
4105 4115 4125 4135 4145 4155 4165 4175 4185 4195
4510 4511 4512 4513 4514 4515 4516 4517 4518 4519
5000 5104 5114 5124 5134 5144 5154 5164 5174 5184
5194 5410 5411 5412 5413 5414 5415 5416 5417 5418
5419 6000 6103 6113 6123 6133 6143 6153 6163 6173
6183 6193 6310 6311 6312 6313 6314 6315 6316 6317
6318 6319 7000 7102 7112 7122 7132 7142 7152 7162
7172 7182 7192 7210 7211 7212 7213 7214 7215 7216
7217 7218 7219 8000 8101 8110 8111 8112 8113 8114
8115 8116 8117 8118 8119 8121 8131 8141 8151 8161
8171 8181 8191 9000 9010 9011 9012 9013 9014 9015
9016 9017 9018 9019 9100 9110 9120 9130 9140 9150
9160 9170 9180 9190 9209 9219 9229 9239 9249 9259
9269 9279 9289 9299 9920 9921 9922 9923 9924 9925
9926 9927 9928 9929 10000</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j">([#~(":+./@E.~[:":+/@(10&#.^:_1))"0)i.999</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</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
def sum_of_digits_is_substring:
tostring
| . as $s
| (explode | map([.]|implode))
| (map(tonumber)|add|tostring) as $ss
| $s | index($ss);
 
[range(0;1000) | select(sum_of_digits_is_substring)]</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]
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">issumsub(n, base=10) = occursin(string(sum(digits(n, base=base)), base=base), string(n, base=base))
 
foreach(p -> print(rpad(p[2], 4), p[1] % 10 == 0 ? "\n" : ""), enumerate(filter(issumsub, 0:999)))
</langsyntaxhighlight>{{out}}
<pre>
0 1 2 3 4 5 6 7 8 9
Line 98 ⟶ 1,299:
912 913 914 915 916 917 918 919
</pre>
 
=={{header|Kotlin}}==
{{trans|Go}}
<syntaxhighlight lang="scala">fun digitSum(n: Int): Int {
var nn = n
var sum = 0
while (nn > 0) {
sum += (nn % 10)
nn /= 10
}
return sum
}
 
fun main() {
var c = 0
for (i in 0 until 1000) {
val ds = digitSum(i)
if (i.toString().contains(ds.toString())) {
print("%3d ".format(i))
 
c += 1
if (c == 8) {
println()
c = 0
}
}
}
println()
}</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 </pre>
 
=={{header|MAD}}==
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(A,B)
ENTRY TO REM.
FUNCTION RETURN A-A/B*B
END OF FUNCTION
INTERNAL FUNCTION(X)
ENTRY TO DSUM.
TEMP = X
SUM = 0
SUML WHENEVER TEMP.NE.0
SUM = SUM + REM.(TEMP,10)
TEMP = TEMP / 10
TRANSFER TO SUML
END OF CONDITIONAL
FUNCTION RETURN SUM
END OF FUNCTION
INTERNAL FUNCTION(X)
ENTRY TO DELFST.
FDGT = 1
SIZE WHENEVER FDGT.LE.X
FDGT = FDGT * 10
TRANSFER TO SIZE
END OF CONDITIONAL
FUNCTION RETURN REM.(X,FDGT/10)
END OF FUNCTION
INTERNAL FUNCTION(N,H)
ENTRY TO INFIX.
WHENEVER N.E.H, FUNCTION RETURN 1B
PFX = H
PFXL WHENEVER PFX.NE.0
SFX = PFX
SFXL WHENEVER SFX.NE.0
WHENEVER SFX.E.N, FUNCTION RETURN 1B
SFX = DELFST.(SFX)
TRANSFER TO SFXL
END OF CONDITIONAL
PFX = PFX/10
TRANSFER TO PFXL
END OF CONDITIONAL
FUNCTION RETURN 0B
END OF FUNCTION
THROUGH SHOW, FOR I=0, 1, I.GE.1000
WHENEVER INFIX.(DSUM.(I),I)
PRINT FORMAT FMT, I
END OF CONDITIONAL
SHOW CONTINUE
 
VECTOR VALUES FMT = $I3*$
END OF PROGRAM </syntaxhighlight>
{{out}}
<pre style='height:50ex;'> 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|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[SumAsSubString]
SumAsSubString[n_Integer] := Module[{id, s},
id = IntegerDigits[n];
s = Total[id];
SequenceCount[id, IntegerDigits[s]] > 0
]
Select[Range[999], SumAsSubString]</syntaxhighlight>
{{out}}
<pre>{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|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (table 5 10 taskresults)]
where taskresults = filter digit_sum_is_substring [0..999]
 
table :: num->num->[num]->[char]
table cw w ns = lay (map concat (split (map fmt ns)))
where split [] = []
split ls = take w ls : split (drop w ls)
fmt n = reverse (take cw ((reverse (shownum n)) ++ repeat ' '))
 
digit_sum_is_substring :: num->bool
digit_sum_is_substring n = (digitsum n) $infix n
 
digitsum :: num->num
digitsum 0 = 0
digitsum n = n mod 10 + digitsum (n div 10)
 
infix :: num->num->bool
infix n h = True, if n = h
= False, if h = 0
= True, if n $infix (h div 10)
= True, if n $infix (chop h)
= False, otherwise
 
chop :: num->num
chop n = 0, if n<10
= n mod mask, otherwise
where mask = last (takewhile (<n) (iterate (*10) 1))</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</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import strutils
 
func digitsum(n: Natural): int =
if n == 0: return 0
var n = n
while n != 0:
result += n mod 10
n = n div 10
 
var count = 0
for n in 0..<1000:
let sn = $n
if $digitsum(n) in sn:
inc count
stdout.write sn.align(3), if count mod 8 == 0: '\n' else: ' '</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</pre>
 
=={{header|Perl}}==
as one-liner ..
<syntaxhighlight lang="perl">// 20210415 Perl programming solution
 
perl -e 'for(0..999){my$n;s/(\d)/$n+=$1/egr;print"$_ "if/$n/}'</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
</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">sdn</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">sn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sn</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">))),</span><span style="color: #000000;">sn</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">999</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10000</span> <span style="color: #008080;">by</span> <span style="color: #000000;">10000</span><span style="color: #0000FF;">-</span><span style="color: #000000;">999</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #000000;">sdn</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Found %d such numbers &lt; %d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">),</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Found 48 such numbers < 1000: 0, 1, 2, 3, 4, ..., 915, 916, 917, 918, 919
Found 365 such numbers < 10001: 0, 1, 2, 3, 4, ..., 9926, 9927, 9928, 9929, 10000
</pre>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pli">sumOfDigitsIsSubstring: procedure options(main);
digitSum: procedure(n) returns(fixed);
declare (ds, x, n) fixed;
ds = 0;
do x=n repeat(x/10) while(x>0);
ds = ds + mod(x, 10);
end;
return(ds);
end digitSum;
chop: procedure(n) returns(fixed);
declare (i, n) fixed;
i = 1;
do while(i<n);
i = i * 10;
end;
i = i/10;
if i=0 then return(0);
else return(mod(n, i));
end chop;
infix: procedure(n, h) returns(bit) recursive;
declare (n, h) fixed;
if n=h then return('1'b);
if h=0 then return('0'b);
if infix(n, h/10) then return('1'b);
return(infix(n, chop(h)));
end infix;
declare (i, col) fixed;
col = 0;
do i=0 to 999;
if infix(digitSum(i), i) then do;
put edit(i) (F(5));
col = col + 1;
if mod(col, 10)=0 then put skip;
end;
end;
put skip;
end sumOfDigitsIsSubstring;</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</pre>
 
=={{header|PL/M}}==
<syntaxhighlight lang="plm">100H:
DIGIT$SUM: PROCEDURE (N) BYTE;
DECLARE N ADDRESS, SUM BYTE;
SUM = 0;
DO WHILE N > 0;
SUM = SUM + N MOD 10;
N = N / 10;
END;
RETURN SUM;
END DIGIT$SUM;
 
ITOA: PROCEDURE (N) ADDRESS;
DECLARE S (6) BYTE INITIAL ('.....$');
DECLARE (N, P) ADDRESS, C BASED P BYTE;
P = .S(5);
DIGIT:
P = P - 1;
C = N MOD 10 + '0';
IF (N := N / 10) > 0 THEN GO TO DIGIT;
RETURN P;
END ITOA;
 
COPY$STRING: PROCEDURE (IN, OUT);
DECLARE (IN, OUT) ADDRESS;
DECLARE (I BASED IN, O BASED OUT) BYTE;
DO WHILE I <> '$';
O = I;
IN = IN + 1;
OUT = OUT + 1;
END;
O = '$';
END COPY$STRING;
 
CONTAINS: PROCEDURE (HAYSTACK, NEEDLE) BYTE;
DECLARE (NEEDLE, HAYSTACK, NPOS, HPOS) ADDRESS;
DECLARE (N BASED NPOS, H BASED HPOS, HS BASED HAYSTACK) BYTE;
DO WHILE HS <> '$';
NPOS = NEEDLE;
HPOS = HAYSTACK;
DO WHILE N = H AND H <> '$' AND N <> '$';
NPOS = NPOS + 1;
HPOS = HPOS + 1;
END;
IF N = '$' THEN RETURN 1;
HAYSTACK = HAYSTACK + 1;
END;
RETURN 0;
END CONTAINS;
 
BDOS: PROCEDURE (FN, ARG);
DECLARE FN BYTE, ARG ADDRESS;
GO TO 5;
END BDOS;
 
PRINT: PROCEDURE (STRING);
DECLARE STRING ADDRESS;
CALL BDOS(9, STRING);
END PRINT;
 
DECLARE N ADDRESS;
DECLARE S1 (6) BYTE, S2 (6) BYTE;
DO N = 0 TO 999;
CALL COPY$STRING(ITOA(N), .S1);
CALL COPY$STRING(ITOA(DIGIT$SUM(N)), .S2);
IF CONTAINS(.S1, .S2) THEN DO;
CALL PRINT(.S1);
CALL PRINT(.' $');
END;
END;
 
CALL BDOS(0,0);
EOF</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</pre>
 
=={{header|Python}}==
Just using the command line:
 
<langsyntaxhighlight lang="python">Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> x = [n for n in range(1000) if str(sum(int(d) for d in str(n))) in str(n)]
Line 114 ⟶ 1,680:
200, 300, 400, 500, 600, 700, 800, 900, 910, 911
912, 913, 914, 915, 916, 917, 918, 919
>>> </langsyntaxhighlight>
 
 
or as a full script, taking an alternative route, and slightly reducing the number of str conversions required:
 
<langsyntaxhighlight lang="python">'''Sum of the digits of n is substring of n'''
 
from functools import reduce
from itertools import chain
 
 
Line 137 ⟶ 1,704:
# main :: IO ()
def main():
'''Matches in [10..999]'''
print(
 
xs = list showMatches(
filter( digitSumIsSubString
digitSumIsSubString,)(999)
(str(n) for n in range(1, 1000))
)
)
w = len(xs[-1])
 
 
print(f'{len(xs)} matches < 1000:\n')
# ----------------------- DISPLAY ------------------------
print(
 
'\n'.join(
# showMatches :: (String -> Bool) -> Int -> String
' '.join(cell.rjust(w, ' ') for cell in row)
def showMatches(p):
for row in chunksOf(10)(xs)
'''A listing of the integer strings [0..limit]
which match the predicate p.
'''
def go(limit):
def triage(n):
s = str(n)
return [s] if p(s) else []
 
xs = list(
chain.from_iterable(
map(triage, range(0, 1 + limit))
)
)
w = len(xs[-1])
return f'{len(xs)} matches < {limit}:\n' + (
'\n'.join(
' '.join(cell.rjust(w, ' ') for cell in row)
for row in chunksOf(10)(xs)
)
)
 
return go
 
 
Line 174 ⟶ 1,758:
if __name__ == '__main__':
main()
</syntaxhighlight>
</lang>
{{Out}}
<pre>4748 matches < 1000:
 
0 1 2 3 4 5 6 7 8 9 10
10 20 30 40 50 60 70 80 90 100 109
109 119 129 139 149 159 169 179 189 199 200
200 300 400 500 600 700 800 900 910 911 912
912 913 914 915 916 917 918 919</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ over findseq swap found ] is hasseq ( [ [ --> b )
 
[ [] swap
[ 10 /mod
rot join swap
dup 0 = until ]
drop ] is digits ( n --> [ )
 
[ digits
0 over witheach +
digits hasseq ] is subsum ( n --> b )
 
[] 1000 times
[ i^ subsum if
[ i^ join ] ]
dup echo
cr cr
say "There are " size echo say " numbers."</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 ]
 
There are 48 numbers.</pre>
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>say "{+$_} matching numbers\n{.batch(10)».fmt('%3d').join: "\n"}" given (^1000).grep: { .contains: .comb.sum }</langsyntaxhighlight>
{{out}}
<pre>48 matching numbers
Line 193 ⟶ 1,804:
200 300 400 500 600 700 800 900 910 911
912 913 914 915 916 917 918 919</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Table (8 5) <Filter DigitSumSubstring <Iota 0 999>>>;
};
 
Cell {
s.W s.N, <Repeat s.W ' '> <Symb s.N>: e.C,
<Last s.W e.C>: (e.X) e.CI = e.CI;
}
 
Table {
(s.Cols s.CW) e.X = <Table () (s.Cols s.Cols s.CW) e.X>;
(e.Row) (s.Cols s.N s.CW), e.Row: {
= ;
e.Row = <Prout e.Row>;
};
(e.Row) (s.Cols 0 s.CW) e.X =
<Prout e.Row>
<Table () (s.Cols s.Cols s.CW) e.X>;
(e.Row) (s.Cols s.N s.CW) s.I e.X =
<Table (e.Row <Cell s.CW s.I>) (s.Cols <- s.N 1> s.CW) e.X>;
};
 
Repeat {
0 s.C = ;
s.N s.C = s.C <Repeat <- s.N 1> s.C>;
};
 
Filter {
s.F = ;
s.F s.I e.X, <Mu s.F s.I>: {
True = s.I <Filter s.F e.X>;
False = <Filter s.F e.X>;
};
};
 
Iota {
s.End s.End = s.End;
s.Start s.End = s.Start <Iota <+ 1 s.Start> s.End>;
};
 
DigitSumSubstring {
s.N, <Symb <DigitSum s.N>>: e.2,
<Symb s.N>: e.1 e.2 e.3 = True;
s.N = False;
};
 
DigitSum {
0 = 0;
s.N, <Divmod s.N 10>: (s.R) s.D = <+ s.D <DigitSum s.R>>;
};</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</pre>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX pgm finds integers whose sum of decimal digits is a substring of N, N < 1000. */
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/
Line 223 ⟶ 1,893:
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
sumDigs:procedure; parse arg x 1 s 2;do j=2 for length(x)-1;s=s+substr(x,j,1);end;return s</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 239 ⟶ 1,909:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 266 ⟶ 1,936:
see nl + "Found " + row + " numbers" + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 279 ⟶ 1,949:
done...
</pre>
 
=={{header|RPL}}==
≪ →STR 0 1 3 PICK SIZE '''FOR''' j
OVER j DUP SUB STR→ + '''NEXT'''
→STR POS
≫ ‘'''∑DIN?'''’ STO
 
≪ { } 1 1000 '''FOR''' j '''IF''' j '''∑DIN? THEN''' j + '''END NEXT''' ≫ EVAL
{{out}}
<pre>
1: { 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 1000 }
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">p (0...1000).select{|n| n.to_s.match? n.digits.sum.to_s}
</syntaxhighlight>
{{out}}
<pre>[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, 1000]
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="Rust">fn sum_digits( mut num : u32 ) -> u32 {
let mut sum : u32 = 0 ;
while num != 0 {
sum += num % 10 ;
num /= 10 ;
}
sum
}
 
fn main() {
let solution : Vec<u32> = (0..1000).filter( | &d | {
let digit_sum : u32 = sum_digits( d ) ;
let sumstring = digit_sum.to_string( ) ;
let sumstr : &str = sumstring.as_str( ) ;
let numstring : String = d.to_string( ) ;
let numstr : &str = numstring.as_str( ) ;
numstr.contains( &sumstr )
}).collect( ) ;
println!("{:?}" , solution ) ;
}</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]
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var upto = 1000
var base = 10
 
var list = (^upto -> grep {
.digits(base).contains(.sumdigits(base).digits(base)...)
})
 
say "Numbers under #{upto} whose sum of digits is a substring of themselves:"
 
list.each_slice(8, {|*a|
say a.map { '%3s' % _ }.join(' ')
})
 
say "\n#{list.len} such numbers found."</syntaxhighlight>
{{out}}
<pre>
Numbers under 1000 whose sum of digits is a substring of themselves:
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
 
48 such numbers found.
</pre>
 
=={{header|SNOBOL4}}==
<syntaxhighlight lang="snobol4"> define('digsum(n)') :(digsum_end)
digsum digsum = 0
dsloop digsum = digsum + remdr(n,10)
n = ne(n,0) n / 10 :s(dsloop)f(return)
digsum_end
 
define('sumsub(n)') :(sumsub_end)
sumsub n digsum(n) :s(return)f(freturn)
sumsub_end
 
i = 0
loop output = sumsub(i) i
i = lt(i,999) i + 1 :s(loop)
end</syntaxhighlight>
{{out}}
<pre style='height:50ex'>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|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./seqfmt" for LstFmt
import "/fmt" for Fmt
 
var numbers = []
Line 295 ⟶ 2,101:
}
System.print("Numbers under 1,000 whose sum of digits is a substring of themselves:")
Fmt.tprint("$3d", numbers, 8)
for (chunk in Lst.chunks(numbers, 8)) Fmt.print("$3d", chunk)
System.print("\n%(numbers.count) such numbers found.")</langsyntaxhighlight>
 
{{out}}
Line 310 ⟶ 2,116:
48 such numbers found.
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func Check(N); \Return 'true' if sum of digits of N is a substring of N
int N, Sum, A, B, C;
[N:= N/10;
C:= rem(0);
N:= N/10;
B:= rem(0);
A:= N;
Sum:= A+B+C;
if Sum=A or Sum=B or Sum=C then return true;
if Sum = B*10 + C then return true;
if Sum = A*10 + B then return true;
return false;
];
 
int Count, N;
[Count:= 0;
for N:= 0 to 1000-1 do
if Check(N) then
[IntOut(0, N);
Count:= Count+1;
if rem(Count/10) = 0 then CrLf(0) else ChOut(0, 9\tab\);
];
CrLf(0);
IntOut(0, Count);
Text(0, " such numbers found below 1000.
");
]</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
48 such numbers found below 1000.
</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Sum_of_the_digits_of_n_is_substring_of_n
// by Galileo, 04/2022
 
for n = 0 to 999
if isSubstring(n) print n using "####";
next
print
sub isSubstring(n)
local n$, lon, i, p
n$ = str$(n)
lon = len(n$)
for i = 1 to lon
p = p + val(mid$(n$,i,1))
next
return instr(n$, str$(p))
end sub</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
---Program done, press RETURN---</pre>
2,114

edits