Ulam Sequence

Author

Eric W. Weisstein
May 24, 2004

This notebook downloaded from http://mathworld.wolfram.com/notebooks/IntegerSequences/UlamSequence.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/UlamSequence.html.

©2005 Wolfram Research, Inc. except for portions noted otherwise

Code

This code should be made more effi

UlamSequence::usage="UlamSequence[u,v,n] gives the first n terms of the (u,v) Ulam sequence."

UlamTermQ[l_List,n_]:=Module[{sums=0,i=0,len=Length[l]},While[i++≤n,If[MemberQ[l,i]&&MemberQ[l,n-i]&&n≠2i,sums++];];
(sums==2)]

UlamSequence[u_,v_,n_:10]:=Module[{U={u,v,u+v},i=u+v+1},While[Length[U]<n,If[UlamTermQ[U,i],U=Append[U,i]];
i++;];
U]
Null

Enumeration

Table[UlamSequence[1,n],{n,2,5}]//TableForm

Table[UlamSequence[2,n],{n,3,5}]//TableForm

Plot

<<Utilities`Plot`

Show[PixelPerfect[NaryPlot[UlamSequence[1,2,200]]]]//Timing

UlamSequence[1,2,150];//Timing