# Multiplicative undelta

Let us define the "multiplicative deltas" of values $$\[\; a_0, \cdots a_N, \;]\$$ as:

$$[\; a_1 / a_0, \cdots, a_{i+1} / a_i, \cdots, a_N / a_{N-1} \;]$$

The reverse operation - namely "multiplicative undelta" - returns values such that the above operation results in the given values.

### Example

Given values $$\[\; 1, 5, 3, 2 \;]\$$ a general solution to the "multiplicative undelta" operation is:

$$[\; a_0, \quad \underset{a_1}{\underbrace{a_0 \cdot 1}}, \quad \underset{a_2}{\underbrace{a_1 \cdot 5}}, \quad \underset{a_3}{\underbrace{a_2 \cdot 3}}, \quad \underset{a_4}{\underbrace{a_3 \cdot 2}} \;]$$

A particular solution can be obtained by setting $$\a_0\$$ to any value other than zero, for example by setting $$\a_0 := 1 \$$ we would get:

$$[\; 1, 1, 5, 15, 30 \;]$$

## Challenge

Your task for this challenge is to implement the operation "multiplicative undelta" as defined above.

## Rules

Inputs are:

• a non-zero value $$\a_0\$$
• a non-empty list/array/vector/... of non-zero "multiplicative deltas"

Output is a list/array/vector/... of values such that the first element is $$\a_0\$$ and for which the "multiplicative deltas" are the input.

Note: If your language has no support of negative integers you may replace non-zero by positive.

## Test cases

2 [21] -> [2,42]
1 [1,5,3,2] -> [1,1,5,15,30]
-1 [1,5,3,2] -> [-1,-1,-5,-15,-30]
7 [1,-5,3,2] -> [7,7,-35,-105,-210]
2 [-12,3,-17,1311] -> [2,-24,-72,1224,1604664]
-12 [7,-1,-12,4] -> [-12,-84,84,-1008,-4032]
1 [2,2,2,2,2,2,2,2] -> [1,2,4,8,16,32,64,128,256]

• May we take a single list of length 1+number of deltas, where the first item is a₀? – Adám Oct 9 '18 at 8:08
• @Adám: I'm gonna say no to that because it would change things for existing answers. – ბიმო Oct 9 '18 at 9:15

scanl(*)


Try it online!

• I was checking if anyone wrote this, and I thought noone wrote it and I said "cool, then I'll write" and then saw your answer at the bottom of the page. upvoted. – Windmill Cookies Oct 7 '18 at 7:06

# APL(Dyalog), 3 bytes

×\∊


Try it online!

If I have to take the number on the left and the array on the right:

-2 thanks to @H.PWiz

7 5 3 bytes

×\,

• ⊣,⊢ can just be , – H.PWiz Oct 6 '18 at 17:52
• @H.PWiz thanks, my tacit is extremely weak – Quintec Oct 6 '18 at 17:54
• You don't need the parens; ×\, evaluates to a function. – Dennis Oct 6 '18 at 17:54
• @Dennis Doesn't seem to work unfortunately – Quintec Oct 6 '18 at 17:56
• You don't need to count the parens, because they're not needed in some situations. f←×\ works, for example. Btw, the (-12 3 -17 1311) in your permalink should be (¯12 3 ¯17 1311). – Dennis Oct 6 '18 at 18:27

# R, 15 bytes

cumprod(scan())


Try it online!

Full program. Function is longer (unless we were allowed to "glue" the inputs together, so that built-in cumprod would suffice as a complete answer):

# R, 28 bytes

function(i,x)cumprod(c(i,x))


Try it online!

# MATL, 3 bytes

hYp


Try it online!

            #implicit input, x_0 and A
h           #horizontally concatenate
Yp          #cumulative product
#implicit output


The hYpe about MATL is real.

# JavaScript (ES6), 27 bytes

Takes input as (m)(a).

m=>a=>[m,...a.map(x=>m*=x)]


Try it online!

# Japt, 3 bytes

å*V


Try it

## Explanation

        :Implicit input of array U and integer V
å       :Cumulatively reduce U
*      :By multiplication
V     :With an initial value of V


# Python 3, 39 bytes

def f(a,b):
for x in[1]+b:a*=x;yield a


Alternative approach. Returns a generator.

Try it online!

# Jelly, 3 bytes

;×\


Try it online!

# Wolfram Language (Mathematica), 14 bytes

FoldList@Times


Try it online!

FoldList[Times, a0, {x1, x2, ..., xn}] produces the desired output. FoldList[Times] is the curried form that produces a pure function still waiting for its a0 and {x1, x2, ..., xn}.

# Perl 6, 9 bytes

{[\*] @_}


Try it online!

# Standard ML, 32 bytes

fun f(x::r)a=a::f r(a*x)|f&a=[a]


Try it online!

Ungolfed:

fun f (x::xr) a = a :: f xr (a*x)
| f    _    a = [a]


# J, 6 5 bytes

-1 byte thanks to Bubbler

*/\@,


Try it online!

## Explanation:

