0

I want to calculate AND of numbers from 0 to (n)^{1/2} - 1 with each of the numbers from 0 to (n)^{1/2} - 1. I want to do this in O(n) time and can't use the XOR, OR, AND operations.

Specifically, can I calculate X+1 AND Y if I know X AND Y?

P.S. - RAM model is being assumed here and operations (add, multiply, divide) on < log(n) bit numbers can be done is constant time.

7
  • 1
    Very similar to stackoverflow.com/questions/5056949/algorithm-to-calculate-xor but not actually a duplicate. Commented Feb 20, 2011 at 13:15
  • 6
    i love the smell of homework in the morning. Commented Feb 20, 2011 at 13:18
  • Are we allowed to use a lookup table? How big can it be? Commented Feb 20, 2011 at 13:24
  • @oil charlesworth Yeah. O(n) sized table is allowed. But building it should be possible in O(n) time Commented Feb 20, 2011 at 13:26
  • @Nitin: So, "no" then! What you've just described is no lookup table and O(n) storage complexity! Commented Feb 20, 2011 at 13:28

1 Answer 1

2

Yes.

Start with a [1x1] grid:

H(-1) = [ 0 ]

Then apply the recursion:

H(i) = [ H(i-1)  H(i-1)
         H(i-1)  H(i-1)+(1 << i) ]

where that denotes matrix concatenation. i.e. each recursion doubles the size of the grid in each dimension. Repeat until you achieve the required size.

Sign up to request clarification or add additional context in comments.

4 Comments

Is the determinant of matrix our answer according to you?
@Nitin: No, the whole matrix is the answer! You were after a 2D array of results, right?
ohk ..so H(i-1)+(1 << i) means H(i-1)+ 1 shifted to left by i X Identity matrix?
@Nitin: Yes, I guess my notation wasn't amazingly clear. But that's what I meant!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.