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

m
syntax highlighting fixup automation
m (→‎{{header|J}}: last edit comment should have said "discard duplicates...")
m (syntax highlighting fixup automation)
Line 6:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">L(n) 1..999
I String(n).count(‘1’) == 2
print(n, end' ‘ ’)</langsyntaxhighlight>
 
{{out}}
Line 15:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">BYTE FUNC OnesCount(INT x)
BYTE c,d
 
Line 38:
FI
OD
RETURN</langsyntaxhighlight>
{{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]
Line 46:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
 
procedure Positives_With_1_Twice is
Line 70:
end loop;
New_Line;
end Positives_With_1_Twice;</langsyntaxhighlight>
{{out}}
<pre>
Line 77:
=={{header|ALGOL 68}}==
Generates the numbers. In order to print them in order, a table of double 1 numbers yes/no is generated.
<langsyntaxhighlight 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 #
Line 95:
FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 105:
=={{header|ALGOL W}}==
Generates the numbers and sorts them into order using [[Sorting algorithms/Quicksort#ALGOL W]].
<langsyntaxhighlight lang="algolw">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 %
Line 129:
write();
write( i_w := 1, s_w := 0, "Found ", double1Count, " numbers" )
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 137:
</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f NUMBERS_N_IN_WHICH_NUMBER_1_OCCUR_TWICE.AWK
BEGIN {
Line 150:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 163:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// 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 ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 175:
{{trans|F#}}
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="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</langsyntaxhighlight>
{{out}}
<pre>
Line 187:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight 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
dim as uinteger m = 0
Line 200:
if numdig(i, 1) = 2 then print i;" ";
next i
print</langsyntaxhighlight>
{{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:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 238:
}
fmt.Println("\n\nFound", len(results), "such numbers.")
}</langsyntaxhighlight>
 
{{out}}
Line 253:
=={{header|J}}==
Generating the numbers as the cartesian product of the eligible digits (and sorting the result, for "prettiness"):
<langsyntaxhighlight Jlang="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</langsyntaxhighlight>
 
=={{header|jq}}==
Line 264:
 
'''Using `count`'''
<langsyntaxhighlight lang="jq">def count(s): reduce s as $x (0; .+1);
 
range(1;1000)
| select( tostring | explode | 2 == count( select(.[] == 49))) # "1"</langsyntaxhighlight>
'''Using `countEquals`'''
<langsyntaxhighlight lang="jq">def countEquals($n; s):
label $out
| foreach (s, null) as $x (-1;
Line 280:
range(1;1000)
| select( tostring
| countEquals(2; select(explode[] == 49))) # "1"</langsyntaxhighlight>
{{out}}
<pre>
Line 313:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">totalddigisn(x, d = 1, n = 2; base=10) = count(j -> j == d, digits(x; base)) == n
 
println(filter(totalddigisn, 1:1000))
</langsyntaxhighlight>{{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|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">sol = Cases[Range[999], _?(Count[IntegerDigits@#, 1] == 2 &)];
Partition[sol, Max[FactorInteger[Length@sol][[All, 1]]]] // TableForm</langsyntaxhighlight>
 
{{out}}<pre>
Line 337:
 
=={{header|Ksh}}==
<langsyntaxhighlight lang="ksh">
#!/bin/ksh
 
Line 357:
[[ ${i} == *{2}(1)* ]] && printf "%d " ${i}
done
echo</langsyntaxhighlight>
{{out}}<pre>11 110 111 112 113 114 115 116 117 118 119 211 311 411 511 611 711 811 911
</pre>
 
=={{header|MiniZinc}}==
<syntaxhighlight lang="minizinc">
<lang 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])]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 428:
</pre>
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import sugar, strutils
 
let result = collect(newSeq):
Line 434:
if count($n, '1') == 2: n
echo "Found ", result.len, " numbers:"
echo result.join(" ")</langsyntaxhighlight>
 
{{out}}
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>
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">program positiveDecimalIntegersWithTheDigit1occurringExactlyTwice(output);
var
n: integer;
Line 451:
end
end
end.</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Numbers_n_in_which_number_1_occur_twice
Line 460:
 
my @twoones = grep tr/1// =~ 2, 1 .. 1000;
print "@twoones\n" =~ s/.{60}\K /\n/gr;</langsyntaxhighlight>
{{out}}
<pre>
Line 468:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="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: #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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 482:
 
=={{header|Pike}}==
<langsyntaxhighlight Pikelang="pike">int main() {
int limit = 1000;
 
Line 493:
 
return 0;
}</langsyntaxhighlight>
{{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|Python}}==
<langsyntaxhighlight lang="python">
#Aamrun, 5th October 2021
 
Line 508:
baseList.append(i)
[print(int(''.join(map(str,j)))) for j in sorted(set(permutations(baseList)))]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 541:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>say display 10, '%3d', ^1000 .grep: { .comb.Bag{'1'} == 2 };
 
sub display {
cache $^c;
"{+$c} matching:\n" ~ $c.batch($^a)».fmt($^b).join: "\n"
}</langsyntaxhighlight>
{{out|Yields}}
<pre>27 matching:
Line 556:
=== version 1 ===
Programming note: &nbsp; the three filters (marked below) could've been incorporated into one REXX statement if code golf is the desired goal.
<langsyntaxhighlight lang="rexx">/*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.*/
Line 580:
say '───────┴'center("" , 1 + cols*(w+1), '─')
say
say 'Found ' found title</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 595:
=== version 2 ===
Programming note: &nbsp; &nbsp; not all REXXes have the &nbsp; '''countstr''' &nbsp; BIF.
<langsyntaxhighlight lang="rexx">/*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.*/
Line 617:
say '───────┴'center("" , 1 + cols*(w+1), '─')
say
say 'Found ' found title</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 652:
end
return sum
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 668:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">say (1..1000 -> grep { .digits.count { _ == 1 } == 2 })</langsyntaxhighlight>
{{out}}
<pre>
Line 678:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/seq" for Lst
import "/fmt" for Fmt
Line 685:
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.")</langsyntaxhighlight>
 
{{out}}
Line 699:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func Ones(N); \Return count of 1's in N
int N, Count;
[Count:= 0;
Line 719:
Text(0, " such numbers found below 1000.
");
]</langsyntaxhighlight>
 
{{out}}
10,327

edits