Sum of the digits of n is substring of n: Difference between revisions
Sum of the digits of n is substring of n (view source)
Revision as of 11:44, 3 May 2024
, 14 days agoAdd Refal
Alextretyak (talk | contribs) |
Not a robot (talk | contribs) (Add Refal) |
||
(18 intermediate revisions by 13 users not shown) | |||
Line 12:
{{trans|Nim}}
<
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 ‘ ’)</
{{out}}
Line 29:
=={{header|8080 Assembly}}==
<
org 100h
lxi h,-1 ; Number
Line 126:
jmp 5
buf0: equ $+32
buf1: equ $+64</
{{out}}
<pre style='height:50ex;'>0
Line 178:
=={{header|Action!}}==
<
INT res,a
Line 218:
OD
PrintF("%E%EThere are %I numbers",count)
RETURN</
{{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]
Line 230:
=={{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]].
<
INT max number = 1 000;
INT n count := 0;
Line 247:
FI
OD
END</
{{out}}
<pre>
Line 259:
=={{header|ALGOL-M}}==
<
integer function mod(a,b);
integer a,b;
Line 316:
end;
end;
end</
{{out}}
<pre> 0 1 2 3 4 5 6 7 8 9
Line 325:
=={{header|ALGOL W}}==
<
% 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
Line 366 ⟶ 365:
end if_n_contains_dSum
end for_n
end.</
{{out}}
<pre>
Line 379 ⟶ 378:
=={{header|APL}}==
{{works with|Dyalog APL}}
<
{{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}}==
<
loop 1000{
n := A_Index-1, sum := 0
Line 396 ⟶ 405:
}
}
MsgBox % result</
{{out}}
<pre>0 1 2 3 4 5 6 7
Line 406 ⟶ 415:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SUM_OF_THE_DIGITS_OF_N_IS_SUBSTRING_OF_N.AWK
BEGIN {
Line 425 ⟶ 434:
return(sum)
}
</syntaxhighlight>
{{out}}
<pre>
Line 437 ⟶ 446:
=={{header|BASIC}}==
<
20 FOR I=0 TO 999
30 J=0: K=I
Line 444 ⟶ 453:
42 J$=STR$(J): J$=RIGHT$(J$,LEN(J$)-1)
50 IF INSTR(I$,J$) THEN PRINT I,
60 NEXT I</
{{out}}
<pre> 0 1 2 3 4
Line 459 ⟶ 468:
=={{header|BCPL}}==
<
let dsum(n) = n=0 -> 0, n rem 10 + dsum(n/10)
Line 487 ⟶ 496:
$)
wrch('*N')
$)</
{{out}}
<pre> 0 1 2 3 4 5 6 7 8 9
Line 496 ⟶ 505:
=={{header|BQN}}==
<
Contains ← (∨´⍷˜ )○•Fmt
∘‿6⥊ (⊢ Contains DigitSum)¨⊸/↕1000</
{{out}}
<pre>┌─
Line 511 ⟶ 520:
┘</pre>
=={{header|C}}==
<
#include <string.h>
Line 535 ⟶ 544:
return 0;
}</
{{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++}}==
<
int digitSum(int n) {
Line 558 ⟶ 567:
std::cout << std::endl;
return 0;
}</
{{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}}==
<
sum: int := 0
while n > 0 do
Line 593 ⟶ 602:
if col // 10 = 0 then stream$putc(po, '\n') end
end
end start_up</
{{out}}
<pre> 0 1 2 3 4 5 6 7 8 9
Line 602 ⟶ 611:
=={{header|COBOL}}==
<
PROGRAM-ID. SUM-SUBSTRING.
Line 654 ⟶ 663:
ADD ND(X) TO DSUM.
ADD 1 TO X.
IF X IS LESS THAN 5 GO TO LOOP.</
{{out}}
<pre style='height:50ex;'> 0
Line 704 ⟶ 713:
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}}==
<
sub digitSum(n: uint16): (s: uint16) is
Line 749 ⟶ 782:
i := i + 1;
end loop;
print_nl();</
{{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>
Line 755 ⟶ 788:
=={{header|D}}==
{{trans|C++}}
<
import std.conv;
import std.stdio;
Line 774 ⟶ 807:
}
writeln;
}</
{{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#}}==
<
// 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>
Line 793 ⟶ 936:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
sequences ;
1000 <iota>
[ [ 1 digit-groups sum present ] [ present ] bi subseq? ] filter
8 group simple-table.</
{{out}}
<pre>
Line 807 ⟶ 950:
400 500 600 700 800 900 910 911
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}}==
<
01.20 Q
Line 834 ⟶ 1,057:
04.70 I (P)4.2,4.8,4.2
04.80 R
04.90 T %3,N,!</
{{out}}
<pre style='height:50ex;'>= 0
Line 884 ⟶ 1,107:
= 918
= 919</pre>
=={{header|FreeBASIC}}==
<
dim as integer nj = len(j), ns = len(s)
for i as integer = 1 to ns - nj + 1
Line 904 ⟶ 1,128:
for i as uinteger = 0 to 999
if is_substring( str(i), str(sumdig(i))) then print i;" ";
next i : print : end</
{{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}}
<
import (
Line 939 ⟶ 1,165:
fmt.Println()
fmt.Println(len(numbers), "such numbers found.")
}</
{{out}}
Line 955 ⟶ 1,181:
=={{header|Haskell}}==
<
import Data.List (isInfixOf)
import Data.List.Split (chunksOf)
Line 990 ⟶ 1,216:
justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)</
{{Out}}
<pre>48 matches in [0..999]
Line 1,040 ⟶ 1,266:
=={{header|J}}==
<
{{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>
Line 1,047 ⟶ 1,273:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
def sum_of_digits_is_substring:
tostring
Line 1,055 ⟶ 1,281:
| $s | index($ss);
[range(0;1000) | select(sum_of_digits_is_substring)]</
{{out}}
<pre>
Line 1,062 ⟶ 1,288:
=={{header|Julia}}==
<
foreach(p -> print(rpad(p[2], 4), p[1] % 10 == 0 ? "\n" : ""), enumerate(filter(issumsub, 0:999)))
</
<pre>
0 1 2 3 4 5 6 7 8 9
Line 1,076 ⟶ 1,302:
=={{header|Kotlin}}==
{{trans|Go}}
<
var nn = n
var sum = 0
Line 1,101 ⟶ 1,327:
}
println()
}</
{{out}}
<pre> 0 1 2 3 4 5 6 7
Line 1,111 ⟶ 1,337:
=={{header|MAD}}==
<
INTERNAL FUNCTION(A,B)
Line 1,164 ⟶ 1,390:
VECTOR VALUES FMT = $I3*$
END OF PROGRAM </
{{out}}
<pre style='height:50ex;'> 0
Line 1,216 ⟶ 1,442:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
SumAsSubString[n_Integer] := Module[{id, s},
id = IntegerDigits[n];
Line 1,222 ⟶ 1,448:
SequenceCount[id, IntegerDigits[s]] > 0
]
Select[Range[999], SumAsSubString]</
{{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}}==
<
func digitsum(n: Natural): int =
Line 1,241 ⟶ 1,503:
if $digitsum(n) in sn:
inc count
stdout.write sn.align(3), if count mod 8 == 0: '\n' else: ' '</
{{out}}
Line 1,253 ⟶ 1,515:
=={{header|Perl}}==
as one-liner ..
<
perl -e 'for(0..999){my$n;s/(\d)/$n+=$1/egr;print"$_ "if/$n/}'</
{{out}}
<pre>
Line 1,262 ⟶ 1,524:
=={{header|Phix}}==
<!--<
<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>
Line 1,271 ⟶ 1,533:
<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 < %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>
<!--</
{{out}}
<pre>
Line 1,279 ⟶ 1,541:
=={{header|PL/I}}==
<
digitSum: procedure(n) returns(fixed);
declare (ds, x, n) fixed;
Line 1,318 ⟶ 1,580:
end;
put skip;
end sumOfDigitsIsSubstring;</
{{out}}
<pre> 0 1 2 3 4 5 6 7 8 9
Line 1,327 ⟶ 1,589:
=={{header|PL/M}}==
<
DIGIT$SUM: PROCEDURE (N) BYTE;
DECLARE N ADDRESS, SUM BYTE;
Line 1,399 ⟶ 1,661:
CALL BDOS(0,0);
EOF</
{{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>
Line 1,406 ⟶ 1,668:
Just using the command line:
<
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 1,418 ⟶ 1,680:
200, 300, 400, 500, 600, 700, 800, 900, 910, 911
912, 913, 914, 915, 916, 917, 918, 919
>>> </
or as a full script, taking an alternative route, and slightly reducing the number of str conversions required:
<
from functools import reduce
Line 1,496 ⟶ 1,758:
if __name__ == '__main__':
main()
</syntaxhighlight>
{{Out}}
<pre>48 matches < 1000:
Line 1,505 ⟶ 1,767:
200 300 400 500 600 700 800 900 910 911
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"
{{out}}
<pre>48 matching numbers
Line 1,515 ⟶ 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}}==
<
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/
Line 1,545 ⟶ 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</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,561 ⟶ 1,909:
=={{header|Ring}}==
<
load "stdlib.ring"
see "working..." + nl
Line 1,588 ⟶ 1,936:
see nl + "Found " + row + " numbers" + nl
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,600 ⟶ 1,948:
Found 48 numbers
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}}==
<
var base = 10
Line 1,616 ⟶ 2,009:
})
say "\n#{list.len} such numbers found."</
{{out}}
<pre>
Line 1,631 ⟶ 2,024:
=={{header|SNOBOL4}}==
<
digsum digsum = 0
dsloop digsum = digsum + remdr(n,10)
Line 1,644 ⟶ 2,037:
loop output = sumsub(i) i
i = lt(i,999) i + 1 :s(loop)
end</
{{out}}
<pre style='height:50ex'>0
Line 1,697 ⟶ 2,090:
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<
import "./
var numbers = []
Line 1,710 ⟶ 2,101:
}
System.print("Numbers under 1,000 whose sum of digits is a substring of themselves:")
Fmt.tprint("$3d", numbers, 8)
System.print("\n%(numbers.count) such numbers found.")</
{{out}}
Line 1,727 ⟶ 2,118:
=={{header|XPL0}}==
<
int N, Sum, A, B, C;
[N:= N/10;
Line 1,753 ⟶ 2,144:
Text(0, " such numbers found below 1000.
");
]</
{{out}}
Line 1,764 ⟶ 2,155:
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>
|