13
\$\begingroup\$

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.

\$\endgroup\$

21 Answers 21

13
\$\begingroup\$

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.

\$\endgroup\$
0
4
\$\begingroup\$

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!

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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.

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

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

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

05AB1E, 3 bytes

Code:

D<*

Explanation:

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

Try it online!

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

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

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

JavaScript REPL, 25 20 bytes

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

Will work on eliminating a REPL

\$\endgroup\$
8
  • \$\begingroup\$ How does this produce output? \$\endgroup\$
    – Dennis
    Commented Feb 19, 2016 at 2:57
  • \$\begingroup\$ In the console. I need to fix that. \$\endgroup\$ Commented Feb 19, 2016 at 2:57
  • \$\begingroup\$ no the console is fine, i consider that valid output for js \$\endgroup\$
    – Seadrus
    Commented Feb 19, 2016 at 2:58
  • \$\begingroup\$ Oh! Fantastic!! \$\endgroup\$ Commented Feb 19, 2016 at 2:58
  • 2
    \$\begingroup\$ @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. \$\endgroup\$
    – Alex A.
    Commented Feb 19, 2016 at 3:11
2
\$\begingroup\$

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

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

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.

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

Jolf, 6 bytes

Try it here!

oj*jwj

Explanation

oj*jwj
oj      set j to
  *jwj  j times j-1
        implicit output
\$\endgroup\$
1
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 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.

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

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.

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

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!

\$\endgroup\$
2
  • \$\begingroup\$ (*)=<<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. \$\endgroup\$
    – Zgarb
    Commented Feb 19, 2016 at 16:21
  • \$\begingroup\$ @Zgarb: Thanks! Adding id makes it a proper function. I've put a note in my answer. \$\endgroup\$
    – nimi
    Commented Feb 19, 2016 at 17:01
1
\$\begingroup\$

Pure bash (no utilities), 40

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

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 5 bytes: :Ans²-Ans \$\endgroup\$
    – lirtosiast
    Commented Feb 20, 2016 at 1:28
1
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

C++ (gcc), 173 / 176 bytes

Both versions have a crucial newline at the end.

Macro version, 173 bytes

#ifndef M
#define M(a,b)a##b
#define W(z,x)M(z,x)
#define B int W(m,__LINE__)=x++
#include<iostream>
int x,n;int main(){for(std::cin>>n;x--;)n*=n-1;std::cout<<n;}
#endif
B;

Try it online!

Template version, 176 bytes

Somewhat more C++ish:

#ifndef B
#define B template
#include<iostream>
int x,n;B<int N>struct A{static int i;};int main(){for(std::cin>>n;x--;)n*=n-1;std::cout<<n;}
#endif
B<>int A<__LINE__>::i=x++;

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Burlesque - 5 bytes

J?d?*

blsq ) 5 J?d?*
20
blsq ) 5 J?d?* J?d?*
380
blsq ) 5 J?d?* J?d?* J?d?*
144020
\$\endgroup\$

Your Answer

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

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