11
\$\begingroup\$

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?

The answer to the above task is 24.

Task

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
\$\endgroup\$
4
  • \$\begingroup\$ Sandbox \$\endgroup\$
    – math scat
    Feb 10 at 16:23
  • 1
    \$\begingroup\$ What's "infinite" or a "dilemma" about this puzzle? \$\endgroup\$ Feb 13 at 11:34
  • \$\begingroup\$ 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. \$\endgroup\$
    – math scat
    Feb 13 at 12:45
  • \$\begingroup\$ Maybe use "arbitrary" instead? \$\endgroup\$
    – Aos Sidhe
    Feb 13 at 14:41

26 Answers 26

9
\$\begingroup\$

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
\$\endgroup\$
7
\$\begingroup\$

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.
\$\endgroup\$
5
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

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)
\$\endgroup\$
4
\$\begingroup\$

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.
\$\endgroup\$
4
\$\begingroup\$

TI-Basic, 15 13 bytes

Prompt A,B
B/prod(1-ʟA⁻¹
\$\endgroup\$
4
\$\begingroup\$

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

\$\endgroup\$
1
  • 5
    \$\begingroup\$ 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.... \$\endgroup\$ Feb 15 at 9:27
3
\$\begingroup\$

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

\$\endgroup\$
2
  • 1
    \$\begingroup\$ exp(total(ln(list)) perhaps? \$\endgroup\$ Feb 11 at 0:55
  • \$\begingroup\$ @GregMartin Wow that is actually smart, I would never have thought of that. \$\endgroup\$
    – Aiden Chow
    Feb 11 at 5:51
3
\$\begingroup\$

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!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 45 bytes \$\endgroup\$
    – att
    Feb 11 at 22:21
2
\$\begingroup\$

05AB1E, 5 bytes

D</P*

Try it online!

Port of AndrovT's Vyxal answer.

Explanation

D</P*  # Implicit input
D      # Duplicate
 <     # Decrement
  /    # Divide
   P   # Product
    *  # Multiply
       # Implicit output
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
7
  • \$\begingroup\$ @AZTECCO Thank you! It saves 3 bytes, not 4 and I hate x=>y=> :) \$\endgroup\$
    – EzioMercer
    Feb 11 at 12:56
  • \$\begingroup\$ @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 :) \$\endgroup\$
    – EzioMercer
    Feb 12 at 13:18
  • \$\begingroup\$ Oh yes I haven't considered it, that's fine then \$\endgroup\$
    – AZTECCO
    Feb 12 at 18:00
  • \$\begingroup\$ 26 bytes \$\endgroup\$
    – Shaggy
    Feb 13 at 15:38
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Shaggy
    Feb 13 at 16:07
1
\$\begingroup\$

Japt, 8 bytes

*VË/ÓDÃ×

Try it

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 9 bytes

I÷×ηΠθΠ⊖θ

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
I           Implicitly print
\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 40 bytes

x=>n=>n*eval([x.join`*`,...x].join`/~-`)

Try it online!

37 with small error

\$\endgroup\$
3
1
\$\begingroup\$

Perl 5, 33 bytes

sub{$a=pop;$a*=$_/($_-1)for@_;$a}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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)

\$\endgroup\$
1
  • \$\begingroup\$ 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 \$\endgroup\$
    – DLosc
    Feb 17 at 22:42
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

Haskell, 33 31 bytes

-2 bytes thanks to @Lynn

p=product
x#n=p x/p(pred<$>x)*n

Attempt This Online!

\$\endgroup\$
1
  • \$\begingroup\$ (map pred x) can become (pred<$>x) or [k-1|k<-x]. \$\endgroup\$
    – Lynn
    Feb 21 at 20:21
0
\$\begingroup\$

Nibbles, 6.5 6 bytes

/*`*$@`*+-1

Attempt This Online!

/  Divide
*   multiply
`*   product
$     the list
@    the number
`*  product
+    add
-1    -1
      the list [vectorized]
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.