CA I - Exercise sheet 02

Exercise  1:

100!!

101!!

1.

Clear[DF1];
DF1[n_]:=n DF1[n-2]/;IntegerQ[n]&&n>1
DF1[n_]:=1/;n==0 || n==1

DF1[100]

DF1[101]

2.

$RecursionLimit=Infinity;

Clear[DF2]
DF2[n_]:=(DF2[n]=n DF2[n-2])/;IntegerQ[n]&&n>1
DF2[n_]:=1/;n==0 || n==1

DF2[100]

DF2[101]

3.

Clear[DF5]
DF5[n_]:=Fold[Times,1,Range[n,1,-2]]

DF5[100]

DF5[101]

4.

Clear[DF6]
DF6[n_]:=Apply[Times,Range[n,1,-2]]

DF6[100]

DF6[101]

Exercise 2:

$RecursionLimit=Infinity;
Clear[Binom];
Binom[n_,k_]:=Binom[n,k]=Binom[n-1,k]+Binom[n-1,k-1]
Binom[n_,k_]:=1/;k==0 || k==n

Binom[1000,500]//Timing

Binomial[1000,500]//Timing