n = 2; mod(factorial(n-1)+1,n) % you get zero, if n is a prime; otherwise, you get a non-zero numberThere are a few special prime numbers \(p\) called Wilson prime, besides the condition \( (p-1)!\ \equiv\ -1 \ \ (\mbox{mod } p)\), which satisfy the more restricted relation \( (p-1)!\ \equiv\ -1 \ \ (\mbox{mod } p^2)\). The first is \(p=5\): frome \( (5-1)! = 4! =24\), we get \( (5-1)! \equiv -1 (\mbox{mod } 25)\), instead of just \( (5-1)! \equiv -1 (\mbox{mod } 5)\). Complete the following script, to find all such Wilson primes that are less than 1000 (there is only one between 10 and 100, and then only another one between 100 and 1000).
N = 1000;
pl = primes(N); % get the list of all primes less than or equal to N
for p=pl % loop over all primes up to N
% check whether p is a Wilson prime
end
Because the built-in function factorial can only be used for numbers less than 19
(forget about the much slower alternative using symbolic numbers),
you have to apply the modulus frequent enough.
dsolve, whose general solution is
\begin{equation}
y(x) = \frac{1-\tanh^2\frac{x+c_2}{2c_1}}{2c_1^2}.
\end{equation}
You can use diff(y,x,2) for the second derivative of y with respective to x, and similarly for higher order derivatives. Notice
that the constants in the general solution could be different from the above expression (but still equivalent).
ode45, and compare the error between the numerical solution and
the exact solution (by evaluating at the vector of time from the numerical solver).
options = odeset('refine',8); in your code, and then ode45( ..., options) to get enough points/resolution for plotting.
This system is chaotic, because the solution could be completely different in the long run,
if the initial condition is changed slightly. Compare the (numerical) solution at the end of the time
interval \(t=500\) with the initial condition \(x(0)=1,\dot{x}(0)=-0.2\) and the initial condition \(x(0)=1+10^{-10},\dot{x}(0)=-0.2\).
options = odeset('refine',8); in your code, and then ode45( ..., options) to get enough points/resolution for plotting.
You can also change the initial condition slightly, to see how the final solution at \(t=500\) changes drastically.
%% Periodic solution of the non-linear ODE y' = -y^3 + sin (t) % save this file as per_nlode.m function per_nlode y0 = fzero(@(y0)sol_diff(y0),0); function yd = sol_diff(y0) % return the difference y(2*pi)-y(0) of solutions to y' = -y^3 + \sin t, with input y(0)=y0Once you find the right initial condition \(y(0)\), solve it on the interval \([0,20\pi]\) and plot it (which should look periodic with period \(2\pi\)).