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

m
m (→‎{{header|RPL}}: optimized code)
m (→‎{{header|Wren}}: Minor tidy)
 
(5 intermediate revisions by 2 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|APL}}==
<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}}==
 
Line 167 ⟶ 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 173 ⟶ 308:
=={{header|Euler}}==
As with the Arturo, F# etc. samples, generates the sequence but doesn't sort it into order.
'''begin''' '''new''' dPos; '''new''' non1digits; '''label''' digitLoop;
<syntaxhighlight lang="euler">
begin new dPos; new non1digits &lt;- ( 0, 2, 3, 4, 5, 6, 7, 8, label9 digitLoop);
non1digits <dPos &lt;- ( 0, 2, 3, 4, 5, 6, 7, 8, 9 );
digitLoop: '''if''' [ dPos &lt;- dPos + 1 ] &lt;= '''length''' non1digits '''then''' '''begin'''
dPos <- 0;
'''new''' d;
digitLoop: if [ dPos <- dPos + 1 ] <= length non1digits then begin
new d &lt;- non1digits[ dPos ];
'''out''' [ d <-* 100 ] + 11; '''out''' non1digits[ dPosd * 10 ] + 101; '''out''' 110 + d;
out ['''goto''' d * 100 ] + 11; out [ d * 10 ] + 101; out 110 + d;digitLoop
'''end''' '''else''' goto digitLoop0
'''end''' $
end else 0
end $
</syntaxhighlight>
{{out}}
<pre>
Line 850 ⟶ 983:
[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 860 ⟶ 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