Positive decimal integers with the digit 1 occurring exactly twice: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Algol 68)
Line 31: Line 31:
119 121 131 141 151 161 171 181 191 211
119 121 131 141 151 161 171 181 191 211
311 411 511 611 711 811 911
311 411 511 611 711 811 911
</pre>

=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">two_ones</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">find_all</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'1'</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)))=</span><span style="color: #000000;">2</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">999</span><span style="color: #0000FF;">),</span><span style="color: #000000;">two_ones</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;">"%d found:\n %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: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n "</span><span style="color: #0000FF;">)})</span>
<!--</lang>-->
{{out}}
<pre>
27 found:
11 101 110 112 113 114 115 116 117
118 119 121 131 141 151 161 171 181
191 211 311 411 511 611 711 811 911
</pre>
</pre>



Revision as of 12:45, 8 July 2021

Positive decimal integers with the digit 1 occurring exactly twice 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 numbers n in which number 1 occur twice, where n < 1,000



ALGOL 68

Generates the numbers. In order to print them in order, a table of double 1 numbers yes/no is generated. <lang algol68>BEGIN # find numbers where the digit 1 occurs twice, up to 999 #

   [ 1 : 999 ]BOOL double 1; FOR i TO UPB double 1 DO double 1[ i ] := FALSE OD;
   # generte the numbers                                      #
   FOR i FROM 0 TO 9 DO
       IF i /= 1 THEN
           double 1[ 110 + i ] := TRUE;
           double 1[ 101 + ( i * 10 ) ] := TRUE;
           double 1[ ( i * 100 ) + 11 ] := TRUE
       FI
   OD;
   # print the numbers in order                               #
   INT double 1 count := 0;
   FOR i TO UPB double 1 DO
       IF double 1[ i ] THEN
           print( ( " ", whole( i, -3 ) ) );
           IF ( double 1 count +:= 1 ) MOD 10 = 0 THEN print( ( newline ) ) FI
       FI
   OD

END</lang>

Output:
  11 101 110 112 113 114 115 116 117 118
 119 121 131 141 151 161 171 181 191 211
 311 411 511 611 711 811 911

Phix

function two_ones(integer i) return length(find_all('1',sprint(i)))=2 end function
sequence res = filter(tagset(999),two_ones)
printf(1,"%d found:\n  %s\n",{length(res),join_by(apply(res,sprint),1,9," ","\n ")})
Output:
27 found:
  11 101 110 112 113 114 115 116 117
 118 119 121 131 141 151 161 171 181
 191 211 311 411 511 611 711 811 911

Ring

<lang ring> load "stdlib.ring" see "working..." + nl see "Numbers n in which number 1 occur twice:" + nl

row = 0 sum = 0 limit = 1000

for n = 1 to limit

   strn = string(n)
   ind = count(strn,"1")
   if ind = 2
      see "" + n + " " 
      row = row + 1
      if row%5 = 0
         see nl
      ok
   ok

next

see nl + "Found " + row + " numbers" + nl see "done..." + nl

func count(cstring,dstring)

    sum = 0
    while substr(cstring,dstring) > 0
          sum = sum + 1
          cstring = substr(cstring,substr(cstring,dstring)+len(string(sum)))
    end
    return sum

</lang>

Output:
working...
Numbers n in which number 1 occur twice:
11 101 110 112 113 
114 115 116 117 118 
119 121 131 141 151 
161 171 181 191 211 
311 411 511 611 711 
811 911 
Found 27 numbers
done...