Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re^3: Need a faster way to find matchesby kikuchiyo (Hermit) |
on Jan 17, 2010 at 17:18 UTC ( [id://817881]=note: print w/replies, xml ) | Need Help?? |
If they are all odd numbers, then their LSB is always 1, isn't it? Why don't you just drop that bit then, and test for the bitwise ANDs of the resulting numbers being 0?
This algorithm is still quadratic, though - you still need to do N^2/2 comparisons for an N-sized array. But what if you build an auxiliary hash out of those lsb-shifted-off integers, then for every value simply check whether the bitwise negated value is present among the hash keys? Or something like this. Beware of bugs, as I have not tested the code above, nor have I proved it correct :)
In Section
Seekers of Perl Wisdom
|
|