When life gives you lemons

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 n lemons!"

Can you fulfill your friend's request for any positive integer n n less than or equal to 1023?

Yes No

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.

10 solutions

Henry U
Dec 11, 2018

Your strategy is to fill every bag with a distinct power of two number of lemons. You will have distributed

2 0 + 2 1 + 2 2 + + 2 8 + 2 9 10 bags = 1 + 2 + 4 + + 256 + 512 10 bags = 1023 \underbrace {2^0+2^1+2^2+\ldots+2^8+2^9}_{\text{10 bags}} = \underbrace {1+2+4+\ldots+256+512}_{\text{10 bags}} = 1023

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.

Can you explain how it works?

Vasu Vats - 2 years, 5 months ago

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.

Henry U - 2 years, 5 months ago

Bonus: Is 10 the smallest number of bags to accomplish this task?

Brian Lie - 2 years, 5 months ago

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 = 1024 > 1023 2^10 = 1024 > 1023 options and I have shown that they work.

For 9 bags, there are only 2 9 = 512 2^9 = 512 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 n is log 2 ( n + 1 ) \lceil \log_2 (n+1) \rceil .

Henry U - 2 years, 5 months ago

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 😀

25 STYLE - 2 years, 5 months ago

Log in to reply

With the method I explained, the largest number you can reach is using a a bags is 2 a 1 2^a-1 . This is because there are 2 a 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 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 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, log 2 n \lceil \log_2 n \rceil gives the log of this power and for other integers, it givs the log of the next power of 2.

Henry U - 2 years, 5 months ago

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

Lyuboslav Karmidzhanov - 2 years, 5 months ago

Just put them all in one bag. No matter how many they want, they'll have enough :P

Tre Baker - 2 years, 5 months ago

Log in to reply

Well, the question says exactly n n lenons , however, If you have a lemon counting machine, it might work :D

Henry U - 2 years, 5 months ago

You can’t re-arrange but you can take it out and give your friend

Abdulla Ahmed - 2 years, 5 months ago

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.

Jeannie Kessler - 2 years, 5 months ago

I did it the same way. PS strategy, not strategie

Adrian Ma - 2 years, 5 months ago

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).

Lyuboslav Karmidzhanov - 2 years, 5 months ago

I want 159 lemons How will you give me?

Candice Andrew - 2 years, 5 months ago

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

Ark Flash - 2 years, 5 months ago

@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.

Matthew Nelson - 2 years, 5 months ago

bag of 128 + 16 + 8 + 4 + 2 + 1

ll The Enchanted Max ll Lu - 2 years, 5 months ago

I want 5 lemons what do you give me?

Samir Seif - 2 years, 5 months ago

Log in to reply

2^2 + 2^0 = 5 there u have your 5 lemons.

Mk Jain - 2 years, 4 months ago

Jk bag 1 and 3

Samir Seif - 2 years, 5 months ago

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.

Dem Rottensoul - 2 years, 4 months ago

1 bag will do, unless the word “any” is replaced by “all.”

miller mcpherson - 2 years ago

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.

David B - 1 year, 12 months ago
Geoff Pilling
Dec 16, 2018

Put 2 n 1 2^{n-1} lemons in the n 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?

Mr. India - 2 years, 5 months ago

Log in to reply

Yes, the bag containing 2 and the bag containing 8 lemons.

Chris Sparke - 2 years, 5 months ago

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!

Chris Brown - 2 years, 5 months ago

I was thinking about binary too.

The Rational Thinker . - 2 years, 5 months ago

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.

Tim Popely - 2 years, 3 months ago

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.

David B - 1 year, 12 months ago

while trying to solve the problem, i kept thinking of how binary coding works. this helped me deal with it

Dhruva Shah - 1 year, 10 months ago

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 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 2^0\Rightarrow 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

Abraham Ntui - 1 year, 1 month ago
Varad Mahashabde
Dec 22, 2018

Well let's look at this in binary, shall we?

We see that 1023 is ten 1s in binary. This is because 1023 = 1024 1 = 2 10 1 1023 = 1024 - 1 = 2^{10} - 1 .

