-3
\$\begingroup\$

Challenge

The objective is simple, in the shortest amount of bytes, write a function or program that takes a number and prints its double factorial.

The input must be taken from stdin or a function argument, and it must print the resulting double factorial to stdout.

Inbuilts are disallowed

\$\endgroup\$
11
  • \$\begingroup\$ What makes this significantly different from Find the factorial? Most approaches will be the same IMO. \$\endgroup\$
    – Geobits
    Commented Jan 20, 2015 at 18:10
  • \$\begingroup\$ @Geobits The approach has to be different, due to the additional requirement of only getting the product of the numbers up to n with the same parity. \$\endgroup\$
    – globby
    Commented Jan 20, 2015 at 18:12
  • 4
    \$\begingroup\$ @globby No, I think I blame you for changing the rules to disallow an existing answer... which could have been avoided by posting the challenge in the sandbox first, where someone certainly would have pointed out that loophole. \$\endgroup\$ Commented Jan 20, 2015 at 18:29
  • 1
    \$\begingroup\$ I waited to VTC as dupe until I saw a few answers, but the current ones only reassure me that most answers here will simply be a couple chars different from the standard factorial. \$\endgroup\$
    – Geobits
    Commented Jan 20, 2015 at 18:41
  • 1
    \$\begingroup\$ If the question does not call out that REPL is not accepted, a JS answer without alert or haskell one without print is totally acceptable \$\endgroup\$
    – Optimizer
    Commented Jan 20, 2015 at 18:49

6 Answers 6

3
\$\begingroup\$

Haskell 21

p n=product[n,n-2..1]
\$\endgroup\$
4
  • \$\begingroup\$ @MartinBüttner: It does. At least within ghci. \$\endgroup\$
    – nimi
    Commented Jan 20, 2015 at 18:38
  • \$\begingroup\$ I can argue that my function without the alert prints to console when run from console. \$\endgroup\$
    – Optimizer
    Commented Jan 20, 2015 at 18:39
  • \$\begingroup\$ @Optimizer: Sure. Why not? Some languages provide or run in interactive environments (aka repl). Use it! \$\endgroup\$
    – nimi
    Commented Jan 20, 2015 at 18:43
  • 1
    \$\begingroup\$ Actually, looks like we have a consensus for this on meta. \$\endgroup\$ Commented Jan 20, 2015 at 18:49
3
\$\begingroup\$

CJam, 13 12 bytes

ri,:)1~%1+:*

Expanded code:

ri            "Read the input as a string and convert it to integer";
  ,           "Create an array of 0 to input integers - 1";
   :)         "Increment each element in the array";
     1~       "Put 1 and do bitwise not to get -2";
       %      "Take every other integer starting from end";
        1+    "Add 1 to the array to account for result of 0 being 1";
          :*  "Reduce product to get double factorial";

At the end of the code, anything on stack is automatically printed to STDOUT in CJam.

Try it online here

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

JavaScript, ES6, 38 36 bytes

Wow this is huge!

f=a=>alert((g=n=>n>1?n*g(n-2):1)(a))
<input id=D /><button onclick="f(+D.value)">Run</button>

Of course I am just counting JS code. HTML is just for stack snippet demo.

Run it in latest Firefox browser.

\$\endgroup\$
1
  • \$\begingroup\$ I know this is old and a closed challenge but you can do it in 34: f=(n,o=1)=>n>1?f(n-2,n*o):alert(o) \$\endgroup\$ Commented Jan 23 at 23:33
2
\$\begingroup\$

Matlab 16

@(n)prod(n:-2:1)

It is pretty sraight forward.

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

r, 30

f=function(n)prod(seq(n,1,-2))
\$\endgroup\$
1
\$\begingroup\$

Pyth: 11 character

u*GhH%2_UQ1

Q is the input value. UQ creates the list [0, 1, 2, ..., Q-1], _ inverts it, and %2 removes every other element [Q-1, Q-3, Q-5, ..., (1 or 0)]. u*GhH...1 reduces the list by multiplying the list elements (increased by 1) to 1.

Try it here: Pyth Compiler/Executor

\$\endgroup\$
0

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