Function Finds the Prime Factorization of An Integer Greater than 1

My task is to create a function that finds the prime factorization of an integer greater than 1. n is a positive integer and v is a row vector containing the prime factorization of n given in order including repetitions. (primes(k) returns a row vector of the primes 2 to k). I understand that I am to create a program that takes a number n such as 600 and produces a row vector v that contains the prime factorization of the number such as v = [2 2 2 3 5 ]. I know I will need to use some rounding techniques like the 'fix' command. I am asking for help on how begin making this program as I barely have anything and am lost in how to continue. Here is what I have:
function v = ex5(n)
%
%
v = zeros(1, %number of primes in n)
if n/2 == fix(n/2)
% n must be divided by 2 and keep dividing until n/2 ~= fix(n/2)
% n must be divided by 3 ...
% n must be divided by 5 ...
% keep dividing by the primes
% store all the primes divisible by n in vector v
end
I understand this is a rough outline of my program. Thank you in advance for the help.

 Accepted Answer

The matlab factor command does it one line. But maybe you have to do it yourself for a homework exercise? For example
f = factor(600)
f =
2 2 2 3 5 5

7 Comments

Yes I have to do it myself for a homework exercise, so I was looking for some pointers on how to begin that process.
First I would suggest just doing it out by hand on a sheet of paper for a few simple examples, but big enough so that they have at a least a couple of repeating factors, e.g 45, 300 just to get a feel for what the steps are
Then think about the loops you need, hint maybe a loop to go through the prime factors 2,3,5... and another loop to see how many times each one divides your number. As you loop you add factors to the list.
If you have a loops then you need to know when to end them. As you already started to indicate you can test if something divides a value without a remainder (as you started looking at with fix, MATLAB also has a function that gives you the remainder, check the help for this)
Hope these ideas can get you started. Once you have some code written, you may find more specific questions to ask
Here is the new program I have written up. It is fairly developed, however it goes into an infinite loop and I do not know why.
function v = ex5(n)
%
%
v = zeros(1,10);
vLength = length(v);
position = 1;
while position <= vLength
if n/2 == fix(n/2)
v(position) = 2;
n = n/2;
position = position + 1;
end
if n/3 == fix(n/3)
v(position) = 3;
n = n/3;
position = position + 1;
end
if n/5 == fix(n/2)
v(position) = 5;
n = n/5;
position = position + 1;
end
end
% I will have to add more primes once I get my program more developed
Consider an input of 7: it is not divisible by 2 so you do not increment position, it is not divisible by 3 so you do not increment position, it is not divisible by 5, so you do not increment position; at the end you would not have incremented position and you would not have changed n, so you have an infinite loop.
Question: are you permitted to use the "next prime" function ?
No we are not permitted to use the “next prime” function. Is there an alternative to this?
Thank you for the help. I believe I have developed a program that works.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!