Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

19 Answers 19

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.

share|improve this answer

Seriously, 4 bytes



,     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!

share|improve this answer

MATL, 3 bytes


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.

share|improve this answer

Python 3, 56 bytes

+1if 0else 0

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

share|improve this answer
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


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.
share|improve this answer

pl, 5 bytes


Try it online.

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



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

05AB1E, 3 bytes




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

Try it online!

share|improve this answer

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

share|improve this answer

JavaScript REPL, 25 20 bytes


Will work on eliminating a REPL

share|improve this answer
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


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.

share|improve this answer

Jolf, 6 bytes

Try it here!



oj      set j to
  *jwj  j times j-1
        implicit output
share|improve this answer

𝔼𝕊𝕄𝕚𝕟, 5 chars / 7 bytes


Try it here (Firefox only).

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


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

share|improve this answer

Perl, 23 bytes


Alternate version, 10 bytes


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

share|improve this answer

Haskell, 14 11 bytes


Usage example

Prelude> (*)=<<pred$5
Prelude> (*)=<<pred$(*)=<<pred$5
Prelude> (*)=<<pred$(*)=<<pred$(*)=<<pred$5

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!

share|improve this answer
(*)=<<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

trap 'echo $a' exit
share|improve this answer

TI-Basic, 6 5 bytes

Runs on TI-83/84 calculators


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.

share|improve this answer
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 :)


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

share|improve this answer

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


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.

share|improve this answer

Mathcad, 39 "bytes"

enter image description here

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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