-2
\$\begingroup\$

This question already has an answer here:

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

marked as duplicate by Geobits, feersum, John Dvorak, globby, Optimizer Jan 20 '15 at 19:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ What makes this significantly different from Find the factorial? Most approaches will be the same IMO. \$\endgroup\$ – Geobits Jan 20 '15 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 Jan 20 '15 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\$ – Martin Ender Jan 20 '15 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 Jan 20 '15 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 Jan 20 '15 at 18:49
3
\$\begingroup\$

Haskell 21

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

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