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

From Rosetta Code
Content added Content deleted
(Created page with "{{Draft task}} ;Task: Find and show numbers n with property that the sum of the digits of n is substring of n, where '''n < 1000''' <br><br> =={{header|Ring}}== <lang ring>...")
 
Line 46: Line 46:
done...
done...
</pre>
</pre>

=={{header|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>

Revision as of 06:29, 14 April 2021

Sum of the digits of n is substring of n is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

Find and show numbers n with property that the sum of the digits of n is substring of n, where n < 1000

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>