# Sequential Multiplication

Your objective is to write a program that takes an input, and, when chained together `N` times, performs "sequential multiplication". What is sequential multiplication, you may ask? It's a sequence with a seed `a` defined as thus:

``````f(0) = a
f(n+1) = f(n)*(f(n)-1)
``````

So, let `a = 5`. Thus, `f(0) = 5`, `f(1) = f(0)*(f(0)-1) = 5*4 = 20`, and `f(2) = f(1)*(f(1)-1) = 20*19 = 380`.

If your program was `ABC`, then `ABC` should take input `a` and output `f(1)`. Program `ABCABC` should output `f(2)`, etc. Your program series should only take input once and only output once.

This is a code-golf so the shortest program in bytes wins. Standard loopholes are banned.

-

# Jelly, 3 bytes

``````×’\$
``````

Try it online!

### How it works

``````×’\$    Main link (or part thereof). Argument (initially input): n

’     Compute n - 1.
×      Multiply n by (n - 1).
\$    Combine the previous two atoms into a monadic quicklink.
``````

Repeating the snippet n times will execute it n times, resulting in the desired output.

-

## Seriously, 4 bytes

``````,;D*
``````

Explanation:

``````,;D*
,     push input (NOP once input is exhausted)
;D   dupe and push n-1
*  multiply
(implicit output at EOF)
``````

Just like in Dennis's Jelly answer, repeating this program `n` times will result in it running `n` times. That's one of the many advantages of imperative stack-based languages.

Try it online!

-

# MATL, 3 bytes

``````tq*
``````

