Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a code in either matlab or c++ for this assignment. Problems In this lab,

ID: 3740848 • Letter: W

Question

write a code in either matlab or c++ for this assignment. Problems In this lab, we will use MATLAB to implement primality tests, namely Fermat’s little theorem and Wilson’s Theorem. We will also implement Fermat’s Last Theorem. 1. Wilson’s Theorem (25 points) Wilson’s theorem states that a number p is prime if and only if the remainder of (p ? 1)! + 1 p is 0. Implement Wilson’s Theorem to determine whether a number is prime or not. Hint: In MATLAB, when you receive your input variable, p, use the following statement p = uint64(p) to convert p from a double to an unsigned integer that ranges from 0 to 18,446,744,073,709,551,61. 2. Fermat’s Little Theorem (25 points) Fermat’s little theorem states that a number p is prime if for all numbers a such that 1 ? a < p, the remainder of a p?1 ? 1 p is 0. Implement Fermat’s Little Theorem. Hint: As done in Problem 1. 3. Speed Test (25 points) Use MATLAB’s tic and toc function to determine the speed of determining whether a number p is prime or not using MATLAB’s built-in isprime, MATLAB’s built-in factor function, Wilson’s Theorem, and Fermat’s Little Theorem. Display the times of each function. 4. Fermat’s Last Theorem (25 points) “Fermat’s Last Theorem states that no three positive integers a,b, and c can satisfy the equation a n + b n = c n for any integer value of n greater than two (From Wikipedia).” For n = 2, we have the Pythagorean triples such as {3, 4, 5} where 32 + 42 = 52 . In MATLAB, empirically verify Fermat’s Last Theorem for bounded values of a, b, and c as seen below: ? ? 1 5 2 6 3 5 ? ? where a is the set of integers from 1 to 5, b is the set of integers from 2 to 6, and c is the set of integers from 3 to 5. To receive input from the user as a matrix, use the code below: >> data = str2num(input('Enter the bounds for a, b, and c: ', 's')); Enter the bounds for a, b, and c: 1 5; 2 6; 3 5 will save the matrix seen above in data. Also, take as input from the user the value of n. If the user inputs 2 for n, a sample output is given below: a a 2 b b 2 a 2 + b 2 c c 2 Equal? 1 1 2 4 5 3 9 False ... 3 9 4 16 25 5 25 True ... 5 25 6 36 61 5 25 False

Explanation / Answer

prompt = 'Enter the value of P';
p = input(prompt); %taking the input
p = uint64(p); %convert p to double to unsigned integer
if(mod((factorial(p-1)+1),p)==0) %checking the condition for p whether it is prime or not
disp('p is prime')
else
disp('p is not a prime')
end