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

Problem: Write a Matlab program to compute the prime factorization of a given po

ID: 3873738 • Letter: P

Question

Problem: Write a Matlab program to compute the prime factorization of a given positive integer greater than 1. Your program should prompt the user for such a positive integer and then compute and display its prime factorization. For example, if the user inputted “360”, the output should be “360 = 2 * 2 *2 * 3 * 3 * 5”. If, however, the inputted number happens to be prime, then your program should detect this fact and inform the user. Do not use any of Matlab's factor functions.

I want to do this using the rem(x,y) function and dividing by 2,3,5,7,9,11,13 until the remainder is zero. And using fprintf for the output. Having trouble writing the if statements.

What I have so far:

clear;
n = input('Please enter a positive integer: ');
for i = 2 : n %leaving out zero and one
if rem(i,2)==0 %check if n is even

I've found another way to do this, but I don't understand it well enough to explain myself if I were to use it AND I can't get the output in the form requested:

clear;
n = input('Please enter a positive integer: ');
for p = 2 : n
r = 0;
while n/p == floor(n/p)
n = n/p;
r = r+1;
end
if r>0
str = [num2str(p) '^' num2str(r)];
fprintf('%8d ',p)
if n == 1
break
end
end
end

I would like help either using the rem(x,y) or an explanation of how this loop is working.

Thanks!

Explanation / Answer

Please run these codes into your matlab window and check out the output

clear;

n = input('Please enter a positive integer: ')=105;

fprintf(' %d =',n) % used to display input

for p = 2 : n

r = 0;

while n/p == floor(n/p)

n = n/p;

r = r+1;

end

if r>0

str = [num2str(p) '^' num2str(r)];

fprintf(' %2d ^ %d *',p,r)

if n == 1

break

end

end

end

Here is the sample output:

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