Sum of the digits of n is substring of n
Find and show numbers n with property that the sum of the digits of n is substring of n, where n < 1000
- Task
REXX
<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.*/ if cols== | cols=="," then cols= 10 /* " " " " " " */ w= 10 /*width of a number in any column. */ @sdsN= ' integers whose sum of decimal digis of N is a substring of N, where N < ' ,
commas(hi)
if cols>0 then say ' index │'center(@sdsN, 1 + cols*(w+1) ) if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─') finds= 0; idx= 1 /*initialize # of found numbers & index*/ $= /*a list of found integers (so far). */
do j=0 for hi; #= sumDigs(j) /*obtain sum of the decimal digits of J*/ if pos(#, j)==0 then iterate /*Sum of dec. digs in J? No, then skip*/ finds= finds + 1 /*bump the number of found integers. */ if cols==0 then iterate /*Build the list (to be shown later)? */ $= $ right( commas( commas(j) ), w) /*add a found number ──► the $ list. */ if finds//cols\==0 then iterate /*have we populated a line of output? */ say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */ idx= idx + cols /*bump the index count for the output*/ end /*j*/
if $\== then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─') say say 'Found ' commas(finds) @sdsN exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ 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</lang>
- output when using the default inputs:
index │ integers whose sum of decimal digis of N is a substring of N, where N < 1,000 ───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── 1 │ 0 1 2 3 4 5 6 7 8 9 11 │ 10 20 30 40 50 60 70 80 90 100 21 │ 109 119 129 139 149 159 169 179 189 199 31 │ 200 300 400 500 600 700 800 900 910 911 41 │ 912 913 914 915 916 917 918 919 ───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── Found 48 integers whose sum of decimal digis of N is a substring of N, where N < 1,000
Ring
<lang ring> load "stdlib.ring" see "working..." + nl see "Numbers n with property that the sum of the digits of n is substring of n are:" + nl see "p p+2 p+6" + nl row = 0 limit = 1000
for n = 0 to limit-1
str = 0 strn = string(n) for m = 1 to len(strn) str = str + number(strn[m]) next str = string(str) ind = substr(strn,str) if ind > 0 row = row + 1 see "" + n + " " if row%10 = 0 see nl ok ok
next
see nl + "Found " + row + " numbers" + nl see "done..." + nl </lang>
- Output:
working... Numbers n with property that the sum of the digits of n is substring of n are: 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 Found 48 numbers done...
Python
Just using the command line:
<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)] >>> len(x) 48 >>> for i in range(0, len(x), (stride:= 10)): print(str(x[i:i+stride])[1:-1])
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 >>> </lang>