Factors

Factor is a scary word, but not really a mathematical monstrosity. If you have an integer value, its factors are the other integers that can divide evenly into the original value. The computer can deftly discover these values, providing you give it the proper C code to munch upon.

When I boned-up on this topic, I thought of money. Yes, I think of money often, but in this case I thought of making change.

For example, currency denominations are all factors of higher values: A $20 bill can be broken into 20 $1s or 4 $5s or two $10s. The values 1, 2, 4, 5, and 10 are all factors of 20. The value 20 is also a factor of 20. Incidentally, all numbers have their own value and 1 as factors. A prime number has only its own value and 1 as factors.

Yes, all this mathematical stuff can get nauseating quite quickly. For coding, however, calculating the factors of an integer presents an interesting puzzle.

One solution I saw had about 150 lines of code and three functions to do the job. Oh, and it used pointers. “Rubbish!” I thought, as I set upon to code my own factoring program.

To handle the math, I employed the modulus operator, %. It returns zero when two integers divide evenly into each other. That’s pretty much the golden ticket I need to determine whether variable x is a factor of integer f:

#include <stdio.h>

int main()
{
    int x;
    unsigned f;

    printf("Enter a positive integer: ");
    scanf("%u",&f);

    printf("Factors of %d:\n",f);
    for(x=1;x<=f;x++)
        if( f % x == 0)
            printf("%d\n",x);

    return(0);
}

The program prompts the user to enter a positive (unsigned) integer value in Lines 8 and 9. A for loop at Line 12 plows through the values 1 through variable f. If the result of f % x is equal to zero, the value is output.

Here’s a sample run for the value 56:

Enter a positive integer: 56
Factors of 56:
1
2
4
7
8
14
28
56

Each of the values output divides evenly into 56.

Here’s output for a prime number:

Enter a positive integer: 101
Factors of 101:
1
101

I tried the code on some humongous values and it ran just fine, though as the values increase, the code runs more slowly. That’s to be expected. Still, I don’t understand why the other example I saw used so many lines of code and different functions to do the same thing?

3 thoughts on “Factors

  1. You could get away with only looping to f/2. That wouldn’t print the number itself of course but you could do that after the loop.

    Doesn’t x need to be unsigned the same as f? If someone entered a number which fits into an unsigned int but not a signed int the loop would break.

    I can’t see how anybody could possibly stretch this out to 150 loc! Did they implement their own scanf, printf and modulus functions? 🙂

  2. Excellent suggestions, Chris. Thanks!

    The solution I saw used pointers to dynamically allocate storage as the code ran. So instead of printing the factors, the code stored them (for some reason), then looped through the stored values to display the results. Weird.

  3. I can see the reasoning behind that approach. In The Real World you might need some sort of data structure holding all the factors of a integer, rather than just finding one, printing it and throwing it away before moving on to the next.

Leave a Reply