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

The function WeirdSum takes as input a positive integer n, and returns the sum o

ID: 3716243 • Letter: T

Question

The function WeirdSum takes as input a positive integer n, and returns the sum of all numbers between 1 and n 1 that are either divisible by 3 or divisible by 4, but not both For instance, WendSum(5)-7, Weird Sum(8)-13, and Weird Sum(13) 30. (a) 7 points] Write a matlab function WeirdSumForLoop.m that uses a for loop to calculate WeirdSum(n) for any positive integer n. (b) [10 points] Write a matlab function WeirdSumNoLoop.m that cal culates WeirdSum(n) for any positive integer n, but does not use a for loop, while loop, or recursion. c)[5 points Write a script WeirdSumTest.m that compares the speed of WeirdSumForLoop.m and WeirdSumNoLoop.m for large values of n. Comment on the differences between the speed.

Explanation / Answer

function sum=WeirdSum(n)
sum=0;
for i=1:n-1
%Check if divisible by 3 or 4
if(rem(i,3)==0 || rem(i,4)==0)
%Check if divisible by 3 and 4
if(rem(i,3)==0 && rem(i,4)==0)
sum=sum;
else
sum=sum+i; %If not add integer
end
end
end
end

%WeirdSumForLoop.m

sum=WeirdSum(13)

%Call the function

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