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

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!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!