A diadic verb, the left argument is a0, the rigth one - the vector

     @, - append the vector to a0 and
*/\   - find the running product


## Batch, 69 bytes

@set/pa=
@echo %a%
@for %%d in (%*) do @set/aa*=%%d&call echo %%a%%


Takes input of $$\a_0\$$ on STDIN and the deltas as command-line arguments.

# Common Lisp, 67 bytes

(lambda(a l &aux(y 1))(mapcar(lambda(x)(setf y(* y x)))(cons a l)))


Try it online!

# Husk, 2 bytes

G*


Try it online!

This is equivalent to nimi's answer in Haskell: scanl(*), which means reduce from the left using multiplication, and return all partial results.

# 05AB1E, 5 3 bytes

šηP


-2 bytes thanks to @BMO.

Explanation:

š      # Prepend the (implicit) input-integer at the start of the (implicit) input-list
#  i.e. -12 and [7,-1,-12,4] → ["-12",7,-1,-12,4]
η     # Prefixes of this new list
#  i.e. ["-12",7,-1,-12,4]
#   → [["-12"],["-12",7],["-12",7,-1],["-12",7,-1,-12],["-12",7,-1,-12,4]]
P    # Take the product of each inner list (and output implicitly)
#  i.e. [["-12"],["-12",7],["-12",7,-1],["-12",7,-1,-12],["-12",7,-1,-12,4]]
#   → [-12,-84,84,-1008,-4032]

• No, the s was there to get the other input. :) s is swap, and š is prepending at the start of the list. Either way, thanks for the -2 – Kevin Cruijssen Oct 8 '18 at 14:42

# Pyth, 6 bytes

*FR._s


Test that one here!

Alternatively, 7 bytes:

.u*NYEQ


Test it here!

The first takes input as a tuple, the second takes input as two separate lines.

Thanks to @Sok for helping me with getting good at mapping and saving 1 byte.

• You can save a byte by implementing the map in the second solution as R, as in *FR._s - demonstration – Sok Oct 8 '18 at 10:27
• @Sok Nice catch! I had tried M assuming it'd work, then got a sugar parse failure - mostly because I didn't remember how the parsing for F <pf2> worked. – Steven H. Oct 9 '18 at 1:51
• I'll be honest, I don't really understand how nested M, F, L, and R work, I just tried a bunch until one worked :o) – Sok Oct 9 '18 at 7:23

# Python 2, 40 bytes

f=lambda a,b:[a]+(b and f(a*b[0],b[1:]))


Try it online!

Surprisingly, the move to Python 3 and use of generators there only saves 1 byte over the recursive solution.

# PowerShell, 29 bytes

param($a,$b)$a;$b|%{($a*=$_)}


Try it online!

This assumes just outputting the values is fine.

> .\scratch.ps1 1 (1,5,3,2)
1
1
5
15
30


If that's not fine, this actually builds the list and then pushes it to toString which prints the same way.

param($a,$b)$c=,$a;$b|%{$c+=$_*$c[-1]};\$c #41 bytes

• Rules are: Output is a list/array/vector/. So, the first is fine. – mazzy Oct 12 '18 at 16:00

# MathGolf, 6 5 bytes

\{\o*


Try it online!

I think this could be 5 bytes (\{\o*) but \ instruction seems a little off when dealing with input. This is now fixed in the latest version.

### Explanation:

\       Swap arguments, pushing both to stack
{      Foreach loop over second argument
\o    Output counter with newline
*   Multiply counter by current element
Implicitly output the last element

• I have redesigned the handling of  with implicit input. It still handles items on the stack the same way, but now it pops two elements from the input onto the stack instead of just one. I think it could be useful for this challenge. – maxb Nov 27 '18 at 11:42

# Python 2, 47 bytes

lambda n,A:reduce(lambda a,c:a+[a[-1]*c],A,[n])


Try it online!

• It may be of interest to you that a python solution is shorter in python 3 using generators – Quintec Oct 7 '18 at 23:34

# Charcoal, 12 bytes

ＩＥ⁺⟦Ｎ⟧ＡΠ⊞Ｏυι


Try it online! Link is to verbose version of code. Explanation:

    Ｎ           Input a₀
⟦ ⟧          Wrap in a list
Ａ         Input deltas as a list
⁺              Concatenate lists
Ｅ              Map over elements
ι    Current element
υ     Predefined empty list variable
⊞Ｏ      Push and return updated list
Π        Product
Ｉ               Cast to string
Implicitly print each value on its own line


# K (oK), 9 bytes

{(*\)x,y}


Try it online!

Joins the first number to the second input as a list, and then returns successive results of multiplication

Test Cases

Enter your input after the function like below and then run, as I'm unsure how to use input properly for this language in TiO

{(*\)x,y}[1;1 5 3 2]


# dc, 13 bytes

p[*pz1<A]dsAx


Try it online!

p[*pz1<A]dsAx
p               # Print the first element
*p            # Multiply top elements on the stack
[  z1<A]dsAx   # until only one element is left