Hence, 1023 = ( 1111111111 ) 2 1023 = {(1111111111) }_{ 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 = 36 9+9+9+9=36 ) than al numbers lower than it) eg.

328 = ( 0101001000 ) 2 ( t h r e e 1 s ) 94 = ( 0001011110 ) 2 ( f i v e 1 s ) 755 = ( 1011110011 ) 2 ( s e v e n 1 s ) 328={ (0101001000) }_{ 2 }\quad (three\quad 1s)\\ \quad 94={ (0001011110) }_{ 2 }\quad (five\quad 1s)\\ 755={ (1011110011) }_{ 2 }\quad (seven\quad 1s)

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 : 58294 = 58294 = (5th digit from the right) × 10 5 1 + \times { 10 }^{ 5-1 }+ (4th digit from the right) × 10 4 1 + \times { 10 }^{ 4-1 }+ (3rd digit from the right) × 10 3 1 + \times { 10 }^{ 3-1 }+ (2nd digit from the right) × 10 2 1 + \times { 10 }^{ 2-1 }+ (1st digit from the right) × 10 1 1 = 5 × 10 4 + 8 × 10 3 + 2 × 10 2 + 9 × 10 1 + 4 × 10 0 \times { 10 }^{ 1-1 }=5\times{10}^{4}+8\times{10}^{3}+2\times{10}^{2}+9\times{10}^{1}+4\times{10}^{0} )

1023 = 1 × 2 9 + 1 × 2 8 + 1 × 2 7 + 1 × 2 2 + 1 × 2 1 + 1 × 2 0 328 = 0 × 2 9 + 0 × 2 8 + 0 × 2 7 + 0 × 2 2 + 0 × 2 1 + 0 × 2 0 94 = 0 × 2 9 + 0 × 2 8 + 0 × 2 7 + 1 × 2 2 + 1 × 2 1 + 0 × 2 0 755 = 1 × 2 9 + 0 × 2 8 + 1 × 2 7 + 0 × 2 2 + 1 × 2 1 + 1 × 2 0 1023=1\times { 2 }^{ 9 }+1\times { 2 }^{ 8 }+1\times { 2 }^{ 7 }+\dots 1\times { 2 }^{ 2 }+1\times { 2 }^{ 1 }+1\times { 2 }^{ 0 }\\ 328=0\times { 2 }^{ 9 }+0\times { 2 }^{ 8 }+0\times { 2 }^{ 7 }+\dots 0\times { 2 }^{ 2 }+0\times { 2 }^{ 1 }+0\times { 2 }^{ 0 }\\ \quad 94=0\times { 2 }^{ 9 }+0\times { 2 }^{ 8 }+0\times { 2 }^{ 7 }+\dots 1\times { 2 }^{ 2 }+1\times { 2 }^{ 1 }+0\times { 2 }^{ 0 }\\ 755=1\times { 2 }^{ 9 }+0\times { 2 }^{ 8 }+1\times { 2 }^{ 7 }+\dots 0\times { 2 }^{ 2 }+1\times { 2 }^{ 1 }+1\times { 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.

\blacksquare

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 ( n 1 ) + ( n 2 ) + . . . + ( n n ) = k = 1 n ( n k ) {n \choose 1}+{n \choose 2}+...+{n \choose n} = \displaystyle \sum_{k=1}^n {n \choose k} .

Thanks to Tartaglia/Pascal's triangle we know that sum equals to 2 n 1 2^n-1 . And, since 1023 = 2 10 1 1023=2^{10}-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.

Tim Pearce - 2 years, 5 months ago
Matthew Huckabey
Dec 19, 2018

This better be a good friend, showing up out of nowhere asking for lemons while Im being tested....

Edwin Gray
Dec 17, 2018

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.

Roham Jarah
Dec 20, 2018

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 n will have to be a positive integer, (you can't give negative number of lemons).

Parth Sankhe - 2 years, 5 months ago
David Bartel
Dec 17, 2018

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 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)

Parth Sankhe - 2 years, 5 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...