Timing

Timing[exp*exp+exp^2]

Timing[N[Pi,10^4];]

Timing[myPi=N[Pi,10^5];]

Plot[Sin[x^2]/Exp[x],{x,0,1}]//Timing

Blocks: With, Module, Block

x="Paolo";
y=Plot[Sin[x^2],{x,0,1}];

With[{
x=0,
y=Apply[Plus,Array[v,{4}]]
},
{x,y}
]
{x,y}

Clear[x,y]

With[
{x=4,
y=x+5},
{x,y}
]

With[{x=4},
With[{y=x+5},
{x,y}
]
]

a=10;b=20;c=30;d=40;

Module[{a,b=5,c,d},
Pi+a+b
]

Module[{a,b=5,c,d, Pi=E},
Pi+a+b
]

Module[
{x=4,
y=x+5},
{x,y}
]

Module[{x=4},
Module[{y=x+5},
{x,y}
]
]

{a,b}

Block[{a,b,c},
Pi+a+b
]

Block[
{x=4,
y=x+5},
{x,y}
]

{x,y}

Block[
{y=x+5,
x=4},
{x,y}
]

Module[{a,b,c},
Block[{},

]
]

?Block

Other constructs

If[test,True,False,undecidable]

4==5

Head[3.0]==Integer

If[Head[3.0]==Integer,True,False,undecidable]

Which, Switch

Table

Do, For, While

Manipulate[
Plot[Sin[x+t],{x,0,2Pi}],
{t,0,Pi,.1}]

?Factor*

Manipulate[
FactorTerms[(1+x)^p],
{p,1,10,1}
]

Simplify[(1+x^4)]

Programming : Fibonacci

1) Recursive   - FibR

fibR[0]=0;
fibR[1]=1;
fibR[n_Integer?Positive]:=fibR[n-1]+fibR[n-2]

fibR[n_Integer/;n>0]:=fibR[n-1]+fibR[n-2]

fibR[30]//Timing

fibR[0]:=(calls[[1]]++;0);
fibR[1]:=(calls[[2]]++;1);
fibR[n_Integer?Positive]:=(calls[[n+1]]++;fibR[n-1]+fibR[n-2])

n=16;
calls=Table[0,{n+1}];
fibR[n]
calls

2) Dynamic Programming  - FibD

Clear[fibD]

$RecursionLimit=100000;
fibD[0]=0;
fibD[1]=1;
fibD[n_]:=fibD[n]=fibD[n-1]+fibD[n-2]

?fibD

fibD[10]

?fibD

fibD[100000]

2+3

?fibD

MemoryInUse[]

3) Imperative: While, For, Do  - FibI, FibI2

?For

fibI[n_]:=
(
For[
i=1;t0=0;t1=1,
i<n,
i++,
{t0,t1}={t1,t0+t1}
];
t1
)

fibI[10]

fibI2[n_]:=
(
For[
i=1;t0=0;t1=1,
i<n,
i++,
aux=t0;t0=t1;t1=aux+t1
];
t1)

fibI2[10]

4) Symbolic  - FibS, FibS2

Solve[
{x+y==4,
x-y==-1},
{x,y}
]

RSolve[{
g[n]==g[n-1]+g[n-2],
g[1]==1,
g[0]==0
},
g[n],n]

fibS2[n_]:=Fibonacci[n]

fibS2[10]

fibW[n_]:=-((((((1/2)-((Sqrt[5])/2)))^n)-((((1/2)+((Sqrt[5])/2)))^n))/(Sqrt[5]))

fibW[10]//Expand

fibS[n_]:=Expand[-((((((1/2)-((Sqrt[5])/2)))^n)-((((1/2)+((Sqrt[5])/2)))^n))/(Sqrt[5]))]

5) Numeric  - FibN

fibW[n_]:=N[-((((((1/2)-((Sqrt[5])/2)))^n)-((((1/2)+((Sqrt[5])/2)))^n))/(Sqrt[5]))]

fibW[10]

fibS[100]

fibW[100]

(fibS[100]-fibW[100])/fibS[100]