You can try it online! Basically similar to the Seriously and Jelly answers. First, it duplicates the top of the stack (get's input first time when the stack is empty). Decrements the top of the stack and multiplies the two elements to give the next input, or result.

-

## Python 3, 56 bytes

``````+1if 0else 0
try:n
except:n=int(input())
n*=n-1
print(n)
``````

Just wanted a solution without output-overwriting trickery. The lack of a trailing newline is important.

-
is that first line all 1 number? – Seadrus Feb 19 at 3:29
@Seadrus No, the syntax highlighter isn't matching the Python parser, which stops parsing the numbers when it hits the `if` and `else`. – xnor Feb 19 at 7:15
This would be shorter in Python 2. – mbomb007 Feb 19 at 22:06
@mbomb007 That won't quite work, the print doesn't get handled propery and you get extra output when you concatenate at least once. – FryAmTheEggman Feb 20 at 0:09
Ah, so that's what that extra output was. – mbomb007 Feb 20 at 2:57

# CJam, 5 bytes

``````r~_(*
``````

Try it online!

### How it works

``````r     e# Read a whitespace-separated token from STDIN.
e# This pushes the input (when called for the first time) or an empty string.
~    e# Evaluate.
e# This turns the input into an integer or discards an empty string.
_   e# Copy the top of the stack.
(  e# Decrement.
* e# Multiply.
``````
-

# pl, 5 bytes

``````_▼•=_
``````

Try it online.

Would be 4 bytes if I hadn't been lazy and not implemented "assign to _"...

## Explanation

``````_▼•=_

_       push _ (input var)
▼      decrement last used var (_)
•     multiply, since it is off by one it auto-adds _ to the arg list
=_   assign result to _
``````
-

# 05AB1E, 3 bytes

Code:

``````D<*
``````

Explanation:

``````D    # Duplicate top of the stack, or input when empty
<   # Decrement on top item
*  # Multiply
``````

Try it online!

-

# GolfScript, 5 bytes

``````~.(*`
``````

Try it online.

De-golfed and commented:

``````~    # Eval the input to turn it from a string into a number.
.    # Duplicate the number.
(    # Decrement one of the copies by one.
*    # Multiply the copies together.
`    # Un-eval the number, turning it back into a string.
``````

The GolfScript interpreter automatically reads the input and places it on the stack, but as a string, not as a number. Thus, we need to turn the input into a number with `~`, and stringify it again afterwards with ```. At the end, the interpreter will automatically print out the stringified number on the stack.

(Now, if the challenge had been to iterate `f(n+1) = f(n)*(-f(n)-1)`, I could've done that in 4 bytes with `~.~*`. Figuring out how and why that works is left as an exercise. :)

-

# JavaScript REPL, 25 20 bytes

``````a=prompt();
a*=a-1//
``````

Will work on eliminating a REPL

-
How does this produce output? – Dennis Feb 19 at 2:57
In the console. I need to fix that. – Cᴏɴᴏʀ O'Bʀɪᴇɴ Feb 19 at 2:57
no the console is fine, i consider that valid output for js – Seadrus Feb 19 at 2:58
Oh! Fantastic!! – Cᴏɴᴏʀ O'Bʀɪᴇɴ Feb 19 at 2:58
@Seadrus Apparently per the meta consensus (which I didn't realize was a thing), REPL-based environments are fine so long as it's specified that it's a REPL. – Alex A. Feb 19 at 3:11

# Y, 7 bytes

``````jzC:t*!
``````

Try it here!

This is how it works: `j` takes numeric input. `z` activates implicit printing. `C` begins a new link. `:` duplicates the value on the stack, and `t` decrements it, leaving us with `[a a-1]`. Then, we get `[a*a-a]` from `*`. `!` skips the next command; on EOF, it does nothing. When chained together, it skips the input command, and the process begins again.

-

# Jolf, 6 bytes

Try it here!

``````oj*jwj
``````

## Explanation

``````oj*jwj
oj      set j to
*jwj  j times j-1
implicit output
``````
-

# 𝔼𝕊𝕄𝕚𝕟, 5 chars / 7 bytes

``````ï×‡ï;
``````

Try it here (Firefox only).

Must I truly explain this? Oh well, here goes...

# Explanation

It basically evaluates to `input*=--input;` in JS.

-

# Perl, 23 bytes

``````l;\$_|=<>;\$_*=~-\$_;print
``````

### Alternate version, 10 bytes

``````\$_*=~-\$_;
``````

This requires the `-p` switch. I'm not sure if it is fair game in a question.

-

## Haskell, 14 11 bytes

``````(*)=<<pred\$
``````

Usage example

``````Prelude> (*)=<<pred\$5
20
Prelude> (*)=<<pred\$(*)=<<pred\$5
380
Prelude> (*)=<<pred\$(*)=<<pred\$(*)=<<pred\$5
144020
``````

Maybe this isn't a proper function. If you're nitpicking you can go with `(*)=<<pred\$id` (<-there's a space at the end) for 14 bytes.

Edit: @Zgarb rewrote the function using the function monad and saved 3 bytes. Thanks!

-
`(*)=<<pred\$` saves 3 bytes. Also, it should be noted that this is doesn't actually define a function, and the input value must be placed directly after it. – Zgarb Feb 19 at 16:21
@Zgarb: Thanks! Adding `id` makes it a proper function. I've put a note in my answer. – nimi Feb 19 at 17:01

# Pure bash (no utilities), 40

``````((a=a?a:\$1,a*=a-1))
trap 'echo \$a' exit
``````
-

# TI-Basic, 6 5 bytes

Runs on TI-83/84 calculators

``````:Ans²-Ans
``````

This program works due to the fact that an expression on the last line of a program is printed instead of the regular `Done` text.

-
5 bytes: `:Ans²-Ans` – lirtosiast Feb 20 at 1:28
Thanks, @lirtosiast – Timtech Feb 20 at 14:47

## Lua, 35 18 Bytes

It's something that Lua can do pretty easily for once!

Edit: I've discovered a lot of things in Lua since I did this, so I'm updating it :)

``````print(...*(...-1))
``````

`...` contains the command-line argument unpacked, inlining it will use it's first value in this case as it won't be allowed to expend, resulting in just printing `n*(n-1)`.

-

# Haskell, 72 bytes

This challenge is not friendly to Haskell.. However, the following works if input is in unary and the code is executed in GHCI:

``````(\x->if odd x then let y=until((>x).(10^))(+1)0 in y*(y-1)else x*(x-1))\$
``````

# Explanation:

Unary is always odd, so the first application will convert to decimal. `x*(x-1)` is always even, so otherwise it returns `x*(x-1)`, where `x` is the input. Because Haskell is strongly typed and 'special' symbols cannot be called like `&1`, I believe that this is pretty much the only way to complete this in Haskell, unless one used global variables or an even odder input form.

-

# Mathcad, 39 "bytes"

From user perspective, Mathcad is effectively a 2D whiteboard, with expressions evaluated from left-to-right,top-to-bottom. Mathcad does not support a conventional "text" input, but instead makes use of a combination of text and special keys / toolbar / menu items to insert an expression, text, plot or component. For example, type ":" to enter the definition operator (shown on screen as ":="), "[" to enter an array index, or "ctl-]" to enter a while loop operator (inclusive of placeholders for the controlling condition and one body expression). What you see in the image above is exactly what appears on the user interface and as "typed" in.

For golfing purposes, the "byte" count is the equivalent number of keyboard operations required to enter an expression.

One thing I'm even less sure about (from an "byte" equivalence point of view) is how to count creating a new region (eg, a:=5 or k:=0..n-1). I've equated each move to a new region as being equal to a newline, and hence 1 byte (in practice, I use the mouse to click where I want the region).

I've only included the active statements and not the comments, and I've included 2 bytes each for the a and n inputs but not the values themselves (5 and 7 in the example).

-