HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Prime Factors (32 bits, without Sieve)

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
factorswithoutbitssieveprime

Problem

I wrote this prime factors program as a programming exercise.

Any comments or constructive criticisms are welcome.

One question: prime[] is an array which stores the number's prime factors and associated exponents. How many elements, at most, does this array require? That is, what is the greatest number of prime factors needed to express any number between 1 and 4294967295? As an example 750 would need 3 elements since 750 = 2 3 5 ^ 3. I could do an exhaustive test using a static variable to keep track of the maximum value of pIndex but the 2.4 billion numbers would take at least 12 hours on my single core machine. Is there a quicker way to determine the answer?

```
/* primeexponents.c

display a number's prime factors using exponents and also its number of
divisors

note: unsigned 32 bit numbers (1 - 4294967295)
*/

#include
#include

void primeExponents (unsigned long int number, unsigned int list[])
{
printf ("\nnumber = %lu\n", number);

struct
{
unsigned long int factor;
int exponent;
} prime[20] = { { 0, 0 } };

unsigned long int num = number;
int pIndex = 0, lIndex = 0;

prime[pIndex].factor = (unsigned long int) list[lIndex];

while ( prime[pIndex].factor 0 )
++pIndex;
++lIndex;
prime[pIndex].factor = (unsigned long int) list[lIndex];
}
}

// num = greatest prime factor

if ( num == prime[pIndex].factor )
++prime[pIndex].exponent;
else {
if ( prime[pIndex].exponent > 0 )
++pIndex;
prime[pIndex].factor = num;
prime[pIndex].exponent = 1;
}

printf ("prime factors =");

unsigned long int product = 1;
int divisors = 1;

for ( int i = 0; i 1 )
printf (" ^ %i", prime[i].exponent);
if ( i >>");
printf ("\n");

printf ("divisors = %i\n\n", divisors);
}

void generateList (unsigned int list[])
{
// generate a list of prime numbers to use a

Solution

You don't need an exhaustive search: \$23571113171923*29 = 6469693230 > 2^{32}\$, so it is safe to allocate 9 slots.

primeExponents does too much and shall be split into three functions, one to factorize a number, another to validate factorization, and yet another to print it.

The factorization logic is extremely hard to follow. Consider instead

void factorize(int num, factors_t * factors, int * primes)
{ 
  for (; num > 1; ++primes) {
    factors->prime = *primes;
    factors.exponent = 0;

    while (num % *primes == 0) {
      factors->exponent++;
      num /= **primes; // Edit: shame on me.
    }

    if (factors->exponent > 0) {
      factors++;
    }
  }
}

Code Snippets

void factorize(int num, factors_t * factors, int * primes)
{ 
  for (; num > 1; ++primes) {
    factors->prime = *primes;
    factors.exponent = 0;

    while (num % *primes == 0) {
      factors->exponent++;
      num /= **primes; // Edit: shame on me.
    }

    if (factors->exponent > 0) {
      factors++;
    }
  }
}

Context

StackExchange Code Review Q#150116, answer score: 3

Revisions (0)

No revisions yet.