4

suppose we have following script for prime factorization

 z=input('enter your number : ');
    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) ];
            disp(str) ;      
            % If z = 1, no more divisions are necessary, 
            % thus breaks the loop and quits
            if z == 1
                break
            end
        end

    end

but output of this code is not formatted well,for instance

 >> integer_factorization
    enter your number : 30
    2^1
    3^1
    5^1

how can i do so that i got

30=2^1*3^1*5^1?

thanks in advance

5 Answers5

2

This is fairly straightforward: Use fprintf(''); instead to print/display the factors.


z=input('enter your number : ');

ans='' %the final answer 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) ];
        %disp(str) ;  

        strcat(ans,'*',str); %concats the * str as per requirement.

        % If z = 1, no more divisions are necessary, 
        % thus breaks the loop and quits
        if z == 1
            break
        end
    end
end

ans=ans(2:end); % to remove the first * 
fprintf(ans); % can even use the disp() function.

So, basically, added a string to append the factors into it and display at the end, outside of the loops.

Community
  • 1
  • 1
ABcDexter
  • 2,751
  • 4
  • 28
  • 40
1

You can simply create a string and add your numbers to the string and finally print the string. something as follow:

 z=input('enter your number : ');
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 = str + [num2str(ii) '^' num2str(s) '*' ];%only update
        % If z = 1, no more divisions are necessary, 
        % thus breaks the loop and quits
        if z == 1
            break
        end
    end
    str = str(0,str.size -1) %use proper command to remove the last * from your string result
    disp(str) ; %display the str at the end in one line
end
USC.Trojan
  • 391
  • 5
  • 14
  • no no delete answer, it was typo,i am asking different issue –  Apr 02 '17 at 19:23
  • edited the answer, you should only edit the command I used to remove the last character of the result string (str) since it has an extra '*' – USC.Trojan Apr 02 '17 at 19:27
1

Litle modification of your code.

z=input('enter your number : ');
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) ];

        % If z = 1, no more divisions are necessary, 
        % thus breaks the loop and quits
        if z == 1
            break
        else 
           str+='*';
        end
    end

end
disp(str);
Incognito
  • 133
  • 1
  • 10
1

This could work! Just with a few changes to your code

z=input('enter your number : ');
str=[num2str(z) '='];
first=0;
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
        if first==0
            str=[str num2str(ii) '^' num2str(s)];
            first=1;
        else
            str=[str '*' num2str(ii) '^' num2str(s)];
            if z == 1
                break
            end
        end
   end
end
1

first of all thanks everyone for your afford, here is my final solution

 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 is several examples

>> integer_factorization
enter your number : 30
prime factorization is 2^1*3^1*5^1

another

>> integer_factorization
enter your number : 35
prime factorization is 5^1*7^1

and last one

>> integer_factorization
enter your number : 100
prime factorization is 2^2*5^2