Formatting a simple factorization algorithm

suppose we follow a script for simple 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 the output of this code is not formatted well, for example

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

how can i get it

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

early

+4
source share
5 answers

This is pretty simple: use fprintf (''); instead for printing / displaying 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, a line has been added to add factors to it and display at the end, outside of the loops.

+2
source

You can simply create a line and add your numbers to the line and finally print the line. sort of:

 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
+1
source

Litle .

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);
+1

!

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
source

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
+1
source

Source: https://habr.com/ru/post/1673834/


All Articles