Friday, March 11, 2011

WAP for Find Prime Factor Number (Prime Factors Only)

#include

double sqroot(const double s)
{

double xn = s / 2.0;
double lastX = 0.0;

// Looping this way ensures we perform only as many calculations as necessary.
// Can be replaced with a static for loop if you really want to.
while(xn != lastX)
{
lastX = xn;
xn = (xn + s/xn) / 2.0;
}
return xn;
}

int main()
{
int n=100,i;
int upper_bound=(int)sqroot(n);
for(i=2;i<=upper_bound;i++)
{
if(n%i==0)
{
printf("%d,",i);
n=n/i;
i--;
if(n==1)
break;
}
}

}

Time Complexity O(sqrt(n))
Space Complexity O(1)
Run Here https://ideone.com/ot5Y3

More Info http://mathworld.wolfram.com/PrimeFactor.html

No comments :