/****************************************************************************
 * File:         factors.c
 * Purpose:      Finding factors
 * Author:       Richard Butler
 * Date:         20 Mar 2002
 ***************************************************************************/

/*
    Factor - Finding factors
    Copyright  Richard Butler 2002

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/

#include <stdio.h>           // scanf(), printf()
#include <time.h>            // time()
#include <stdlib.h>          // atoi()

int factorise(int register); // This function searches for factors of the
                             // given number

int main(int argc, char *argv[])
{
  int    factors = 0;  // Hold the number of factors found
  int    number = 0;   // Integer to find factors
  time_t begin = 0;    // Hold how much time has passed

  // This part of the program allows the user to input a integer from either
  // (a) the command line prompt or (b) during the program
  if (argc != 2) {
    printf("Integer: ");
    scanf("%d", &number);
  }
  else {
    number = atoi(argv[1]);
  }

  // Start the timer
  begin = time(NULL);

  // Call the factorise() function to find factors. The nunber of factors is
  // returned to the variable 'factors'
  factors = factorise(number);

  // For speed reasons this program won't show prime factors (e.g. factors
  // of 16 would NOT include 16X1 or 1X16
  printf("\n(This program will not show prime factors)\n");

  // Display the total number of factors and the time taken to process
  // the number
  printf("\nTotal number of factors: %d (%d seconds)\n", factors, time(NULL)-begin);

  return 0;
}

int factorise(int register number)
{
  int register   x = 0;        // Part one of the loop
  int register   y = 0;        // Part two of the loop
  int            factors = 0;  // Hold the number of factors found

  // Off we go...
  for (x = 1; x <= (number / 2); x++) {

    for (y = 1; y <= (number / 2); y++) {

      if (x * y == number) {
        // If x * y = to the number, we have factors and these are printed
        // in a nice format
        printf("%8d * %8d = %d\n", x, y, number);

        // Add one to the list of factors
        factors++;
      }

    }

  }

  // Send the total number of factors back to the main function and exit.
  return factors;
}
