First of all, thanks to everyone for your permission, here is my final decision
z=input('enter your number : ');
string='';
for ii=2:z
s=0;
while z/ii==floor(z/ii) % check if z is divisible by ii
z=z/ii;
s=s+1;
end
if s>0
str =[num2str(ii) '^' num2str(s) ];
string=strcat(string,str);
string= strcat(string,'*');
% If z = 1, no more divisions are necessary,
% thus breaks the loop and quits
if z == 1
break
end
end
end
string=string(1:end-1);% remove last sign of multiplicaiton
fprintf('prime factorization is %s\n',string);
here are some examples
>> integer_factorization
enter your number : 30
prime factorization is 2^1*3^1*5^1
other
>> integer_factorization
enter your number : 35
prime factorization is 5^1*7^1
And last
>> integer_factorization
enter your number : 100
prime factorization is 2^2*5^2
source
share