Recursive double factorial P61384


Statement
 

pdf   zip   main.cc   main.c   main.java   main.py

thehtml

Write a recursive function that returns n!!.

Recall that n!! = n × (n − 2) × (n − 4) × …. For instance, 9!! = 9 × 7 × 5 × 3 × 1 = 945 and 8!! = 8 × 6 × 4 × 2 = 384. By definition, 0!! = 1!! = 1.

Interface

C++
int double_factorial(int n);
C
int double_factorial(int n);
Java
public static int doubleFactorial(int n);
Python
double_factorial(n) # returns int
 
double_factorial(n: int) -> int

Precondition

Assume 0≤ n ≤ 19.

Observation You only need to submit the required procedure; your main program will be ignored.

Public test cases
  • Input/Output

    double_factorial(9) → 945
    double_factorial(8) → 384
    double_factorial(1) → 1
    double_factorial(0) → 1
    double_factorial(19) → 654729075
  • Information
    Author
    Salvador Roura
    Language
    English
    Translator
    Salvador Roura
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C C++ Java Python
    User solutions
    C C++ Java Python