Prime Factorization

// Prime Factorization

public class Prime_Factorization
{
public static void main(int n)
{
int sp=1,d,i;
int copy=n;
while(sp!=copy)
{
d=factor(n);
if(isprime(d))
{
System.out.println(d);
sp=sp*d;
n=n/d;
}

}

}

public static int factor(int n)
{
int i;
for(i=2;i<=n;i++)
{
if(n%i==0)
break;
}

return i;
}

public static boolean isprime(int n)
{
int i,c=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
c++;
}
}

if(c==2)
return true;
else return false;
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s