Arbitrary Apple Dilemma

I have encountered this type of puzzle, that usually involves apples, several times. It goes as follows:

There is a certain number of apples in a basket. Ava takes a third of the apples. Bob takes a quarter of the apples that are left. Finally, Dennis takes one-sixth of the apples left. In the end, there are 10 leftover apples. How many apples were in the basket?

Given a list of positive integers and an integer representing the apples left in the end, return the number of apples originally in the basket.

Specs

• The $$\n\$$th element of the list represents the $$\\frac 1 k\$$ share of apples the $$\n\$$th person took in the apples left over.
• There will always exist one integer solution, but the step calculations might result in rational numbers
• This is , so the shortest answer wins.

Test cases

[3, 4, 6], 10    -> 24
[2], 14          -> 28
[6], 30          -> 36
[4, 3], 20       -> 40
[5, 8, 7], 9     -> 15
[2, 9, 4, 8], 7  -> 24

• Sandbox Commented Feb 10, 2023 at 16:23
• Good question; I probably came to the conclusion to call it infinite because the list has a variable length, I used dilemma as a synonym for puzzle. Honestly, I have no idea why. Commented Feb 13, 2023 at 12:45
• Maybe use "arbitrary" instead? Commented Feb 13, 2023 at 14:41

Vyxal, 5 4 bytes

Ė⌐Π/


Try it Online!

Based on @Sean's Raku answer. I feel stupid for not seeing this 4 byte solution sooner.

Ė     reciprocal (1/x)
⌐    complement (1-x)
Π   product
/  divide


Raku, 23 bytes

{$^n/[*] 1 X-(1 X/@^a)}  Try it online! • The number of apples is passed as the parameter $^n, and the list of integers as the parameter @^a.
• 1 X/ @^a produces the list of reciprocals of the integers.
• 1 X- (1 X/ @^a) subtracts each reciprocal from 1.
• [*] multiples together all of those fractions.

R, 21 bytes

\(x,n)prod(x/(x-1),n)


Attempt This Online!

Uses the fact that $$\\frac{1}{1-\frac{1}{x}} = \frac{x}{x-1}\$$ and then multiplies everything together using prod.

Wolfram Language (Mathematica), 17 16 bytes

##&@@(#/--+#)#2&


Try it online!

        --+#    {3,4,6}-1
#/        {3,4,6}/{2,3,5}
##&@@(      )   Sequence[3/2,4/3,6/5]
#2 Sequence[3/2,4/3,6/5]*10
Times[3/2,4/3,6/5,10] (=24)


J, 8 bytes

