Positive decimal integers with the digit 1 occurring exactly twice: Difference between revisions
m →{{header|J}}: last edit comment should have said "discard duplicates..." |
Thundergnat (talk | contribs) m syntax highlighting fixup automation |
||
Line 6: | Line 6: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
< |
<syntaxhighlight lang="11l">L(n) 1..999 |
||
I String(n).count(‘1’) == 2 |
I String(n).count(‘1’) == 2 |
||
print(n, end' ‘ ’)</ |
print(n, end' ‘ ’)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 15: | Line 15: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">BYTE FUNC OnesCount(INT x) |
||
BYTE c,d |
BYTE c,d |
||
Line 38: | Line 38: | ||
FI |
FI |
||
OD |
OD |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Positive_decimal_integers_with_the_digit_1_occurring_exactly_twice.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Positive_decimal_integers_with_the_digit_1_occurring_exactly_twice.png Screenshot from Atari 8-bit computer] |
||
Line 46: | Line 46: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
procedure Positives_With_1_Twice is |
procedure Positives_With_1_Twice is |
||
Line 70: | Line 70: | ||
end loop; |
end loop; |
||
New_Line; |
New_Line; |
||
end Positives_With_1_Twice;</ |
end Positives_With_1_Twice;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 77: | Line 77: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Generates the numbers. In order to print them in order, a table of double 1 numbers yes/no is generated. |
Generates the numbers. In order to print them in order, a table of double 1 numbers yes/no is generated. |
||
< |
<syntaxhighlight 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; |
[ 1 : 999 ]BOOL double 1; FOR i TO UPB double 1 DO double 1[ i ] := FALSE OD; |
||
# generte the numbers # |
# generte the numbers # |
||
Line 95: | Line 95: | ||
FI |
FI |
||
OD |
OD |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 105: | Line 105: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
Generates the numbers and sorts them into order using [[Sorting algorithms/Quicksort#ALGOL W]]. |
Generates the numbers and sorts them into order using [[Sorting algorithms/Quicksort#ALGOL W]]. |
||
< |
<syntaxhighlight lang="algolw">begin % find numbers where the digit 1 occurs twice, up to 999 % |
||
integer double1Count; |
integer double1Count; |
||
integer array double1 ( 1 :: 100 ); % assume there will be at most 100 numbers % |
integer array double1 ( 1 :: 100 ); % assume there will be at most 100 numbers % |
||
Line 129: | Line 129: | ||
write(); |
write(); |
||
write( i_w := 1, s_w := 0, "Found ", double1Count, " numbers" ) |
write( i_w := 1, s_w := 0, "Found ", double1Count, " numbers" ) |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 137: | Line 137: | ||
</pre> |
</pre> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f NUMBERS_N_IN_WHICH_NUMBER_1_OCCUR_TWICE.AWK |
# syntax: GAWK -f NUMBERS_N_IN_WHICH_NUMBER_1_OCCUR_TWICE.AWK |
||
BEGIN { |
BEGIN { |
||
Line 150: | Line 150: | ||
exit(0) |
exit(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 163: | Line 163: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// 3 digit numbers with 2 ones. Nigel Galloway: July 6th., 2021 |
// 3 digit numbers with 2 ones. Nigel Galloway: July 6th., 2021 |
||
[0;2;3;4;5;6;7;8;9]|>List.collect(fun g->[[g;1;1];[1;g;1];[1;1;g]])|>List.iter(fun(n::g::l::_)->printf "%d " (n*100+g*10+l)); printfn "" |
[0;2;3;4;5;6;7;8;9]|>List.collect(fun g->[[g;1;1];[1;g;1];[1;1;g]])|>List.iter(fun(n::g::l::_)->printf "%d " (n*100+g*10+l)); printfn "" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 175: | Line 175: | ||
{{trans|F#}} |
{{trans|F#}} |
||
{{works with|Factor|0.99 2021-06-02}} |
{{works with|Factor|0.99 2021-06-02}} |
||
< |
<syntaxhighlight lang="factor">USING: io math math.functions prettyprint sequences |
||
sequences.extras ; |
sequences.extras ; |
||
{ 0 2 3 4 5 6 7 8 9 } |
{ 0 2 3 4 5 6 7 8 9 } |
||
[| n | { { n 1 1 } { 1 n 1 } { 1 1 n } } ] map-concat |
[| n | { { n 1 1 } { 1 n 1 } { 1 1 n } } ] map-concat |
||
[ <reversed> 0 [ 10^ * + ] reduce-index pprint bl ] each nl</ |
[ <reversed> 0 [ 10^ * + ] reduce-index pprint bl ] each nl</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 187: | Line 187: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">function numdig(byval n as integer, d as const integer) as uinteger |
||
'counts the number of occurrences of digit d in the number n |
'counts the number of occurrences of digit d in the number n |
||
dim as uinteger m = 0 |
dim as uinteger m = 0 |
||
Line 200: | Line 200: | ||
if numdig(i, 1) = 2 then print i;" "; |
if numdig(i, 1) = 2 then print i;" "; |
||
next i |
next i |
||
print</ |
print</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> |
{{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> |
||
Line 209: | Line 209: | ||
{{trans|Wren}} |
{{trans|Wren}} |
||
{{libheader|Go-rcu}} |
{{libheader|Go-rcu}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 238: | Line 238: | ||
} |
} |
||
fmt.Println("\n\nFound", len(results), "such numbers.") |
fmt.Println("\n\nFound", len(results), "such numbers.") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 253: | Line 253: | ||
=={{header|J}}== |
=={{header|J}}== |
||
Generating the numbers as the cartesian product of the eligible digits (and sorting the result, for "prettiness"): |
Generating the numbers as the cartesian product of the eligible digits (and sorting the result, for "prettiness"): |
||
< |
<syntaxhighlight lang="j"> ~./:~10 #. >,{~.(2 1#1;i.10){~(! A.&i. ])3 |
||
11 101 110 111 112 113 114 115 116 117 118 119 121 131 141 151 161 171 181 191 211 311 411 511 611 711 811 911</ |
11 101 110 111 112 113 114 115 116 117 118 119 121 131 141 151 161 171 181 191 211 311 411 511 611 711 811 911</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 264: | Line 264: | ||
'''Using `count`''' |
'''Using `count`''' |
||
< |
<syntaxhighlight lang="jq">def count(s): reduce s as $x (0; .+1); |
||
range(1;1000) |
range(1;1000) |
||
| select( tostring | explode | 2 == count( select(.[] == 49))) # "1"</ |
| select( tostring | explode | 2 == count( select(.[] == 49))) # "1"</syntaxhighlight> |
||
'''Using `countEquals`''' |
'''Using `countEquals`''' |
||
< |
<syntaxhighlight lang="jq">def countEquals($n; s): |
||
label $out |
label $out |
||
| foreach (s, null) as $x (-1; |
| foreach (s, null) as $x (-1; |
||
Line 280: | Line 280: | ||
range(1;1000) |
range(1;1000) |
||
| select( tostring |
| select( tostring |
||
| countEquals(2; select(explode[] == 49))) # "1"</ |
| countEquals(2; select(explode[] == 49))) # "1"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 313: | Line 313: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">totalddigisn(x, d = 1, n = 2; base=10) = count(j -> j == d, digits(x; base)) == n |
||
println(filter(totalddigisn, 1:1000)) |
println(filter(totalddigisn, 1:1000)) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<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] |
[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> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">sol = Cases[Range[999], _?(Count[IntegerDigits@#, 1] == 2 &)]; |
||
Partition[sol, Max[FactorInteger[Length@sol][[All, 1]]]] // TableForm</ |
Partition[sol, Max[FactorInteger[Length@sol][[All, 1]]]] // TableForm</syntaxhighlight> |
||
{{out}}<pre> |
{{out}}<pre> |
||
Line 337: | Line 337: | ||
=={{header|Ksh}}== |
=={{header|Ksh}}== |
||
< |
<syntaxhighlight lang="ksh"> |
||
#!/bin/ksh |
#!/bin/ksh |
||
Line 357: | Line 357: | ||
[[ ${i} == *{2}(1)* ]] && printf "%d " ${i} |
[[ ${i} == *{2}(1)* ]] && printf "%d " ${i} |
||
done |
done |
||
echo</ |
echo</syntaxhighlight> |
||
{{out}}<pre>11 110 111 112 113 114 115 116 117 118 119 211 311 411 511 611 711 811 911 |
{{out}}<pre>11 110 111 112 113 114 115 116 117 118 119 211 311 411 511 611 711 811 911 |
||
</pre> |
</pre> |
||
=={{header|MiniZinc}}== |
=={{header|MiniZinc}}== |
||
<syntaxhighlight lang="minizinc"> |
|||
<lang MiniZinc> |
|||
%Permutations with some identical elements. Nigel Galloway: July 6th., 2021 |
%Permutations with some identical elements. Nigel Galloway: July 6th., 2021 |
||
include "count.mzn"; |
include "count.mzn"; |
||
array [1..3] of var 0..9: N; constraint count(N,1,2); |
array [1..3] of var 0..9: N; constraint count(N,1,2); |
||
output [show((N[1]*100)+(N[2]*10)+N[3])] |
output [show((N[1]*100)+(N[2]*10)+N[3])] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 428: | Line 428: | ||
</pre> |
</pre> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import sugar, strutils |
||
let result = collect(newSeq): |
let result = collect(newSeq): |
||
Line 434: | Line 434: | ||
if count($n, '1') == 2: n |
if count($n, '1') == 2: n |
||
echo "Found ", result.len, " numbers:" |
echo "Found ", result.len, " numbers:" |
||
echo result.join(" ")</ |
echo result.join(" ")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 440: | Line 440: | ||
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> |
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|Pascal}}== |
=={{header|Pascal}}== |
||
< |
<syntaxhighlight lang="pascal">program positiveDecimalIntegersWithTheDigit1occurringExactlyTwice(output); |
||
var |
var |
||
n: integer; |
n: integer; |
||
Line 451: | Line 451: | ||
end |
end |
||
end |
end |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; # https://rosettacode.org/wiki/Numbers_n_in_which_number_1_occur_twice |
use strict; # https://rosettacode.org/wiki/Numbers_n_in_which_number_1_occur_twice |
||
Line 460: | Line 460: | ||
my @twoones = grep tr/1// =~ 2, 1 .. 1000; |
my @twoones = grep tr/1// =~ 2, 1 .. 1000; |
||
print "@twoones\n" =~ s/.{60}\K /\n/gr;</ |
print "@twoones\n" =~ s/.{60}\K /\n/gr;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 468: | Line 468: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight 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;">string</span> <span style="color: #000000;">s</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: #000000;">s</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: #008080;">function</span> <span style="color: #000000;">two_ones</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</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: #000000;">s</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;">apply</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: #7060A8;">sprint</span><span style="color: #0000FF;">),</span><span style="color: #000000;">two_ones</span><span style="color: #0000FF;">)</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;">apply</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: #7060A8;">sprint</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: #000000;">res</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> |
<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: #000000;">res</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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 482: | Line 482: | ||
=={{header|Pike}}== |
=={{header|Pike}}== |
||
< |
<syntaxhighlight lang="pike">int main() { |
||
int limit = 1000; |
int limit = 1000; |
||
Line 493: | Line 493: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{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> |
<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|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python"> |
||
#Aamrun, 5th October 2021 |
#Aamrun, 5th October 2021 |
||
Line 508: | Line 508: | ||
baseList.append(i) |
baseList.append(i) |
||
[print(int(''.join(map(str,j)))) for j in sorted(set(permutations(baseList)))] |
[print(int(''.join(map(str,j)))) for j in sorted(set(permutations(baseList)))] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 541: | Line 541: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>say display 10, '%3d', ^1000 .grep: { .comb.Bag{'1'} == 2 }; |
||
sub display { |
sub display { |
||
cache $^c; |
cache $^c; |
||
"{+$c} matching:\n" ~ $c.batch($^a)».fmt($^b).join: "\n" |
"{+$c} matching:\n" ~ $c.batch($^a)».fmt($^b).join: "\n" |
||
}</ |
}</syntaxhighlight> |
||
{{out|Yields}} |
{{out|Yields}} |
||
<pre>27 matching: |
<pre>27 matching: |
||
Line 556: | Line 556: | ||
=== version 1 === |
=== version 1 === |
||
Programming note: the three filters (marked below) could've been incorporated into one REXX statement if code golf is the desired goal. |
Programming note: the three filters (marked below) could've been incorporated into one REXX statement if code golf is the desired goal. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program finds positive decimal integers which contain exactly two ones (1s). */ |
||
parse arg hi cols . /*obtain optional argument from the CL.*/ |
parse arg hi cols . /*obtain optional argument from the CL.*/ |
||
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/ |
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/ |
||
Line 580: | Line 580: | ||
say '───────┴'center("" , 1 + cols*(w+1), '─') |
say '───────┴'center("" , 1 + cols*(w+1), '─') |
||
say |
say |
||
say 'Found ' found title</ |
say 'Found ' found title</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 595: | Line 595: | ||
=== version 2 === |
=== version 2 === |
||
Programming note: not all REXXes have the '''countstr''' BIF. |
Programming note: not all REXXes have the '''countstr''' BIF. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program finds positive decimal integers which contain exactly two ones (1s). */ |
||
parse arg hi cols . /*obtain optional argument from the CL.*/ |
parse arg hi cols . /*obtain optional argument from the CL.*/ |
||
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/ |
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/ |
||
Line 617: | Line 617: | ||
say '───────┴'center("" , 1 + cols*(w+1), '─') |
say '───────┴'center("" , 1 + cols*(w+1), '─') |
||
say |
say |
||
say 'Found ' found title</ |
say 'Found ' found title</syntaxhighlight> |
||
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} <br><br> |
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} <br><br> |
||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
see "working..." + nl |
see "working..." + nl |
||
Line 652: | Line 652: | ||
end |
end |
||
return sum |
return sum |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 668: | Line 668: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">say (1..1000 -> grep { .digits.count { _ == 1 } == 2 })</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 678: | Line 678: | ||
{{libheader|Wren-seq}} |
{{libheader|Wren-seq}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/math" for Int |
||
import "/seq" for Lst |
import "/seq" for Lst |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 685: | Line 685: | ||
var results = (11..911).where { |i| Int.digits(i).count { |d| d == 1 } == 2 }.toList |
var results = (11..911).where { |i| Int.digits(i).count { |d| d == 1 } == 2 }.toList |
||
for (chunk in Lst.chunks(results, 7)) Fmt.print("$5d", chunk) |
for (chunk in Lst.chunks(results, 7)) Fmt.print("$5d", chunk) |
||
System.print("\nFound %(results.count) such numbers.")</ |
System.print("\nFound %(results.count) such numbers.")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 699: | Line 699: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">func Ones(N); \Return count of 1's in N |
||
int N, Count; |
int N, Count; |
||
[Count:= 0; |
[Count:= 0; |
||
Line 719: | Line 719: | ||
Text(0, " such numbers found below 1000. |
Text(0, " such numbers found below 1000. |
||
"); |
"); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
Revision as of 01:59, 28 August 2022
- Task
Find positive decimal integers n in which the digit 1 occurs exactly twice, where n < 1,000.
11l
L(n) 1..999
I String(n).count(‘1’) == 2
print(n, end' ‘ ’)
- 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
Action!
BYTE FUNC OnesCount(INT x)
BYTE c,d
c=0
WHILE x#0
DO
d=x MOD 10
IF d=1 THEN
c==+1
FI
x==/10
OD
RETURN (c)
PROC Main()
INT i
FOR i=0 TO 999
DO
IF OnesCount(i)=2 THEN
PrintI(i) Put(32)
FI
OD
RETURN
- Output:
Screenshot from Atari 8-bit computer
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
Ada
with Ada.Text_IO; use Ada.Text_IO;
procedure Positives_With_1_Twice is
function One_Twice (N : Positive) return Boolean is
NN : Natural := N;
One_Count : Natural := 0;
begin
while NN > 0 loop
if NN mod 10 = 1 then
One_Count := One_Count + 1;
end if;
NN := NN / 10;
end loop;
return One_Count = 2;
end One_Twice;
begin
for N in 1 .. 999 loop
if One_Twice (N) then
Put (N'Image); Put (" ");
end if;
end loop;
New_Line;
end Positives_With_1_Twice;
- 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
ALGOL 68
Generates the numbers. In order to print them in order, a table of double 1 numbers yes/no is generated.
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
- 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
ALGOL W
Generates the numbers and sorts them into order using Sorting algorithms/Quicksort#ALGOL W.
begin % find numbers where the digit 1 occurs twice, up to 999 %
integer double1Count;
integer array double1 ( 1 :: 100 ); % assume there will be at most 100 numbers %
% Quicksorts in-place the array of integers v, from lb to ub - external %
procedure quicksort ( integer array v( * )
; integer value lb, ub
) ; algol "sortingAlgorithms_Quicksort" ;
% increments n by 1 and returns its new value %
integer procedure inc ( integer value result n ) ; begin n := n + 1; n end inc ;
% generate the numbers %
double1Count := 0;
for i := 0 until 9 do begin
if i not = 1 then begin
double1( inc( double1Count ) ) := 110 + i;
double1( inc( double1Count ) ) := 101 + ( i * 10 );
double1( inc( double1Count ) ) := ( i * 100 ) + 11
end if_i_ne_1
end for_i ;
% sort the numbers %
quickSort( double1, 1, double1Count );
% print the numbers %
for i := 1 until double1Count do writeon( i_w := 1, s_w := 1, double1( i ) );
write();
write( i_w := 1, s_w := 0, "Found ", double1Count, " numbers" )
end.
- 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 Found 27 numbers
AWK
# syntax: GAWK -f NUMBERS_N_IN_WHICH_NUMBER_1_OCCUR_TWICE.AWK
BEGIN {
start = 1
stop = 999
for (i=start; i<=stop; i++) {
if (gsub(/1/,"&",i) == 2) {
printf("%4d%1s",i,++count%10?"":"\n")
}
}
printf("\nNumber 1 occurs twice %d-%d: %d\n",start,stop,count)
exit(0)
}
- 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 Number 1 occurs twice 1-999: 27
Delphi
See Pascal
F#
// 3 digit numbers with 2 ones. Nigel Galloway: July 6th., 2021
[0;2;3;4;5;6;7;8;9]|>List.collect(fun g->[[g;1;1];[1;g;1];[1;1;g]])|>List.iter(fun(n::g::l::_)->printf "%d " (n*100+g*10+l)); printfn ""
- Output:
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
Factor
USING: io math math.functions prettyprint sequences
sequences.extras ;
{ 0 2 3 4 5 6 7 8 9 }
[| n | { { n 1 1 } { 1 n 1 } { 1 1 n } } ] map-concat
[ <reversed> 0 [ 10^ * + ] reduce-index pprint bl ] each nl
- Output:
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
FreeBASIC
function numdig(byval n as integer, d as const integer) as uinteger
'counts the number of occurrences of digit d in the number n
dim as uinteger m = 0
while n
if n mod 10 = d then m+=1
n\=10
wend
return m
end function
for i as uinteger = 1 to 999
if numdig(i, 1) = 2 then print i;" ";
next i
print
- 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
Free Pascal
See Pascal
Go
package main
import (
"fmt"
"rcu"
)
func main() {
fmt.Println("Decimal numbers under 1,000 whose digits include two 1's:")
var results []int
for i := 11; i <= 911; i++ {
digits := rcu.Digits(i, 10)
count := 0
for _, d := range digits {
if d == 1 {
count++
}
}
if count == 2 {
results = append(results, i)
}
}
for i, n := range results {
fmt.Printf("%5d", n)
if (i+1)%7 == 0 {
fmt.Println()
}
}
fmt.Println("\n\nFound", len(results), "such numbers.")
}
- Output:
Decimal numbers under 1,000 whose digits include two 1's: 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 such numbers.
J
Generating the numbers as the cartesian product of the eligible digits (and sorting the result, for "prettiness"):
~./:~10 #. >,{~.(2 1#1;i.10){~(! A.&i. ])3
11 101 110 111 112 113 114 115 116 117 118 119 121 131 141 151 161 171 181 191 211 311 411 511 611 711 811 911
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`
def count(s): reduce s as $x (0; .+1);
range(1;1000)
| select( tostring | explode | 2 == count( select(.[] == 49))) # "1"
Using `countEquals`
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"
- 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
Julia
totalddigisn(x, d = 1, n = 2; base=10) = count(j -> j == d, digits(x; base)) == n
println(filter(totalddigisn, 1:1000))
- 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]
Mathematica / Wolfram Language
sol = Cases[Range[999], _?(Count[IntegerDigits@#, 1] == 2 &)];
Partition[sol, Max[FactorInteger[Length@sol][[All, 1]]]] // TableForm
- 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
Ksh
#!/bin/ksh
# Positive decimal integers with the digit 1 occurring twice
# # Variables:
#
integer MAX=999
# # Functions:
#
######
# main #
######
for ((i=10; i<MAX; i++)); do
[[ ${i} == *{2}(1)* ]] && printf "%d " ${i}
done
echo
- Output:
11 110 111 112 113 114 115 116 117 118 119 211 311 411 511 611 711 811 911
MiniZinc
%Permutations with some identical elements. Nigel Galloway: July 6th., 2021
include "count.mzn";
array [1..3] of var 0..9: N; constraint count(N,1,2);
output [show((N[1]*100)+(N[2]*10)+N[3])]
- Output:
110 ---------- 101 ---------- 11 ---------- 211 ---------- 311 ---------- 411 ---------- 511 ---------- 611 ---------- 711 ---------- 811 ---------- 911 ---------- 121 ---------- 131 ---------- 141 ---------- 151 ---------- 161 ---------- 171 ---------- 181 ---------- 191 ---------- 112 ---------- 113 ---------- 114 ---------- 115 ---------- 116 ---------- 117 ---------- 118 ---------- 119 ---------- ========== Finished in 206msec
Nim
import sugar, strutils
let result = collect(newSeq):
for n in 1..<1000:
if count($n, '1') == 2: n
echo "Found ", result.len, " numbers:"
echo result.join(" ")
- Output:
Found 27 numbers: 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
Pascal
program positiveDecimalIntegersWithTheDigit1occurringExactlyTwice(output);
var
n: integer;
begin
for n := 1 to 999 do
begin
if ord(n mod 10 = 1) + ord(n mod 100 div 10 = 1) + ord(n div 100 = 1) = 2 then
begin
writeLn(n)
end
end
end.
Perl
#!/usr/bin/perl
use strict; # https://rosettacode.org/wiki/Numbers_n_in_which_number_1_occur_twice
use warnings;
my @twoones = grep tr/1// =~ 2, 1 .. 1000;
print "@twoones\n" =~ s/.{60}\K /\n/gr;
- 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(string s) return length(find_all('1',s))=2 end function sequence res = filter(apply(tagset(999),sprint),two_ones) printf(1,"%d found:\n %s\n",{length(res),join_by(res,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
Pike
int main() {
int limit = 1000;
for(int i = 0; i < limit + 1; i++) {
if(String.count((string)i, "1") == 2) {
write((string)i + " ");
}
}
write("\n");
return 0;
}
- 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
Python
#Aamrun, 5th October 2021
from itertools import permutations
for i in range(0,10):
if i!=1:
baseList = [1,1]
baseList.append(i)
[print(int(''.join(map(str,j)))) for j in sorted(set(permutations(baseList)))]
- Output:
11 101 110 112 121 211 113 131 311 114 141 411 115 151 511 116 161 611 117 171 711 118 181 811 119 191 911
Raku
say display 10, '%3d', ^1000 .grep: { .comb.Bag{'1'} == 2 };
sub display {
cache $^c;
"{+$c} matching:\n" ~ $c.batch($^a)».fmt($^b).join: "\n"
}
- Yields:
27 matching: 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
REXX
version 1
Programming note: the three filters (marked below) could've been incorporated into one REXX statement if code golf is the desired goal.
/*REXX program finds positive decimal integers which contain exactly two ones (1s). */
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
w= 10 /*width of a number in any column. */
title= ' positive decimal integers which contain exactly two ones (1s) which are <' hi
say ' index │'center(title, 1 + cols*(w+1) )
say '───────┼'center("" , 1 + cols*(w+1), '─')
found= 0; idx= 1 /*initialize # integers and the index. */
$= /*a list of integers found (so far). */
do j=1 for hi-1 /*find positive integers within range. */
p= pos(1, j); if p==0 then iterate /*integer doesn't have a one (1)? Skip.*/ /* ◄■■■■■■■ a filter.*/
p= pos(1, j, p+1); if p==0 then iterate /* " " " a 2nd one? " */ /* ◄■■■■■■■ a filter.*/
p= pos(1, j, p+1); if p>0 then iterate /* " does " a 3rd one? " */ /* ◄■■■■■■■ a filter.*/
found= found + 1 /*bump the number of integers found. */
$= $ right(j, w) /*add an integer to the ──► $ list. */
if found//cols\==0 then iterate /*have we populated a line of output? */
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
idx= idx + cols /*bump the index count for the output*/
end /*j*/
/*stick a fork in it, we're all done. */
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
say '───────┴'center("" , 1 + cols*(w+1), '─')
say
say 'Found ' found title
- output when using the default inputs:
index │ positive decimal integers which contain exactly two ones (1s) which are < 1000 ───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── 1 │ 11 101 110 112 113 114 115 116 117 118 11 │ 119 121 131 141 151 161 171 181 191 211 21 │ 311 411 511 611 711 811 911 ───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── Found 27 positive decimal integers which contain exactly two ones (1s) which are < 1000
version 2
Programming note: not all REXXes have the countstr BIF.
/*REXX program finds positive decimal integers which contain exactly two ones (1s). */
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
w= 10 /*width of a number in any column. */
title= ' positive decimal integers which contain exactly two ones (1s) which are <' hi
say ' index │'center(title, 1 + cols*(w+1) )
say '───────┼'center("" , 1 + cols*(w+1), '─')
found= 0; idx= 1 /*initialize # integers and the index. */
$= /*a list of integers found (so far). */
do j=1 for hi-1 /*find positive integers within range. */
if countstr(1, j)\==2 then iterate /*Doesn't have exactly 2 one's? Skip. */ /* ◄■■■■■■■ a filter.*/
found= found + 1 /*bump the number of integers found. */
$= $ right(j, w) /*add an integer to the ──► $ list. */
if found//cols\==0 then iterate /*have we populated a line of output? */
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
idx= idx + cols /*bump the index count for the output*/
end /*j*/
/*stick a fork in it, we're all done. */
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
say '───────┴'center("" , 1 + cols*(w+1), '─')
say
say 'Found ' found title
- output is identical to the 1st REXX version.
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++
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++
cstring = substr(cstring,substr(cstring,dstring)+len(string(sum)))
end
return sum
- 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...
Sidef
say (1..1000 -> grep { .digits.count { _ == 1 } == 2 })
- 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]
Wren
import "/math" for Int
import "/seq" for Lst
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
for (chunk in Lst.chunks(results, 7)) Fmt.print("$5d", chunk)
System.print("\nFound %(results.count) such numbers.")
- Output:
Decimal numbers under 1,000 whose digits include two 1's: 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 such numbers.
XPL0
func Ones(N); \Return count of 1's in N
int N, Count;
[Count:= 0;
repeat N:= N/10;
if rem(0) = 1 then Count:= Count+1;
until N = 0;
return Count;
];
int N, Count;
[for N:= 1 to 1000-1 do
if Ones(N) = 2 then
[IntOut(0, N);
Count:= Count+1;
if rem(Count/10) = 0 then CrLf(0) else ChOut(0, 9\tab\);
];
CrLf(0);
IntOut(0, Count);
Text(0, " such numbers found below 1000.
");
]
- 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 27 such numbers found below 1000.