Exit[]

Programming

Timings

N[Pi,10^2]

Timing[N[Pi,10^3]]

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

N[Pi,10^8];//Timing

If[], Which[], Switch[], With[], Module[], Block[] ...
Look up the constructs in the Mathematica guide!

Blocks

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

With[{x=1},
Print["x=",x,"   y=",y];
];
Print["x=",x,"   y=",y];
]

Clear[x,y]

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

{x,y}
]

With[{a,b,c},
Pi]

Probably that was not what you're looking for. 
Module[]

Module[{a,b,c,d},
Pi]

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

{x,y}
]

Block[]

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

{x,y}
]

Clear[x,y]

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

{x,y}
]

x=5; y = x+5

(x=5; y = x+5)

Example: Fibonacci

What's the most efficient way to compute the n-th number of the Fibonacci sequence?

1) Recursive

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

fibR[30]//Timing

fibR[50]//Timing

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

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

fibR[15]

calls

2) Dynamic Programming

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[11]

?fibD

fibD[100000]

4

MemoryInUse[]

3+4

?fibR

?fibD

fibD[30]

?fibD

Clear[fibD]

fibD[1000]

Timing[fibD[30]]

3) Imperative: While, For, Do

?For

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

fibI[1000]

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

fibI2[1000]

4) Symbolic

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]

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

Clear[fibS]

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

fibW[40]//Expand

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

fibS[150]

fibS[1000]//Timing

5) Numeric

Clear[fibN]

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

fibS[1000]

fibW[1000]

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

fibN[1000]

fibS[1000]

fibN[1000]-fibS[1000]

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

fibN2[1000]

fibN2[1000]-fibS[1000]

fibN2[1000]//Timing

6) Functional

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

?Nest

Nest[f,x0,5]

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

fibF[1000]//Timing

7) Matrix Form

M1={{0,1},{1,1}};
M2={{a,b},{b,c}};
{M1//MatrixForm,
M2//MatrixForm,
M1.M2//MatrixForm,
M1.M2/.{a+b->c,b+c->d}//MatrixForm}

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

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

fibM[1000]

{vals,vects}=Eigensystem[{{0,1},{1,1}}]

Expand[vals^1000];

Tests

$RecursionLimit= 10000

Clear[fibD];
fibD[0]=0;fibD[1]=1;
fibD[n_]:=fibD[n]=fibD[n-1]+fibD[n-2];

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

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

Timing[fibD[10000]]

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]))]

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}
]//MatrixForm

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

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

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

Some plotting

g[x_]:=Sin[.5 x^2]

Options[Plot]

pl=Plot[g[x],{x,0,2 Pi},
PlotRange->{-1,1},
PlotStyle->Yellow,
Background->Brown]

Range[0,2Pi,.1]//Length

ListPlot[
Map[{#,g[#]}&,
Range[0,2Pi,.1]]]

lp=ListPlot[Map[{#,g[#]}&,Range[0,2Pi,.1]],Joined->True,PlotStyle->Red]

Show[{pl,lp},Background->RGBColor[.2,.3,.4]]

evals={};

gaux[x_]:=(evals=Join[evals,{x}];g[x])

pl=Plot[gaux[x],{x,0,2 Pi},PlotRange->{-1,1},PlotStyle->Yellow,Background->Brown]

Length[evals]

evals

pts=Graphics[
Map[{AbsolutePointSize[1],Blue,Point[{#,g[#]}]}&,evals]
]

Show[{pl,pts}]

g3D[x_,y_]:=Sin[x^2+y^2]

g3Daux[x_,y_]:=(evals3D=Join[evals3D,{{x,y}}];g3D[x,y])

evals3D={};
Plot3D[g3Daux[x,y],{x,0,3/2Pi},{y,0,Pi},BoxRatios->Automatic,PlotPoints->30]

{Length[evals3D],30^2}

pt3d=Graphics3D[
Map[{AbsolutePointSize[1.2],White,Point[Join[#,{g3D@@#}]]}&,evals3D],Background->Gray
]

pl3d=Plot3D[g3D[x,y],{x,0,3Pi/2},{y,0,Pi},BoxRatios->Automatic,Mesh->False,Lighting->{{"Ambient",Black}}]

Show[{pl3d,pt3d}]