Write a Matlab function facFunc that does the following: 1. Takes as an input a
ID: 670932 • Letter: W
Question
Write a Matlab function facFunc that does the following:
1. Takes as an input a positive integer num and a vector vec. (2 pts)
2. If the vector vec consists of a single integer greater than 1, the function should return the highest power of
vec(1) such that num is divisible by it.(2 pts)
3. If the vector vec consists of multiple integers greater than 1, the function should return (as a vector) the highest
powers of vec(1), vec(2), ... etc., such that num is divisible by it.(2 pts)
4. If the vector vec contains a value that is not an integer, or is less than or equal to 1, the function should return
-1 for that value. (2 pts)
Test your function with at least 4 di erent inputs. (2 pts)
Example outputs could look as follows (user input is in boldface):
>> n=facFunc(18,2)
n =
1
>> n=facFunc(18,3)
n =
2
>> n=facFunc(18,4)
n =
0
>> n=facFunc(18,1.5)
n =
-1
>> n=facFunc(36,[0 1 2 3 4 5])
n =
-1 -1 2 2 1 0
>> n=facFunc(250,[2 3 4 5])
n =
1 0 0 3
so far I have:
function n = facFunc(num,vec)
n = -ones(size(vec));
for i = 1:length(vec)
if vec(i) > 1 || vec(i) == floor(vec(i))
xTest = 1;
while vec(i)^xTest <= num
if mod(num,vec(i)^xTest) < 1e-6 % To account for numerical computation errors
n(i) = xTest;
end
xTest = xTest+1;
end
end
end
this, however, only gives the correct response to (18,2) and (18,3)
I have received two other answer, none of which have gotten any closer than this one. Someone please give me an answer that works for all the examples. (:/) I'm desperate.
Explanation / Answer
//This will work , let me know if you miss any case
---------------------------sample output--------------------------------------
---------------------------------------------------------------------
simplified function-
function A = facFunc(num, input)
size = length(input) ;
A= zeros(1, size);
for i = 1 : size
%if it contains fraction should return -1
if(input(i) > 1 && ( input(i) - floor(input(i))== 0))
curpower = 1;
while(true)
if (rem(num,power(input(i), curpower) )== 0)
A(1,i) = curpower;
curpower++;
else
break;
end
end
else
A(1,i) = -1;
end
i++;
end
end
disp("Displaying results");
n = facFunc(18,2);
disp("n = ");
disp(n);
n=facFunc(18,3)
n=facFunc(18,4)
disp("----------------------------");
n=facFunc(18,1.5)
disp("----------------------------");
n=facFunc(36,[0 1 2 3 4 5])
n=facFunc(250,[2 3 4 5])
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.