A mysterious stranger approaches you and hands you 1023 lemons and 10 bags. The stranger says, "Arrange these lemons in these bags however you like, but soon you will be required to give a specific number of lemons (by handing over some bag or bags), and you cannot transfer the lemons between bags once you have arranged them all. Good luck!" Then, she walks away.
After you finish arranging the lemons, a friend suddenly approaches you and frantically asks, "Quick! There's no time to explain! I need exactly n lemons!"
Can you fulfill your friend's request for any positive integer n less than or equal to 1023?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Can you explain how it works?
Log in to reply
You start with the number of required lemons and sbtract the largest power of 2 less than this number. You take the bag with this number of lemons. Then, you look at the difference, so how many lemons are missing and again, you look for the largest power od 2 that is less than that. You take this bag, and then continue with the still missing lemons. You repeat this procedure until there are no more lemons missing. Then, you gove your friend all bags that you've selected. This is the same as expressing the required number of lemons in binary and then giving the bags that correspond to the different positions.
Bonus: Is 10 the smallest number of bags to accomplish this task?
Log in to reply
For every bag there are 2 options: Either give it to your friend, or don't. So, for 10 bags there are 2 1 0 = 1 0 2 4 > 1 0 2 3 options and I have shown that they work.
For 9 bags, there are only 2 9 = 5 1 2 options, so you can give at most 512 different numbers of lemons, which is not enough.
In general, the minimum number of bags needed to be able to get every number of lemons between 0 and n is ⌈ lo g 2 ( n + 1 ) ⌉ .
Hi. Will this answer is true for 1024 lemons? If no, explain. You said; "In general, the minimum number of bags needed to be able to get every number of lemons between 0 and n is ceiling function [log (n+1) base 2] " I didn't get this, please explain. Any help will be appreciated. Thank you 😀
Log in to reply
With the method I explained, the largest number you can reach is using a bags is 2 a − 1 . This is because there are 2 a possible ways to give or don't give the bags, but giving 0 bags and therefore 0 lemons is also counted, so you can give at most 2 a − 1 lemons.
Now, if the maximum number is one less than a power of 2, then you can take the log base 2 of this power (which is n + 1 ) to get the number of required bags. If the maximum number of lemons is not a power of 2, even if it's just one more, then you have to take one more bag than for the next smallest power of 2, because otherwise you don't have enough combinations. And this is exactly what the ceiling function does. For one less than a power of 2, ⌈ lo g 2 n ⌉ gives the log of this power and for other integers, it givs the log of the next power of 2.
In other words you would need 11 bags for 1024 lemons ( up to 2047 lemons) as adding one more bag (bit) adds 2^10 (1024) lemons, you can’t add 0.13 bags (bits) so that’s why you ceil the log2,1024 = 10.13 to the next whole integer, i.e 11
Just put them all in one bag. No matter how many they want, they'll have enough :P
Log in to reply
Well, the question says exactly n lenons , however, If you have a lemon counting machine, it might work :D
You can’t re-arrange but you can take it out and give your friend
Log in to reply
Exactly that was what I thought. The tenth bag I’d put extra lemons in. It doesn’t say all the bags have to have equal amounts.
I did it the same way. PS strategy, not strategie
At first I was confused as to why it was 1023 and not 1024, but then realized that 10 bits can represent the numbers from 0-1023,I.e. 1024 NUMBERS as a COUNT of numbers not a sum as it includes the number 0. So abstracting myself from all the binary calculations I thought about the problem as having 10 bags (bits) which can be on and off (I.e give or not give) that can represent any number from 0-1023 (with the most significant having 512 lemons down to the least significant bit (bag) having 1 lemon).
I want 159 lemons How will you give me?
Log in to reply
1 x Bag of 128, 1x bag of 16, 1x bag of 8, 1x bag of 4, 1x bag 2, 1x bag of 1 lemons
@Candice Andrew He can just give you the bags in the 1st, 2nd, 3rd, 4th, 5th, and 8th position. Since they will have 1 + 2 + 4 + 8 + 16 + 128 = 159.
bag of 128 + 16 + 8 + 4 + 2 + 1
I want 5 lemons what do you give me?
Jk bag 1 and 3
As an interesting fact, I knew that it das posible because I learnt to count in binary with my fingers, and it turns out that with N fingers you can count in binary up to 2^N -1. Yes, you can count up to 1023 with two hands in binary. It is even larger if you count in base three.
1 bag will do, unless the word “any” is replaced by “all.”
An easy proof that this always works:
First, notice that [2^n]+[2^(n-1)]+...+[2^1]+1=[2^(n+1)]-1 which is easy with a visual "proof".
Claim: If it works for 2^n, it works for 2^(n+1) ("It works for 2^n" means that for x<=2^n you can hand over x lemons)
Proof: Because it works for 2^n it also works for (2^n)-1=2^(n-1)+...+1=: ( ) But for that you obviously don't need the bag with 2^n lemons. So to count from 2^n onwards, just let him keep that bag and count with the smaller bags all the way to [2^n] + ( ) which is 2^(n+1)-1
So the claim is true.
And as it obviously works for 2^0, it works for all n>=0.
Put 2 n − 1 lemons in the n th bag.
Then the binary representation of the number tells you which bags to hand over! 🙂
If n = 10 then it's binary is 1010, which bags do we have to give? 2nd and 4th?
Log in to reply
Yes, the bag containing 2 and the bag containing 8 lemons.
Yes, that's right! The bags have 1, 2, 4, 8, 16, etc. So, the 2nd bag has 2 lemons, and the 4th bag has 8 lemons, which equals 10 total lemons. Sometimes it is easier to see the bags in reverse order to line up with the binary representation:
512, 256, 128, 64, 32, 16, 8, 4, 2, 1
Now, express a number as binary, such as 947: 1110110011
So the bags are 10 (512), 9 (256), 8 (128), 6 (32), 5 (16), 2 (2), and 1 (1). 512+256+128+32+16+2+1 = 947 lemons.
It always works!
I was thinking about binary too.
Log in to reply
I automatically recognized 1023 being one less that 1024. I supposed powers of 2 would be involved. But I didn't spend enough time on the problem and jumped to the wrong conclusion.
An easy proof that this always works:
First, notice that [2^n]+[2^(n-1)]+...+[2^1]+1=[2^(n+1)]-1 which is easy with a visual "proof".
Claim: If it works for 2^n, it works for 2^(n+1) ("It works for 2^n" means that for x<=2^n you can hand over x lemons)
Proof: Because it works for 2^n it also works for (2^n)-1=2^(n-1)+...+1=: ( ) But for that you obviously don't need the bag with 2^n lemons. So to count from 2^n onwards, just let him keep that bag and count with the smaller bags all the way to [2^n] + ( ) which is 2^(n+1)-1
So the claim is true.
And as it obviously works for 2^0, it works for all n>=0.
while trying to solve the problem, i kept thinking of how binary coding works. this helped me deal with it
Put one lemon in the first bag, then put twice as many lemons that are in the current bag into the next bag and so forth.
To hand over n lemons, check to see whether the number is odd, if so hand over the first bag. Repeatedly move to the next bag, halve the number requested throwing away any remainder, if the resultant is odd, hand over that bag.
Added: 2019 June 25 2325: The first paragraph describes putting ascending power of starting from 2 0 ⇒ 1 . The second paragraph describes a method on implicitly generating the binary form of the requested number in a manner that facilitates the selection of the proper bags (that is, the correct powers of 2) . See [Russian peasant multiplication](https://en.wikipedia.org/wiki/Ancient_Egyptian_multiplication#Russian_peasant_multiplication).
What I did was I used our currency system 1,2,5,10,30,50,100,200,500 and 135 in each bag.
You can give all numbers even odd numbers
Well let's look at this in binary, shall we?
We see that 1023 is ten 1s in binary. This is because 1 0 2 3 = 1 0 2 4 − 1 = 2 1 0 − 1 .
Hence, 1 0 2 3 = ( 1 1 1 1 1 1 1 1 1 1 ) 2 .
And for all numbers less than 1023, we can write them as 1s and 0s, but the number of 1s is always less than ten (in base10, you can think of this as 9999 having a greater digit sum ( 9 + 9 + 9 + 9 = 3 6 ) than al numbers lower than it) eg.
3 2 8 = ( 0 1 0 1 0 0 1 0 0 0 ) 2 ( t h r e e 1 s ) 9 4 = ( 0 0 0 1 0 1 1 1 1 0 ) 2 ( f i v e 1 s ) 7 5 5 = ( 1 0 1 1 1 1 0 0 1 1 ) 2 ( s e v e n 1 s )
Remembering the good ol' place value expansion we learn in 3rd class, we can rewrite them as sums of the power of 2 (since binary is base2) multiplied with the respective digits. This can also be viewed as the digits telling us whether to add a particular power of two or not .
(base 10 analogy : 5 8 2 9 4 = (5th digit from the right) × 1 0 5 − 1 + (4th digit from the right) × 1 0 4 − 1 + (3rd digit from the right) × 1 0 3 − 1 + (2nd digit from the right) × 1 0 2 − 1 + (1st digit from the right) × 1 0 1 − 1 = 5 × 1 0 4 + 8 × 1 0 3 + 2 × 1 0 2 + 9 × 1 0 1 + 4 × 1 0 0 )
1 0 2 3 = 1 × 2 9 + 1 × 2 8 + 1 × 2 7 + … 1 × 2 2 + 1 × 2 1 + 1 × 2 0 3 2 8 = 0 × 2 9 + 0 × 2 8 + 0 × 2 7 + … 0 × 2 2 + 0 × 2 1 + 0 × 2 0 9 4 = 0 × 2 9 + 0 × 2 8 + 0 × 2 7 + … 1 × 2 2 + 1 × 2 1 + 0 × 2 0 7 5 5 = 1 × 2 9 + 0 × 2 8 + 1 × 2 7 + … 0 × 2 2 + 1 × 2 1 + 1 × 2 0
Well now view things in terms of the bags. Just like how we interpreted the binary digit expansion, we can say that the digits tell us whether to give a particular bag or not . And as we can see from the binary representation of 1023, we can fill all the bags, and as seen from the binary representation of a number less than 1023, we only have to give bags that can be filled by 1023 lemons.
Hence we can give any number of lemons, we just have to fill the bags with power of 2, starting from one.
■
As others solutions say, the number of lemons in each bag is just a power of 2. But why?
Having n numbers, you have to sum them in all possible ways in order to obtain all the numbers up to the requested value (1023).
Basically, you have to sum the ways to pick one number, the ways of pick two numbers, ..., the ways of pick n numbers (all of them); picked numbers will be summed and, if they're well chosen, it won't be repetitions.
In combinatorics notation ( 1 n ) + ( 2 n ) + . . . + ( n n ) = k = 1 ∑ n ( k n ) .
Thanks to Tartaglia/Pascal's triangle we know that sum equals to 2 n − 1 . And, since 1 0 2 3 = 2 1 0 − 1 , the answer to the problem question is affermative .
Its a base of 2 since you can either select or deselect each bag, that's only degree of freedom you have. Much more direct explanation.
This better be a good friend, showing up out of nowhere asking for lemons while Im being tested....
Let the Nth bag contain 2^N lemons. Write n in the base 2; whenever a 1 appears in the representation, hand over that bag. Ed Gray
Simple. Binary. Put 1 lemon in the first bag, 2 lemon in the second bag, and so on. When somebody ask you to give n amount of lemon, you figure out what the number is in binary, then grab the lemon from the appropriate bags.
Why was it specified for n to be a positive integer? If you have bags from 2^0 to 2^9 you will be able to fulfil requests for any integer n value, just add to the odd value below n and then add 2^0 (1).
I don't get your point, n will have to be a positive integer, (you can't give negative number of lemons).
10 bags and 1023 solutions. You can’t transfer lemons out of the bags... but you can the crate. So fill the bags with 0 lemons and then fill them up to the required number when asked. Remember... the first request may be for 1023 lemons... so putting any lemon other than perhaps 1 lemon in the first bag makes you unable to give out 1023 lemons if so required.
I think you have got the question wrong, you are asked to arrange the lemons in the bags only, and when asked for n lemons, you should only hand over 1 or more bags, and you cannot do anything else. (I know the situation is not very practical but its just a way to phrase a problem)
Problem Loading...
Note Loading...
Set Loading...
Your strategy is to fill every bag with a distinct power of two number of lemons. You will have distributed
10 bags 2 0 + 2 1 + 2 2 + … + 2 8 + 2 9 = 10 bags 1 + 2 + 4 + … + 2 5 6 + 5 1 2 = 1 0 2 3
lemons, so all lemons will be used.
When your friend asks you for a number of lemons, you have to express this number in binary and give them the bags that correspond to the right powers of 2.
So, it always works.