>>11
Here, X is the base, but it only finds the strings with M non-zero digits. It could be made more complicated to allow the number of each kind of digit to be selected. ((M←1)=+/0≠A)⌿A←⍉(N/X)⊤⍳(X←3)*N←3
0 0 1
0 0 2
0 1 0
0 2 0
1 0 0
2 0 0
Name:
Anonymous2012-01-19 22:11
... is it just (the above) multiplied by (base - 1) to the power of (n - m) ?
>>13 ((M←1)=+/0≠A)⌿A←⍉(N/X)⊤⍳(X←3)*N←3
1. Store 3 into N (number).
2. Store 3 into X (base).
3. Raise X to the Nth power (call this ⍵).
4. Create a vector from 0 to ⍵.
5. Duplicate X into an N-length vector (call this ⍺).
6. Convert each number in ⍵ into a base X number using ⍺ (call this ⍵).
7. Transpose matrix ⍵ and store it into A.
8. Store 1 into M.
9. Set a 0 for all 0 digits or a 1 for all non-zero digits in A.
10. Sum this to create a vector (call this ⍵).
11. Place a 1 if the element of ⍵ is equal to M or 0 otherwise (call this ⍺).
12. Select (gather) on the first axis all elements in A for which the corresponding element of ⍺ is 1 (the result).
This just generates all permutations of a vector then takes the nub of that.
(i.x) 0 1 2 3 ... x-1
(i.x)<y 1 1 ... 1 (y times) 0 0 ... 0 (x times)
(i.!x) 0 1 2 3 ... fact(x)-1
a A. b a'th permutation of b
~. a nub of elements
Name:
Anonymous2012-01-20 22:47
>>1
Its a basic constraint problem, readily solved with recursion. You should do your own homework, so I'll give you pascal code run through Google Translate.
プログラムBinShit;
(* U MENA PASKAL*)
手順nbin(N、M:整数、tは文字列);
開始
もしN> 0
開始
もしM>0の場合、nbin(N - 1、M - 1、'1'+ t)は;
nbin(N - 1、M、'0'+ t)は
最後に
他
の場合はm =0の場合、writelnの(T)
終わり;
three lines in prolog, yah!
GNU Prolog 1.3.0
By Daniel Diaz
Copyright (C) 1999-2007 Daniel Diaz
| ?- [user].
compiling user for byte code...
f(0,[]).
f(N,[0|R]) :- f(N,R).
f(N,[1|R]) :- NN is N - 1, f(NN,R).
user compiled, 4 lines read - 704 bytes written, 48521 ms
>>30
The A. operator did almost all the work for me. I'd say that other guy's APL implementations is at least a bit more impressive. The Prolog one is cool, too.