In[63]:=
firstprimeabove[ n_]:= Module[ {k},
k=n+1;
While[ !PrimeQ[k],
k=k+1;
];
Return[k];
]
In[64]:=
firstprimeabove[ 167]
Out[64]=
173
In the While loop, we increment k as along as it is not prime. When the loop is
finished k is prime. Observe that even if the input is prime, then the
function returns the
next prime. The first statement of Module is a list of the variables internal
to the Module, and
are placed within braces. After that, there is a series of statements
separated by semi-
colons.
Exercise: Modify the function to return the second prime above n, or more
generally, the function should take a second argument r, and return the rth
prime
above n.
Up to Loops and Conditionals