I slipped in putting Rogers solution in, so errors. Done correctly it takes
about 80% of my methods running time. Thanks for all the different answers.
I also see how the 'and' works in Rogers solution.
I have been off for a few days or I would have sent this earlier.
Ralph Selfridge
On Tue, 28 Aug 2007, Roger Hui wrote:
and =: 17 b.
xor =: 22 b.
or =: 23 b.
shift=: 33 b.
nc=: 3 : 0
n=. y
t=. n xor n and n-1
b=. t+n
b or _2 shift (b xor n) <[EMAIL PROTECTED] t
)
#: nc^:(i.3!5) 7
0 0 1 1 1
0 1 0 1 1
0 1 1 0 1
0 1 1 1 0
1 0 0 1 1
1 0 1 0 1
1 0 1 1 0
1 1 0 0 1
1 1 0 1 0
1 1 1 0 0
I."1 #: nc^:(i.3!5) 7
2 3 4
1 3 4
1 2 4
1 2 3
0 3 4
0 2 4
0 2 3
0 1 4
0 1 3
0 1 2
(3 comb 5) -: |. I."1 #: nc^:(i.3!5) 7
1
----- Original Message -----
From: Andrew Nikitin <[EMAIL PROTECTED]>
Date: Tuesday, August 28, 2007 6:26
Subject: [Jprogramming] Permutations of a sort.
To: [email protected]
Ralph, I am not sure if you know this trick or not, but if you
start with
interger (n) whose binary representation contains exactly k ones then
{ int t,b;
t=n^(n&(n-1));
b=t+n;
n=b|(((b^n)/t)>>2);
}
generates next larger number with exactly k ones. As long as you
stay within
int size (in your case with 28 elements you do) it is a decent method.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm