All pastes #769881 Raw Edit

Miscellany

public c v1 · immutable
·published 2007-11-11 19:02 UTC
rendered paste body
int primefactors(int length){  int intToTest = length;  int nrOfFactors = 0, i = 0, bigFactor = 0;  int maxVal=(int)sqrt(length);   if (length<2)    return 0;  for (i = 2; intToTest > 1 && nrOfFactors <3 && i<=maxVal; i++)  {    while ((intToTest%i)==0)    {      bigFactor = i;      intToTest /= i;      nrOfFactors++;    }  }  if (1!=maxVal && (intToTest%maxVal)==0)    nrOfFactors--;  if (nrOfFactors == 0) //one factor found (prime itself) ==> prime    return -2;  if (nrOfFactors == 1) //if there are exactly two factors    return bigFactor;  if (nrOfFactors > 1) //if there are more than 2 factors    return -3;  return 0;}