-5
There's a cool magic trick that works using the power of binary. The effect of the trick is as follows:
An audience member chooses some natural number in the range of
1 to x
where x is chosen by the magician.The magician hands the audience member some special cards. Each card contains some numbers from
1 to x
.The audience member selects the cards which contain their number.
Almost instantly, the magician can determine the original number selected.
Specification:
The numbers used for the cards are determined based on binary place value. Each card is first labeled with a power of 2. The first card becomes 1, the second becomes 2, the third becomes 4, and so on.
From now on, I will refer to card n
as the card labeled with n
.
To determine whether a number k
is on card n
, determine whether k in binary has at 1 at place value n. Consider the numbers k=13
and n=4
.
K in binary is 1101
. The second digit (n=4) is 1, so k=13, n=4
is a valid combination.
Goal:
Given two natural numbers 0 < n < 128
and 0 < k < 128
, determine whether n
appears on card k
. Any reasonable input and output is allowed. Standard loopholes are banned.
This is code-golf, so the fewest bytes wins.
4Better add the test cases directly here. – Mr. Xcoder – 2017-08-15T14:19:14.387
2Also, what is *determine whether
k
in binary has at 1 at place valuen
* supposed to mean? Did you mean that the character in the binary representation ofk
at indexn
is1
? – Mr. Xcoder – 2017-08-15T14:22:30.987@Mr.Xcoder Probably "at" should be "a" but not sure. – Erik the Outgolfer – 2017-08-15T14:23:02.310
1Also, what did you mean by *The second digit (n=4)*. Did you refer to the fourth digit? I am not sure I understand. – Mr. Xcoder – 2017-08-15T14:25:35.800
@Mr.Xcoder No, binary place value 4. So like the 4-s digit. – HyperNeutrino – 2017-08-15T14:26:44.000
@HyperNeutrino I don't understand your comment either :P EDIT: I understood. The exponent of
2
such that we reachn
(2^2 = 4
)! – Mr. Xcoder – 2017-08-15T14:27:05.610@Mr.Xcoder You know how we have 1s digits, 10s digits, 100s digits, etc? Same logic applies here – HyperNeutrino – 2017-08-15T14:28:25.423
1Does the truthy/falsy value have to be consistent? – Erik the Outgolfer – 2017-08-15T14:34:27.173
Why did you swap
n
andk
at the end "determine whethern
appears on cardk
"? – Jonathan Allan – 2017-08-15T14:37:48.157It does not seem at all clear if the input is the binary place or the minimum number on the card (most people seem to be assuming the latter) - first you say "From now on, I will refer to card n as the card labeled with n" then immediately you say "determine whether k in binary has at 1 at place value n.". – Jonathan Allan – 2017-08-15T14:41:17.927
Why did you ask a question and put it on hold? Couldn't you simply delete it? – rahnema1 – 2017-08-15T15:27:24.307
@rahnema1 Only moderators can delete answered questions (when the answerers received reputation for it). This is common if you want to abandon the challenge. – Mr. Xcoder – 2017-08-15T15:58:21.797