Sum of the digits of n is substring of n: Difference between revisions
Content deleted Content added
Drkameleon (talk | contribs) |
Thundergnat (talk | contribs) m syntax highlighting fixup automation |
||
Line 12: | Line 12: | ||
{{trans|Nim}} |
{{trans|Nim}} |
||
< |
<syntaxhighlight lang="11l">V count = 0 |
||
L(n) 1000 |
L(n) 1000 |
||
I String(sum(String(n).map(Int))) C String(n) |
I String(sum(String(n).map(Int))) C String(n) |
||
count++ |
count++ |
||
print(f:‘{n:3}’, end' I count % 8 == 0 {"\n"} E ‘ ’)</ |
print(f:‘{n:3}’, end' I count % 8 == 0 {"\n"} E ‘ ’)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 29: | Line 29: | ||
=={{header|8080 Assembly}}== |
=={{header|8080 Assembly}}== |
||
< |
<syntaxhighlight lang="8080asm">puts: equ 9 |
||
org 100h |
org 100h |
||
lxi h,-1 ; Number |
lxi h,-1 ; Number |
||
Line 126: | Line 126: | ||
jmp 5 |
jmp 5 |
||
buf0: equ $+32 |
buf0: equ $+32 |
||
buf1: equ $+64</ |
buf1: equ $+64</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style='height:50ex;'>0 |
<pre style='height:50ex;'>0 |
||
Line 178: | Line 178: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">INT FUNC SumDigits(INT num) |
||
INT res,a |
INT res,a |
||
Line 218: | Line 218: | ||
OD |
OD |
||
PrintF("%E%EThere are %I numbers",count) |
PrintF("%E%EThere are %I numbers",count) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{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] |
[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: | Line 230: | ||
=={{header|ALGOL 68}}== |
=={{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]]. |
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 max number = 1 000; |
||
INT n count := 0; |
INT n count := 0; |
||
Line 247: | Line 247: | ||
FI |
FI |
||
OD |
OD |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 259: | Line 259: | ||
=={{header|ALGOL-M}}== |
=={{header|ALGOL-M}}== |
||
< |
<syntaxhighlight lang="algolm">begin |
||
integer function mod(a,b); |
integer function mod(a,b); |
||
integer a,b; |
integer a,b; |
||
Line 316: | Line 316: | ||
end; |
end; |
||
end; |
end; |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0 1 2 3 4 5 6 7 8 9 |
<pre> 0 1 2 3 4 5 6 7 8 9 |
||
Line 325: | Line 325: | ||
=={{header|ALGOL W}}== |
=={{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 % |
% returns true if the digits of s contains the digits of t, false otherwise % |
||
logical procedure containsDigits( integer value s, t ) ; |
logical procedure containsDigits( integer value s, t ) ; |
||
Line 365: | Line 365: | ||
end if_n_contains_dSum |
end if_n_contains_dSum |
||
end for_n |
end for_n |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 378: | Line 378: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
< |
<syntaxhighlight lang="apl">(⊢(/⍨)(∨/⍕∘(+/(⍎¨⍕))⍷⍕)¨)0,⍳999</syntaxhighlight> |
||
{{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 |
<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 |
||
Line 385: | Line 385: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">print select 1..999 'num -> |
||
contains? to :string num |
contains? to :string num |
||
to :string sum digits num</ |
to :string sum digits num</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 394: | Line 394: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">result := "", cntr := 1 |
||
loop 1000{ |
loop 1000{ |
||
n := A_Index-1, sum := 0 |
n := A_Index-1, sum := 0 |
||
Line 405: | Line 405: | ||
} |
} |
||
} |
} |
||
MsgBox % result</ |
MsgBox % result</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 1 2 3 4 5 6 7 |
<pre>0 1 2 3 4 5 6 7 |
||
Line 415: | Line 415: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f SUM_OF_THE_DIGITS_OF_N_IS_SUBSTRING_OF_N.AWK |
# syntax: GAWK -f SUM_OF_THE_DIGITS_OF_N_IS_SUBSTRING_OF_N.AWK |
||
BEGIN { |
BEGIN { |
||
Line 434: | Line 434: | ||
return(sum) |
return(sum) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 446: | Line 446: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
< |
<syntaxhighlight lang="basic">10 DEFINT I,J,K |
||
20 FOR I=0 TO 999 |
20 FOR I=0 TO 999 |
||
30 J=0: K=I |
30 J=0: K=I |
||
Line 453: | Line 453: | ||
42 J$=STR$(J): J$=RIGHT$(J$,LEN(J$)-1) |
42 J$=STR$(J): J$=RIGHT$(J$,LEN(J$)-1) |
||
50 IF INSTR(I$,J$) THEN PRINT I, |
50 IF INSTR(I$,J$) THEN PRINT I, |
||
60 NEXT I</ |
60 NEXT I</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0 1 2 3 4 |
<pre> 0 1 2 3 4 |
||
Line 468: | Line 468: | ||
=={{header|BCPL}}== |
=={{header|BCPL}}== |
||
< |
<syntaxhighlight lang="bcpl">get "libhdr" |
||
let dsum(n) = n=0 -> 0, n rem 10 + dsum(n/10) |
let dsum(n) = n=0 -> 0, n rem 10 + dsum(n/10) |
||
Line 496: | Line 496: | ||
$) |
$) |
||
wrch('*N') |
wrch('*N') |
||
$)</ |
$)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0 1 2 3 4 5 6 7 8 9 |
<pre> 0 1 2 3 4 5 6 7 8 9 |
||
Line 505: | Line 505: | ||
=={{header|BQN}}== |
=={{header|BQN}}== |
||
< |
<syntaxhighlight lang="bqn">DigitSum ← +´•Fmt-'0'˙ |
||
Contains ← (∨´⍷˜ )○•Fmt |
Contains ← (∨´⍷˜ )○•Fmt |
||
∘‿6⥊ (⊢ Contains DigitSum)¨⊸/↕1000</ |
∘‿6⥊ (⊢ Contains DigitSum)¨⊸/↕1000</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>┌─ |
<pre>┌─ |
||
Line 520: | Line 520: | ||
┘</pre> |
┘</pre> |
||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 544: | Line 544: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{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|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
int digitSum(int n) { |
int digitSum(int n) { |
||
Line 567: | Line 567: | ||
std::cout << std::endl; |
std::cout << std::endl; |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{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|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">digit_sum = proc (n: int) returns (int) |
||
sum: int := 0 |
sum: int := 0 |
||
while n > 0 do |
while n > 0 do |
||
Line 602: | Line 602: | ||
if col // 10 = 0 then stream$putc(po, '\n') end |
if col // 10 = 0 then stream$putc(po, '\n') end |
||
end |
end |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0 1 2 3 4 5 6 7 8 9 |
<pre> 0 1 2 3 4 5 6 7 8 9 |
||
Line 611: | Line 611: | ||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
< |
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
||
PROGRAM-ID. SUM-SUBSTRING. |
PROGRAM-ID. SUM-SUBSTRING. |
||
Line 663: | Line 663: | ||
ADD ND(X) TO DSUM. |
ADD ND(X) TO DSUM. |
||
ADD 1 TO X. |
ADD 1 TO X. |
||
IF X IS LESS THAN 5 GO TO LOOP.</ |
IF X IS LESS THAN 5 GO TO LOOP.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style='height:50ex;'> 0 |
<pre style='height:50ex;'> 0 |
||
Line 715: | Line 715: | ||
=={{header|Comal}}== |
=={{header|Comal}}== |
||
< |
<syntaxhighlight lang="comal">0010 FUNC digit'sum#(n#) CLOSED |
||
0020 sum#:=0 |
0020 sum#:=0 |
||
0030 WHILE n# DO sum#:+n# MOD 10;n#:=n# DIV 10 |
0030 WHILE n# DO sum#:+n# MOD 10;n#:=n# DIV 10 |
||
Line 731: | Line 731: | ||
0150 ENDFOR i# |
0150 ENDFOR i# |
||
0160 PRINT |
0160 PRINT |
||
0170 END</ |
0170 END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 1 2 3 4 5 6 7 8 9 10 20 30 40 50 |
<pre>0 1 2 3 4 5 6 7 8 9 10 20 30 40 50 |
||
Line 739: | Line 739: | ||
=={{header|Cowgol}}== |
=={{header|Cowgol}}== |
||
< |
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
||
sub digitSum(n: uint16): (s: uint16) is |
sub digitSum(n: uint16): (s: uint16) is |
||
Line 782: | Line 782: | ||
i := i + 1; |
i := i + 1; |
||
end loop; |
end loop; |
||
print_nl();</ |
print_nl();</syntaxhighlight> |
||
{{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> |
||
Line 788: | Line 788: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="d">import std.algorithm; |
||
import std.conv; |
import std.conv; |
||
import std.stdio; |
import std.stdio; |
||
Line 807: | Line 807: | ||
} |
} |
||
writeln; |
writeln; |
||
}</ |
}</syntaxhighlight> |
||
{{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|Draco}}== |
=={{header|Draco}}== |
||
< |
<syntaxhighlight lang="draco">\util.g |
||
proc nonrec digit_sum(word n) word: |
proc nonrec digit_sum(word n) word: |
||
Line 848: | Line 848: | ||
fi |
fi |
||
od |
od |
||
corp</ |
corp</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0 1 2 3 4 5 6 7 8 9 10 20 30 40 50 60 70 80 90 100 |
<pre> 0 1 2 3 4 5 6 7 8 9 10 20 30 40 50 60 70 80 90 100 |
||
Line 855: | Line 855: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Sum digits of n is substring of n: Nigel Galloway. April 16th., 2021 |
// 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 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) |
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 "" |
{1..999}|>Seq.filter(fun n->fG n (fN 0 n))|>Seq.iter(printf "%d "); printfn "" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 869: | Line 869: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2021-02-05}} |
{{works with|Factor|0.99 2021-02-05}} |
||
< |
<syntaxhighlight lang="factor">USING: grouping kernel math.text.utils present prettyprint |
||
sequences ; |
sequences ; |
||
1000 <iota> |
1000 <iota> |
||
[ [ 1 digit-groups sum present ] [ present ] bi subseq? ] filter |
[ [ 1 digit-groups sum present ] [ present ] bi subseq? ] filter |
||
8 group simple-table.</ |
8 group simple-table.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 887: | Line 887: | ||
=={{header|Fermat}}== |
=={{header|Fermat}}== |
||
No string conversion. |
No string conversion. |
||
< |
<syntaxhighlight lang="fermat">Func Digsum(n, b) = |
||
ds:=0; {digital sum of n in base b} |
ds:=0; {digital sum of n in base b} |
||
while n>0 do |
while n>0 do |
||
Line 914: | Line 914: | ||
nt:=nt\10; |
nt:=nt\10; |
||
od; |
od; |
||
od;</ |
od;</syntaxhighlight> |
||
{{out}}<pre> |
{{out}}<pre> |
||
1 |
1 |
||
Line 966: | Line 966: | ||
=={{header|FOCAL}}== |
=={{header|FOCAL}}== |
||
< |
<syntaxhighlight lang="focal">01.10 F N=0,999;D 2;D 4 |
||
01.20 Q |
01.20 Q |
||
Line 990: | Line 990: | ||
04.70 I (P)4.2,4.8,4.2 |
04.70 I (P)4.2,4.8,4.2 |
||
04.80 R |
04.80 R |
||
04.90 T %3,N,!</ |
04.90 T %3,N,!</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style='height:50ex;'>= 0 |
<pre style='height:50ex;'>= 0 |
||
Line 1,042: | Line 1,042: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">function is_substring( s as string, j as string ) as boolean |
||
dim as integer nj = len(j), ns = len(s) |
dim as integer nj = len(j), ns = len(s) |
||
for i as integer = 1 to ns - nj + 1 |
for i as integer = 1 to ns - nj + 1 |
||
Line 1,061: | Line 1,061: | ||
for i as uinteger = 0 to 999 |
for i as uinteger = 0 to 999 |
||
if is_substring( str(i), str(sumdig(i))) then print i;" "; |
if is_substring( str(i), str(sumdig(i))) then print i;" "; |
||
next i : print : end</ |
next i : print : 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> |
{{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,075: | Line 1,075: | ||
{{trans|Wren}} |
{{trans|Wren}} |
||
{{libheader|Go-rcu}} |
{{libheader|Go-rcu}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,096: | Line 1,096: | ||
fmt.Println() |
fmt.Println() |
||
fmt.Println(len(numbers), "such numbers found.") |
fmt.Println(len(numbers), "such numbers found.") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,112: | Line 1,112: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.Char (digitToInt) |
||
import Data.List (isInfixOf) |
import Data.List (isInfixOf) |
||
import Data.List.Split (chunksOf) |
import Data.List.Split (chunksOf) |
||
Line 1,147: | Line 1,147: | ||
justifyRight :: Int -> Char -> String -> String |
justifyRight :: Int -> Char -> String -> String |
||
justifyRight n c = (drop . length) <*> (replicate n c <>)</ |
justifyRight n c = (drop . length) <*> (replicate n c <>)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>48 matches in [0..999] |
<pre>48 matches in [0..999] |
||
Line 1,197: | Line 1,197: | ||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j">([#~(":+./@E.~[:":+/@(10&#.^:_1))"0)i.999</syntaxhighlight> |
||
{{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> |
||
Line 1,204: | Line 1,204: | ||
{{works with|jq}} |
{{works with|jq}} |
||
'''Works with gojq, the Go implementation of jq''' |
'''Works with gojq, the Go implementation of jq''' |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
def sum_of_digits_is_substring: |
def sum_of_digits_is_substring: |
||
tostring |
tostring |
||
Line 1,212: | Line 1,212: | ||
| $s | index($ss); |
| $s | index($ss); |
||
[range(0;1000) | select(sum_of_digits_is_substring)]</ |
[range(0;1000) | select(sum_of_digits_is_substring)]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,219: | Line 1,219: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight 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))) |
foreach(p -> print(rpad(p[2], 4), p[1] % 10 == 0 ? "\n" : ""), enumerate(filter(issumsub, 0:999))) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
0 1 2 3 4 5 6 7 8 9 |
0 1 2 3 4 5 6 7 8 9 |
||
Line 1,233: | Line 1,233: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="scala">fun digitSum(n: Int): Int { |
||
var nn = n |
var nn = n |
||
var sum = 0 |
var sum = 0 |
||
Line 1,258: | Line 1,258: | ||
} |
} |
||
println() |
println() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0 1 2 3 4 5 6 7 |
<pre> 0 1 2 3 4 5 6 7 |
||
Line 1,268: | Line 1,268: | ||
=={{header|MAD}}== |
=={{header|MAD}}== |
||
< |
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER |
||
INTERNAL FUNCTION(A,B) |
INTERNAL FUNCTION(A,B) |
||
Line 1,321: | Line 1,321: | ||
VECTOR VALUES FMT = $I3*$ |
VECTOR VALUES FMT = $I3*$ |
||
END OF PROGRAM </ |
END OF PROGRAM </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style='height:50ex;'> 0 |
<pre style='height:50ex;'> 0 |
||
Line 1,373: | Line 1,373: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">ClearAll[SumAsSubString] |
||
SumAsSubString[n_Integer] := Module[{id, s}, |
SumAsSubString[n_Integer] := Module[{id, s}, |
||
id = IntegerDigits[n]; |
id = IntegerDigits[n]; |
||
Line 1,379: | Line 1,379: | ||
SequenceCount[id, IntegerDigits[s]] > 0 |
SequenceCount[id, IntegerDigits[s]] > 0 |
||
] |
] |
||
Select[Range[999], SumAsSubString]</ |
Select[Range[999], SumAsSubString]</syntaxhighlight> |
||
{{out}} |
{{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> |
<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|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import strutils |
||
func digitsum(n: Natural): int = |
func digitsum(n: Natural): int = |
||
Line 1,398: | Line 1,398: | ||
if $digitsum(n) in sn: |
if $digitsum(n) in sn: |
||
inc count |
inc count |
||
stdout.write sn.align(3), if count mod 8 == 0: '\n' else: ' '</ |
stdout.write sn.align(3), if count mod 8 == 0: '\n' else: ' '</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,410: | Line 1,410: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
as one-liner .. |
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/}'</ |
perl -e 'for(0..999){my$n;s/(\d)/$n+=$1/egr;print"$_ "if/$n/}'</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,419: | Line 1,419: | ||
=={{header|Phix}}== |
=={{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: #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: #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,428: | Line 1,428: | ||
<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: #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> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,436: | Line 1,436: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pli">sumOfDigitsIsSubstring: procedure options(main); |
||
digitSum: procedure(n) returns(fixed); |
digitSum: procedure(n) returns(fixed); |
||
declare (ds, x, n) fixed; |
declare (ds, x, n) fixed; |
||
Line 1,475: | Line 1,475: | ||
end; |
end; |
||
put skip; |
put skip; |
||
end sumOfDigitsIsSubstring;</ |
end sumOfDigitsIsSubstring;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0 1 2 3 4 5 6 7 8 9 |
<pre> 0 1 2 3 4 5 6 7 8 9 |
||
Line 1,484: | Line 1,484: | ||
=={{header|PL/M}}== |
=={{header|PL/M}}== |
||
< |
<syntaxhighlight lang="plm">100H: |
||
DIGIT$SUM: PROCEDURE (N) BYTE; |
DIGIT$SUM: PROCEDURE (N) BYTE; |
||
DECLARE N ADDRESS, SUM BYTE; |
DECLARE N ADDRESS, SUM BYTE; |
||
Line 1,556: | Line 1,556: | ||
CALL BDOS(0,0); |
CALL BDOS(0,0); |
||
EOF</ |
EOF</syntaxhighlight> |
||
{{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> |
||
Line 1,563: | Line 1,563: | ||
Just using the command line: |
Just using the command line: |
||
< |
<syntaxhighlight 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. |
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)] |
>>> x = [n for n in range(1000) if str(sum(int(d) for d in str(n))) in str(n)] |
||
Line 1,575: | Line 1,575: | ||
200, 300, 400, 500, 600, 700, 800, 900, 910, 911 |
200, 300, 400, 500, 600, 700, 800, 900, 910, 911 |
||
912, 913, 914, 915, 916, 917, 918, 919 |
912, 913, 914, 915, 916, 917, 918, 919 |
||
>>> </ |
>>> </syntaxhighlight> |
||
or as a full script, taking an alternative route, and slightly reducing the number of str conversions required: |
or as a full script, taking an alternative route, and slightly reducing the number of str conversions required: |
||
< |
<syntaxhighlight lang="python">'''Sum of the digits of n is substring of n''' |
||
from functools import reduce |
from functools import reduce |
||
Line 1,653: | Line 1,653: | ||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main() |
main() |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre>48 matches < 1000: |
<pre>48 matches < 1000: |
||
Line 1,664: | Line 1,664: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>say "{+$_} matching numbers\n{.batch(10)».fmt('%3d').join: "\n"}" given (^1000).grep: { .contains: .comb.sum }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>48 matching numbers |
<pre>48 matching numbers |
||
Line 1,674: | Line 1,674: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight 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.*/ |
parse arg hi cols . /*obtain optional argument from the CL.*/ |
||
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/ |
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/ |
||
Line 1,702: | Line 1,702: | ||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ? |
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</ |
sumDigs:procedure; parse arg x 1 s 2;do j=2 for length(x)-1;s=s+substr(x,j,1);end;return s</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 1,718: | Line 1,718: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
see "working..." + nl |
see "working..." + nl |
||
Line 1,745: | Line 1,745: | ||
see nl + "Found " + row + " numbers" + nl |
see nl + "Found " + row + " numbers" + nl |
||
see "done..." + nl |
see "done..." + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,760: | Line 1,760: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">var upto = 1000 |
||
var base = 10 |
var base = 10 |
||
Line 1,773: | Line 1,773: | ||
}) |
}) |
||
say "\n#{list.len} such numbers found."</ |
say "\n#{list.len} such numbers found."</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,788: | Line 1,788: | ||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
< |
<syntaxhighlight lang="snobol4"> define('digsum(n)') :(digsum_end) |
||
digsum digsum = 0 |
digsum digsum = 0 |
||
dsloop digsum = digsum + remdr(n,10) |
dsloop digsum = digsum + remdr(n,10) |
||
Line 1,801: | Line 1,801: | ||
loop output = sumsub(i) i |
loop output = sumsub(i) i |
||
i = lt(i,999) i + 1 :s(loop) |
i = lt(i,999) i + 1 :s(loop) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style='height:50ex'>0 |
<pre style='height:50ex'>0 |
||
Line 1,856: | Line 1,856: | ||
{{libheader|Wren-seq}} |
{{libheader|Wren-seq}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/math" for Int |
||
import "/seq" for Lst |
import "/seq" for Lst |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 1,868: | Line 1,868: | ||
System.print("Numbers under 1,000 whose sum of digits is a substring of themselves:") |
System.print("Numbers under 1,000 whose sum of digits is a substring of themselves:") |
||
for (chunk in Lst.chunks(numbers, 8)) Fmt.print("$3d", chunk) |
for (chunk in Lst.chunks(numbers, 8)) Fmt.print("$3d", chunk) |
||
System.print("\n%(numbers.count) such numbers found.")</ |
System.print("\n%(numbers.count) such numbers found.")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,884: | Line 1,884: | ||
=={{header|XPL0}}== |
=={{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; |
int N, Sum, A, B, C; |
||
[N:= N/10; |
[N:= N/10; |
||
Line 1,910: | Line 1,910: | ||
Text(0, " such numbers found below 1000. |
Text(0, " such numbers found below 1000. |
||
"); |
"); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,923: | Line 1,923: | ||
=={{header|Yabasic}}== |
=={{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 |
// by Galileo, 04/2022 |
||
Line 1,941: | Line 1,941: | ||
return instr(n$, str$(p)) |
return instr(n$, str$(p)) |
||
end sub</ |
end sub</syntaxhighlight> |
||
{{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> 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 |