%[:*/1-%


Try it online!

Takes the number of remaining apples as left argument and the list as right argument.

• The first % (division) forms a dyadic hook, and divides the left argument by the result of applying [:*/1-% to the right argument (monadically).
• The second % takes the reciprocals of the numbers in the list.
• 1 - subtracts each reciprocal from 1.
• */ takes the product; a cap ([:) is used to do so monadically.

TI-Basic, 15 13 bytes

Prompt A,B
B/prod(1-ʟA⁻¹


MATL, 5 bytes

tq/hp


Try it out at MATL Online

        % Implicitly retrieve the first input, an array
t       % Duplicate the first input (x)
q       % Subtract 1 (x - 1)
/       % Divide (element-wise) the two values on the stack (x / (x - 1))
h       % Implicitly retrieve the second input and append to the array
p       % Compute the product of all elements
% Implicitly display the result


• Hello Suever! :) Interestingly, "tq" is a common abbreviation for "te quiero" (I love you) in Spanish! More interestingly, "hp" is also a common abbreviation in Spanish, but I may refrain pointing out what it refers to.... Commented Feb 15, 2023 at 9:27

Desmos, 39 28 bytes

-11 bytes thanks to @Greg Martin with an amazing insight!

f(l,k)=k/e^{ln(1-1/l).total}


Port of most of the other answers here. Taking the product of a list in Desmos is super annoying; there should be an equivalent of total for products lol. Thanks to Greg Martin, total can be used in a very creative way to take the product of a list!

Try It On Desmos!

Try It On Desmos! - Prettified

• exp(total(ln(list)) perhaps? Commented Feb 11, 2023 at 0:55
• @GregMartin Wow that is actually smart, I would never have thought of that. Commented Feb 11, 2023 at 5:51

Python, 45 bytes

-9 bytes thanks to att

Avoids the use of math.prod; utilises the walrus := operator instead.

lambda l,n:round([n:=n*x/~-x for x in l][-1])


Attempt This Online!

• 45 bytes
– att
Commented Feb 11, 2023 at 22:21

05AB1E, 5 bytes

D</P*


Try it online!

Explanation

D</P*  # Implicit input
D      # Duplicate
<     # Decrement
/    # Divide
P   # Product
*  # Multiply
# Implicit output


Wolfram Language (Mathematica), 18 bytes

#2/Times@@(1-1/#)&


Try it online!

Unnamed function that takes the list as its first input and the final number of applies as its second input. Same formula as most other answers here. One trivium: unless I'm overlooking some possibility, it's shorter to use Times here than its abbreviation 1##& because the precedence of / would require extra parentheses in the second case.

Arturo, 29 24 bytes

$=>[&*∏map&'x->x//x-1]  Try it This answer assumes potential floating point issues are okay. Let me know if that is a bad assumption. This uses a new feature in 0.9.82 that was released today. Function arguments no longer need to be named. The first instance of & is the first argument and the second instance of & is the second. Python NumPy, 35 bytes lambda l,n:n*l.prod()//(l-1).prod()  Attempt This Online! Takes a numpy vector and a number. A bit uninspired. On the plus side: has no accuracy issues. Jelly, 5 bytes İCP÷@  Try it online! Port of Sean's Raku answer. Explanation İCP÷@ # Implicit input İ # Reciprocal (1 / z) C # Complement (1 - z) P # Product ÷@ # Divide # Implicit output  K (ngn/k), 10 bytes {x+x%y-1}/  Try it online! • {...}/ set up an implicitly seeded reduce, seeded with x (the number of remaining apples) and run over y (the list of integers) • x%y-1 divide x by y minus one • x+ add this to the above, and pass into the next iteration of the reduce An alternative answer that is also 10 bytes is: {x%/1-1%y}  • 1-1%y subtract the reciprocals of y from one • x%/ beginning with x, do a divide-reduce over the above JavaScript, 27 bytes (x,n)=>x.map(c=>n*=c/~-c)|n // 27 (x,n)=>x.map(c=>n+=n/~-c)|n // 27  The first formula is obvious: if we get $$\\frac{1}{x}\$$ so there left $$\\frac{x-1}{x}\$$ and we need to divide reminder to $$\\frac{x-1}{x}\$$ and it is the same to multiply reminder to $$\\frac{x}{x-1}\$$ The second formula is the same as first just in another format: $$\ n * \frac{x}{x - 1} = n * (\frac{x - 1 + 1}{x - 1}) = n * (1 + \frac{1}{x - 1}) = n + \frac{n}{x-1} \$$ Try it: f1=(x,n)=>x.map(c=>n*=c/~-c)|n ;f2=(x,n)=>x.map(c=>n+=n/~-c)|n ;[ [[3, 4, 6], 10], // 24 [[2], 14], // 28 [[6], 30], // 36 [[4, 3], 20], // 40 [[5, 8, 7], 9], // 15 [[2, 9, 4, 8], 7], // 24 ].map(([a,b]) => console.log(f1(a,b), f2(a,b))) UPD 31 -> 29 Thanks to AZTECCO for the tip to reduce bytes count UPD 29 -> 27 Thanks to Shaggy for the tip to reduce bytes count • @AZTECCO Thank you! It saves 3 bytes, not 4 and I hate x=>y=> :) Commented Feb 11, 2023 at 12:56 • @AZTECCO Yes, if I write x=>y instead of (x,y) it will be 28 bytes :) I just hate to call function like f()() so I will not write x=>y. If you really want you can write your answer with 28 bytes, I don't mind :) Commented Feb 12, 2023 at 13:18 • Oh yes I haven't considered it, that's fine then Commented Feb 12, 2023 at 18:00 • 26 bytes Commented Feb 13, 2023 at 15:38 • It doesn't; that's the point! ;) | is a bitwise operator so, essentially, it tries to convert the array to an integer, which it can't. Commented Feb 13, 2023 at 16:07 Japt, 8 bytes *VË/ÓDÃ×  Try it Factor + math.unicode, 24 21 bytes [ dup 1 v-n v/ Π * ]  Try it online! Port of @pajonk's R answer.  ! 10 { 3 4 6 } dup ! 10 { 3 4 6 } { 3 4 6 } 1 ! 10 { 3 4 6 } { 3 4 6 } 1 v-n ! 10 { 3 4 6 } { 2 3 5 } v/ ! 10 { 1+1/2 1+1/3 1+1/5 } Π ! 10 2+2/5 (product) * ! 24  Charcoal, 9 bytes Ｉ÷×ηΠθΠ⊖θ  Try it online! Link is to verbose version of code. Explanation: Variant of @pajonk's answer that doesn't suffer from floating-point errors.  η Second input × Multiplied by θ First input Π Product ÷ Integer divided by θ First input ⊖ Vectorised decrement Π Product Ｉ Implicitly print  JavaScript (Node.js), 40 bytes x=>n=>n*eval([x.join*,...x].join/~-)  Try it online! 37 with small error • 38 with error – l4m2 Commented Feb 10, 2023 at 23:45 • 40 with currying x=>n=> Commented Feb 11, 2023 at 4:33 • @Samathingamajig I know just couldn't find a good way to write test :) – l4m2 Commented Feb 11, 2023 at 5:28 Perl 5, 33 bytes sub{$a=pop;$a*=$_/($_-1)for@_;$a}


Try it online!

F#, 39 bytes

let i=Seq.foldBack(fun n c->c*n/(n-1.))


Try it online!

A partial-application solution. The function i takes two parameters and applies them directly to Seq.foldBack - the source, and the initial state. In this case, the source is the collection of divisions made on the apples (eg. [4, 3]), and the initial state is the remaining apples (eg. 20).

Seq.foldBack threads an accumulator through the collection, starting with an initial state, then applying the function specified using the next value in the sequence to that state (in this case, calculating the apple count from the next division), and so on until the sequence is finished. Because it's Seq.foldBack, it goes through the collection in reverse order. It returns the result of the accumulator: the initial number of apples.

For those who are curious, the reason for the 1. instead of just plain 1 is that F# is very very strongly typed. You cannot subtract an integer from an float. Both have to be the same type. 1 is an integer, and 1. is a float.

Pip, 18 9 bytes

q*$*Yg/Dg  Attempt This Online! Takes the list on the command line and the number on STDIN. -9 thanks to @DLosc Explanation q*$*Yg/Dg  # Implicit input
q          # The number
*         # multiplied by
$*Y # the product of g/ # the list divided by Dg # the list minus one # Implicit output  Old: Fig/BN-g{o:o*i}o*q # Implicit input Fi # for i in ... g/ # ... the list divided by ... BN-g # ... the list minus one ... { } # ... do: o: # set o to ... o*i # ... o times i o*q # output o times the number  (o is automatically initialised to 1) • You can take a product by folding on multiplication using the $ fold meta-operator: $*a is the product of a. In this case, it's going to be a bit inconvenient because $* has higher precedence than binary /, but this tip can help. Also, BN-g is a clever way to get (g-1); but in this case, since you don't use g again, you can just decrement it with Dg. 9 bytes: q*$*Yg/Dg Commented Feb 17, 2023 at 22:42 Haskell, 22 bytes foldr(\k a->k*a/(k-1))  Accepts the number, and then the list: foldr(\k a->k*a/(k-1)) 10 [3, 4, 6] is 24. Attempt This Online! Haskell, 33 31 bytes -2 bytes thanks to @Lynn p=product x#n=p x/p(pred<$>x)*n


Attempt This Online!

• (map pred x) can become (pred<$>x) or [k-1|k<-x]. – lynn Commented Feb 21, 2023 at 20:21 Nibbles, 6.5 6 bytes /**$@*+-1


Attempt This Online!

/  Divide
*   multiply
*   product
\$     the list
@    the number
*  product