Parasitic number

Test whether the first input x is an n-parasitic number


( n is the second input.)
Examples:

parasitic(128205,4) ---> true

parasitic(179487,4) ---> true

parasitic(179487,3) ---> false

MATLAB CODE:
clc
clear all
close all
x=input('Enter the number');
y=input('Enter the 2nd number');
z=x*y;
m=x;
Y=[];
c=0;
while(m>0)
    b=rem(m,10);
    Y=[b Y];
    c=c+1;
    m=(m-b)/10;
end
xi=[];
xi(1)=Y(c);
kl=1;
for i=2:c
    xi(i)=Y(kl);
    kl=kl+1;
end
m=z;
W=[];
while(m>0)
    b=rem(m,10);
    W=[b W];
    m=(m-b)/10;
end
aq=W-xi;
g=0;
for i=1:length(aq)
    if(aq(i)~=0)
        g=g+1;
    end
end
if(g==0)
    disp('YES');
else
    disp('No');
end

EXPLANATION:






Output:



Try with some other inputs and check :-)

Function:


function u = parasitic(x,y)
z=x*y;
m=x;
Y=[];
c=0;
while(m>0)
b=rem(m,10);
Y=[b Y];
c=c+1;
m=(m-b)/10;
end
xi=[];
xi(1)=Y(c);
kl=1;
for i=2:c
xi(i)=Y(kl);
kl=kl+1;
end
m=z;
W=[];
while(m>0)
b=rem(m,10);
W=[b W];
m=(m-b)/10;
end
aq=W-xi;
g=0;
for i=1:length(aq)
if(aq(i)~=0)
g=g+1;
end
end
if(g==0)
u=true;
else
u= false;
end
end

    

No comments