Positive decimal integers with the digit 1 occurring exactly twice: Difference between revisions
Realize in F# |
Realize in Minizinc |
||
Line 130: | Line 130: | ||
</pre> |
</pre> |
||
=={{header|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])] |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
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 |
|||
</pre> |
|||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
<lang Nim>import sugar, strutils |
<lang Nim>import sugar, strutils |
Revision as of 15:51, 10 July 2021
- Task
Find positive decimal integers n in which the digit 1 occurs exactly twice, where n < 1,000.
ALGOL 68
Generates the numbers. In order to print them in order, a table of double 1 numbers yes/no is generated. <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 # 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</lang>
- 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. <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 % % 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.</lang>
- 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
F#
<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 "" </lang>
- 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
Go
<lang 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.")
}</lang>
- 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.
Julia
<lang julia>totalddigisn(x, d = 1, n = 2; base=10) = count(j -> j == d, digits(x; base)) == n
println(filter(totalddigisn, 1:1000))
</lang>
- 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]
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])] </lang>
- 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
<lang 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(" ")</lang>
- 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
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
Raku
<lang perl6>say display 10, '%3d', ^1000 .grep: { .comb.Bag{'1'} == 2 };
sub display {
cache $^c; "{+$c} matching:\n" ~ $c.batch($^a)».fmt($^b).join: "\n"
}</lang>
- 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. <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.*/ 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</lang>
- 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. <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.*/ 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</lang>
- output is identical to the 1st REXX version.
Ring
<lang 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
</lang>
- 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...
Wren
<lang ecmascript>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.")</lang>
- 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
<lang 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. "); ]</lang>
- 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.