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

Line 157:
Found 27 such numbers.
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
This entry contains two solutions: the first is fast for this problem, but algorithmically inefficient;
the second uses `countEquals` so candidates can be discarded quickly. The output is the same in both cases and so is shown only once.
 
'''Using `count`'''
<lang jq>def count(s): reduce s as $x (0; .+1);
 
range(1;1000)
| select( tostring | explode | 2 == count( select(.[] == 49))) # "1"</lang>
'''Using `countEquals`'''
<lang jq>def countEquals($n; s):
label $out
| foreach (s, null) as $x (-1;
. + 1;
if $x == null then . == $n
elif . > $n then false, break $out
else empty
end);
range(1;1000)
| select( tostring
| countEquals(2; select(explode[] == 49))) # "1"</lang>
{{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|Julia}}==
2,442

edits