Prime factorization is the process of separating a composite number into its prime factors. You are to write a recursive function that accepts a positive integer x, a test divisor, and an array of the current prime factors. It should then output the prime factors of the integer x. For example, the prime factors of 72 are [2,2,2,3,3]. You are required to do the following: 1. Stop the program if invalid inputs are used. 2. Verify that x is a positive integer greater than 1. 3. Verify that divisor is a positive integer in the range (1, 4. Perform the prime factorization of x. 5. The elements in your output array should increase from left to right.

Respuesta :

Answer:

function pf=primeFactor(n)

%Print the number of 2s that divide n

pf=[];

while mod(n,2) == 0

pf=[pf,2];

n = n/2;

end

% n must be odd at this point. So we can skip

% one element (Note i = i +2)

for i=3:sqrt(n)

% While i divides n, print i and divide n

while mod(n, i) == 0

pf=[pf,i];

n = n/i;

end

end

% This condition is to handle the case when n

% is a prime number greater than 2

if (n > 2)

pf=[pf,n];

end

end

Explanation: