X is positive integer.I have to find 3 distinct numbers A, B and C such that 0 ≤ A,B,C < 230 and (A^B)+(B^C)+(C^A)=X where '^' is a bitwise XOR operator
Asked
Active
Viewed 82 times
-1

President James K. Polk
- 40,516
- 21
- 95
- 125

Rishav Founsa
- 1
- 5
-
2any attempt at code? how would you solve the problem by hand? – Shawn Ramirez Apr 09 '22 at 05:05
1 Answers
1
Disclaimer: I'm not an mathematician.
There are several solutions, this finds one of them.
take the X and convert it to binary (e.g 20220408 - yesterday's date)
X=1001101001000100111111000
If the last digit is 1 (X is odd), there is no solution
Discard the last 0 (divide by 2 or shift right)
X=100110100100010011111100
Create 3 binary numbers: start with zeroes and put each 1 bit from X to either a or b or c. It does not matter which one. Just for fun let's do it in a round-robin matter:
a=100000100000000010010000
b=000100000100000001001000
c=000010000000010000100100
Note: if there is only one 1 bit (X is a power of 2), a,b,c won't be distinct, because 2 of them will be zero.
Check the result:
a=int('100000100000000010010000', 2)
b=int('000100000100000001001000', 2)
c=int('000010000000010000100100', 2)
print((a^b)+(b^c)+(c^a)) # 20220408

VPfB
- 14,927
- 6
- 41
- 75