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

Write a Matlab function that does the following: 1. Takes as an input a positive

ID: 670760 • Letter: W

Question

Write a Matlab function 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

Explanation / Answer

corrected solution

function A = facFunc(num, intVec)
    len = length(intVec) ;

    % fprintf( "lengtn = %d , num = %d ", len, num);

    result = zeros(1, len);
    for i = 1 : len
        %fprintf(" %d = ", intVec(i));
         %find out its fraction or not
       fraction = intVec(i) - floor(intVec(i));
        if(intVec(i) > 1 && fraction == 0)
            pow = 1;
            while(true)
               divisor = power(intVec(i), pow);
               reminder = rem(num, divisor );
               %fprintf("div = %d , rem = %d ", divisor, reminder);
               if (reminder == 0)
                   result(1,i) = pow;
                   pow++;
               else
                    break;
               end
           
            end
        else
            result(1,i) = -1;
       
        end
         i++;
    end
    A = result;
end

n = facFunc(18,2);
fprintf("n = %d ",n);

n=facFunc(18,3)

n=facFunc(18,4)
n=facFunc(18,1.5)
n=facFunc(36,[0 1 2 3 4 5])
n=facFunc(250,[2 3 4 5])

old solution

------------------------------------------------------------------------

function A = facFunc(num, intVec)
    len = length(intVec) ;
  
    % fprintf( "lengtn = %d , num = %d ", len, num);
   
    result = zeros(1, len);
    for i = 1 : len
        %fprintf(" %d = ", intVec(i));
        if(intVec(i) > 1)
            pow = 1;
            while(true)
               divisor = power(intVec(i), pow);
               reminder = rem(num, divisor );
               %fprintf("div = %d , rem = %d ", divisor, reminder);
               if (reminder == 0)
                   result(1,i) = pow;
                   pow++;
               else
                    break;
               end
             
            end
        else
            result(1,i) = -1;
         
        end
         i++;
    end
    A = result;
end

n = facFunc(18,2);
disp(n);

n=facFunc(18,3)

n=facFunc(18,4)
n=facFunc(18,1.5)
n=facFunc(36,[0 1 2 3 4 5])
n=facFunc(250,[2 3 4 5])

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