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

m
(added Arturo implementation)
m (→‎{{header|Wren}}: Minor tidy)
 
(11 intermediate revisions by 5 users not shown)
Line 13:
<pre>
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>
 
=={{header|8080 Assembly}}==
<syntaxhighlight lang="asm"> org 100h
loop: lda tho ; Are we there yet?
cpi '0'
rnz
call incr ; If not, increment
lxi b,0203h ; Need two ones, 3 steps
mvi a,'1'
lxi h,acc
ones: cmp m ; Count the ones
jnz $+4
dcr b
dcr c
inx h
jnz ones
xra a ; If two ones, print
ora b
cz prn
jmp loop
 
incr: lxi h,acc ; Increment accumulator
mvi a,'9'+1
incrl: inr m
cmp m
rnz
mvi m,'0'
inx h
jmp incrl
 
prn: lxi h,acc+4 ; Print accumulator w/o leading zero
mvi a,'0'
skip: dcx h
cmp m
jz skip
prl: push h
mvi c,2 ; CP/M print character
mov e,m
call 5
pop h
dcx h
xra a
cmp m
jnz prl
mvi c,9 ; CP/M print newline
lxi d,nl
jmp 5
 
acc: db '000' ; Accumulator (stored backwards)
tho: db '0' ; Thousands digit (stop if not 0 anymore)
nl: db 13,10,'$' ; Newline</syntaxhighlight>
{{out}}
<pre>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>
 
=={{header|Action!}}==
Line 136 ⟶ 215:
Found 27 numbers
</pre>
=={{header|arturoAPL}}==
<syntaxhighlight lang="apl">(⊢(/⍨)(2='1'+.=⍕)¨) ⍳1000</syntaxhighlight>
{{out}}
<pre>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>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">prints [11 101 110]
Line 144 ⟶ 228:
 
<pre>11 101 110 211 121 112 311 131 113 411 141 114 511 151 115 611 161 116 711 171 117 811 181 118 911 191 119</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
Line 166 ⟶ 251:
Number 1 occurs twice 1-999: 27
</pre>
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. TWO-ONES.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 NUM PIC 9999.
77 FMT PIC ZZ9.
77 ONES PIC 9.
PROCEDURE DIVISION.
BEGIN.
PERFORM COUNT-ONES
VARYING NUM FROM 1 BY 1 UNTIL NUM IS EQUAL TO 1000.
STOP RUN.
COUNT-ONES.
MOVE ZERO TO ONES.
INSPECT NUM TALLYING ONES FOR ALL '1'.
IF ONES IS EQUAL TO 2,
MOVE NUM TO FMT,
DISPLAY FMT.</syntaxhighlight>
{{out}}
<pre> 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>
 
=={{header|Delphi}}==
Line 171 ⟶ 307:
 
=={{header|Euler}}==
As with the Arturo, F# etc. samples, generates the sequence but doesn't sort it into order.
<syntaxhighlight lang="euler">
'''begin''' '''new''' dPos; '''new''' inon1digits; '''label''' iLoopdigitLoop;
non1digits &lt;- ( 0, 2, 3, 4, 5, 6, 7, 8, 9 );
i <- 9;
iLoop: if [ i <- i + 1 ] < 1000 then begindPos &lt;- 0;
digitLoop: '''if''' [ dPos &lt;- dPos + 1 ] &lt;= '''length''' non1digits '''then''' '''begin'''
new v; new oneCount; label vloop;
oneCount <- 0 '''new''' d;
v < d &lt;- inon1digits[ dPos ];
'''out''' [ d * 100 ] + 11; '''out''' [ d * 10 ] + 101; '''out''' 110 + d;
vLoop: if v > 0 then begin
if [ v'''goto''' mod 10 ] = 1 then oneCount <- oneCount + 1digitLoop
'''end''' '''else''' 0;
'''end''' $
v <- v % 10;
goto vLoop
end else 0;
if oneCount = 2 then out i else 0;
goto iLoop
end else 0
end $
</syntaxhighlight>
{{out}}
<pre>
Line 194 ⟶ 323:
NUMBER 101
NUMBER 110
NUMBER 112
NUMBER 113
NUMBER 114
NUMBER 115
NUMBER 116
NUMBER 117
NUMBER 118
NUMBER 119
NUMBER 121
NUMBER 131
NUMBER 141
NUMBER 151
NUMBER 161
NUMBER 171
NUMBER 181
NUMBER 191
NUMBER 211
NUMBER 121
NUMBER 112
NUMBER 311
NUMBER 131
NUMBER 113
NUMBER 411
NUMBER 141
NUMBER 114
NUMBER 511
NUMBER 151
NUMBER 115
NUMBER 611
NUMBER 161
NUMBER 116
NUMBER 711
NUMBER 171
NUMBER 117
NUMBER 811
NUMBER 181
NUMBER 118
NUMBER 911
NUMBER 191
NUMBER 119
</pre>
 
Line 829 ⟶ 958:
done...
</pre>
 
=={{header|RPL}}==
≪ →STR 0
1 3 PICK SIZE '''FOR''' j
OVER j DUP SUB "1" == + '''NEXT'''
SWAP DROP 2 ==
≫ '<span style="color:blue">ONLY2?</span>' STO
≪ { }
1 1000 '''FOR''' j
'''IF''' j <span style="color:blue">ONLY2?</span> '''THEN''' j + '''END NEXT'''
≫ EVAL
 
{{out}}
<pre>
1: { 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>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">p (1..1000).select{|n| n.digits.count(1) == 2}
</syntaxhighlight>
{{out}}
<pre>
[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>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program two_ones;
print([n : n in [1..999] | 2 = #[d : d in str n | val d = 1]]);
end program;</syntaxhighlight>
{{out}}
<pre>[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>
 
=={{header|Sidef}}==
Line 839 ⟶ 1,000:
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./seqfmt" for LstFmt
import "/fmt" for Fmt
 
System.print("Decimal numbers under 1,000 whose digits include two 1's:")
var results = (11..911).where { |i| Int.digits(i).count { |d| d == 1 } == 2 }.toList
Fmt.tprint("$5d", results, 7)
for (chunk in Lst.chunks(results, 7)) Fmt.print("$5d", chunk)
System.print("\nFound %(results.count) such numbers.")</syntaxhighlight>
 
9,476

edits