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 17:50, 28 August 2022
, 1 year agosyntax highlighting fixup automation
Drkameleon (talk | contribs) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
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 ) ;
Line 365:
end if_n_contains_dSum
end for_n
end.</
{{out}}
<pre>
Line 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
Line 385:
=={{header|Arturo}}==
<
contains? to :string num
to :string sum digits num</
{{out}}
Line 394:
=={{header|AutoHotkey}}==
<
loop 1000{
n := A_Index-1, sum := 0
Line 405:
}
}
MsgBox % result</
{{out}}
<pre>0 1 2 3 4 5 6 7
Line 415:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SUM_OF_THE_DIGITS_OF_N_IS_SUBSTRING_OF_N.AWK
BEGIN {
Line 434:
return(sum)
}
</syntaxhighlight>
{{out}}
<pre>
Line 446:
=={{header|BASIC}}==
<
20 FOR I=0 TO 999
30 J=0: K=I
Line 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 468:
=={{header|BCPL}}==
<
let dsum(n) = n=0 -> 0, n rem 10 + dsum(n/10)
Line 496:
$)
wrch('*N')
$)</
{{out}}
<pre> 0 1 2 3 4 5 6 7 8 9
Line 505:
=={{header|BQN}}==
<
Contains ← (∨´⍷˜ )○•Fmt
∘‿6⥊ (⊢ Contains DigitSum)¨⊸/↕1000</
{{out}}
<pre>┌─
Line 520:
┘</pre>
=={{header|C}}==
<
#include <string.h>
Line 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 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 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 611:
=={{header|COBOL}}==
<
PROGRAM-ID. SUM-SUBSTRING.
Line 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 715:
=={{header|Comal}}==
<
0020 sum#:=0
0030 WHILE n# DO sum#:+n# MOD 10;n#:=n# DIV 10
Line 731:
0150 ENDFOR i#
0160 PRINT
0170 END</
{{out}}
<pre>0 1 2 3 4 5 6 7 8 9 10 20 30 40 50
Line 739:
=={{header|Cowgol}}==
<
sub digitSum(n: uint16): (s: uint16) is
Line 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 788:
=={{header|D}}==
{{trans|C++}}
<
import std.conv;
import std.stdio;
Line 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|Draco}}==
<
proc nonrec digit_sum(word n) word:
Line 848:
fi
od
corp</
{{out}}
<pre> 0 1 2 3 4 5 6 7 8 9 10 20 30 40 50 60 70 80 90 100
Line 855:
=={{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 869:
=={{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 887:
=={{header|Fermat}}==
No string conversion.
<
ds:=0; {digital sum of n in base b}
while n>0 do
Line 914:
nt:=nt\10;
od;
od;</
{{out}}<pre>
1
Line 966:
=={{header|FOCAL}}==
<
01.20 Q
Line 990:
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 1,042:
=={{header|FreeBASIC}}==
<
dim as integer nj = len(j), ns = len(s)
for i as integer = 1 to ns - nj + 1
Line 1,061:
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>
Line 1,075:
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 1,096:
fmt.Println()
fmt.Println(len(numbers), "such numbers found.")
}</
{{out}}
Line 1,112:
=={{header|Haskell}}==
<
import Data.List (isInfixOf)
import Data.List.Split (chunksOf)
Line 1,147:
justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)</
{{Out}}
<pre>48 matches in [0..999]
Line 1,197:
=={{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,204:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
def sum_of_digits_is_substring:
tostring
Line 1,212:
| $s | index($ss);
[range(0;1000) | select(sum_of_digits_is_substring)]</
{{out}}
<pre>
Line 1,219:
=={{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,233:
=={{header|Kotlin}}==
{{trans|Go}}
<
var nn = n
var sum = 0
Line 1,258:
}
println()
}</
{{out}}
<pre> 0 1 2 3 4 5 6 7
Line 1,268:
=={{header|MAD}}==
<
INTERNAL FUNCTION(A,B)
Line 1,321:
VECTOR VALUES FMT = $I3*$
END OF PROGRAM </
{{out}}
<pre style='height:50ex;'> 0
Line 1,373:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
SumAsSubString[n_Integer] := Module[{id, s},
id = IntegerDigits[n];
Line 1,379:
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|Nim}}==
<
func digitsum(n: Natural): int =
Line 1,398:
if $digitsum(n) in sn:
inc count
stdout.write sn.align(3), if count mod 8 == 0: '\n' else: ' '</
{{out}}
Line 1,410:
=={{header|Perl}}==
as one-liner ..
<
perl -e 'for(0..999){my$n;s/(\d)/$n+=$1/egr;print"$_ "if/$n/}'</
{{out}}
<pre>
Line 1,419:
=={{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,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: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 1,436:
=={{header|PL/I}}==
<
digitSum: procedure(n) returns(fixed);
declare (ds, x, n) fixed;
Line 1,475:
end;
put skip;
end sumOfDigitsIsSubstring;</
{{out}}
<pre> 0 1 2 3 4 5 6 7 8 9
Line 1,484:
=={{header|PL/M}}==
<
DIGIT$SUM: PROCEDURE (N) BYTE;
DECLARE N ADDRESS, SUM BYTE;
Line 1,556:
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,563:
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,575:
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,653:
if __name__ == '__main__':
main()
</syntaxhighlight>
{{Out}}
<pre>48 matches < 1000:
Line 1,664:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
{{out}}
<pre>48 matching numbers
Line 1,674:
=={{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,702:
/*──────────────────────────────────────────────────────────────────────────────────────*/
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,718:
=={{header|Ring}}==
<
load "stdlib.ring"
see "working..." + nl
Line 1,745:
see nl + "Found " + row + " numbers" + nl
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,760:
=={{header|Sidef}}==
<
var base = 10
Line 1,773:
})
say "\n#{list.len} such numbers found."</
{{out}}
<pre>
Line 1,788:
=={{header|SNOBOL4}}==
<
digsum digsum = 0
dsloop digsum = digsum + remdr(n,10)
Line 1,801:
loop output = sumsub(i) i
i = lt(i,999) i + 1 :s(loop)
end</
{{out}}
<pre style='height:50ex'>0
Line 1,856:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<
import "/seq" for Lst
import "/fmt" for Fmt
Line 1,868:
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)
System.print("\n%(numbers.count) such numbers found.")</
{{out}}
Line 1,884:
=={{header|XPL0}}==
<
int N, Sum, A, B, C;
[N:= N/10;
Line 1,910:
Text(0, " such numbers found below 1000.
");
]</
{{out}}
Line 1,923:
=={{header|Yabasic}}==
<
// by Galileo, 04/2022
Line 1,941:
return instr(n$, str$(p))
end sub</
{{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
|