fibW[n_]:=N[-((((((1/2)-((Sqrt[5])/2)))^n)-((((1/2)+((Sqrt[5])/2)))^n))/(Sqrt[5])),50]

?fibW

fibW[10]

fibW[100]

fibS[100]

(fibS[100]-fibW[100])/fibS[100]

fibN[n_]:=N[-((((((1/2)-((Sqrt[5])/2)))^n)-((((1/2)+((Sqrt[5])/2)))^n))/(Sqrt[5])),Round[n/2]]

fibW[100]

fibW[1000]

fibN2[n_]:=-(((((.5-((Sqrt[5.])/2)))^n)-(((.5+((Sqrt[5.])/2)))^n))/(Sqrt[5.]))

fibN2[1000]

fibN2[n_]:=-N[(((((.5-((Sqrt[5.])/2)))^n)-(((.5+((Sqrt[5.])/2)))^n))/(Sqrt[5.])),50]

fibN2[1000]

6) Functional  - FibF

next[{x_,y_}]:={y,x+y}

next[next[{0,1}]]

?Nest

Nest[next,{0,1},9]

fibF[n_]:=
Nest[next,{0,1},n-1][[2]]

fibF[10]

7) Matrix Form  - FibM

M1={{0,1},
     {1,1}};
M2={{a,b},
     {b,a+b}};
MatrixForm /@
{M1,
M1.M1,
M1.M1.M1,
M1.M2,
M1.M1.M1.M1}

Table[MatrixForm[MatrixPower[{{0,1},{1,1}},i]],{i,9}]

fibM[n_]:=
MatrixPower[
{{0,1},
{1,1}},n-1][[2,2]]

fibM[10]

Tests - correctness

$RecursionLimit= 10000

Map[
#[100]&,{fibD,fibI,fibI2,fibS,fibS2,fibN,fibF,fibM}
]

With[{
res=Map[
#[1000]&,
{fibI,fibI2,fibS,fibS2,fibN,fibF,fibM}
]},
Norm[res-res[[1]]]
]

Exit[]

Tests - speed

definitions

fibI[n_]:=
(
For[
i=1;t0=0;t1=1,
i<n,i++,
{t0,t1}={t1,t0+t1}
];
t1
)

fibI2[n_]:=
(
For[
i=1;t0=0;t1=1,
i<n,
i++,
aux=t0;t0=t1;t1=aux+t1
];
t1)

fibS[n_]:=Expand[-((((((1/2)-((Sqrt[5])/2)))^n)-((((1/2)+((Sqrt[5])/2)))^n))/(Sqrt[5]))]

fibS2[n_]:=Fibonacci[n]

fibN[n_]:=N[-((((((1/2)-((Sqrt[5])/2)))^n)-((((1/2)+((Sqrt[5])/2)))^n))/(Sqrt[5])),Round[n/2]]

next[{x_,y_}]:={y,x+y}

fibF[n_]:=
Nest[next,{0,1},n-1][[2]]

fibM[n_]:=
MatrixPower[{{0,1},{1,1}},n-1][[2,2]]

Map[
Flatten@{#,Timing[#[10000];]}&,{fibI,fibI2,fibS,fibN,fibF,fibM,fibS2}
]//MatrixForm

Map[
Flatten@{#,Timing[#[500000];]}&,{fibI,fibI2,fibN,fibF,fibM,fibS2}
]//MatrixForm

Map[
Flatten@{#,Timing[#[1000000];]}&,{
fibN,fibM,fibS2}
]//MatrixForm

Map[
Flatten@{#,Timing[#[100000000];]}&,{fibM,fibS2}
]//MatrixForm

?fibS2

Lists & Sets

l={1,2,3,{exp,,f[6]},2,3,3}

Reverse[l]

Flatten[l]

Drop, Take, Select, Join, Append, Prepend,

Union[{1,b,2,Sin[4],exp,3,{exp,,f[6]},2,3,3}]

Union[{1,b,2,Sin[4],exp,3,{exp,,f[6]},2,3,g[3,4,5],3}]//FullForm

Intersection,Union, Complement,

?Complement

Complement[{1,2,3,4,5},{4,5,6}]