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

Write a function in *****MATLAB***** that takes an integer (n) and then computes

ID: 1716938 • Letter: W

Question

Write a function in *****MATLAB***** that takes an integer (n) and then computes the following based on the value of the integer: While the value of n is greater than 1, replace the integer with half of its value (n/2) if the integer is even. Otherwise, replace the integer with three times its value, plus 1 (3*n + 1). Make provision to count the number of values in (or the length of) the sequence that results. Example calculation: If n = 10, the sequence of integers is 5, 16, 8, 4, 2, 1 and so the length is 6. Make a plot of the length of the sequence that occurs as a function of the integers from 2 to 30. For example, when n = 10, the length is 6 while for n = 15, the length is 17. Is there any pattern? Try larger numbers to see if any pattern occurs. Is there any integer for which the sequence does not terminate?

Explanation / Answer

n=input('Enter the n value');
i=0;
while n>1
k=rem(n,2);
if(k==0);
n=n/2;
else
n=(3*n)+1;
end
i=i+1;
seq(i)=n;

end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote