The   proper divisors   of a positive integer N are those numbers, other than N itself, that divide N without remainder.

For N > 1 they will always include 1,   but for N == 1 there are no proper divisors.



Examples

The proper divisors of     6     are   1, 2, and 3.

The proper divisors of   100   are   1, 2, 4, 5, 10, 20, 25, and 50.



Task

 Create a routine to generate all the proper divisors of a number.
 use it to show the proper divisors of the numbers 1 to 10 inclusive.
 Find a number in the range 1 to 20,000 with the most proper divisors. Show the number and just the count of how many proper divisors it has.


Show all output here.



Related tasks

   Amicable pairs
   Abundant, deficient and perfect number classifications
   Aliquot sequence classifications
   Factors of an integer
   Prime decomposition


 
BEGIN
 
% COMPUTE P MOD Q % 
INTEGER FUNCTION MOD (P, Q);
INTEGER P, Q;
BEGIN
    MOD := P - Q * (P / Q);
END;
 
% COUNT, AND OPTIONALLY DISPLAY, PROPER DIVISORS OF N %
INTEGER FUNCTION DIVISORS(N, DISPLAY);
INTEGER N, DISPLAY;
BEGIN
    INTEGER I, LIMIT, COUNT, START, DELTA;
    IF MOD(N, 2) = 0 THEN
      BEGIN
        START := 2;
        DELTA := 1;
      END
    ELSE  % ONLY NEED TO CHECK ODD DIVISORS %
      BEGIN
        START := 3;
        DELTA := 2;
      END;
    % 1 IS A DIVISOR OF ANY NUMBER > 1 %
    IF N > 1 THEN COUNT := 1 ELSE COUNT := 0;
    IF (DISPLAY <> 0) AND (COUNT <> 0) THEN WRITEON(1);
    % CHECK REMAINING POTENTIAL DIVISORS %
    I := START;
    LIMIT := N / START;
    WHILE I <= LIMIT DO
      BEGIN
        IF MOD(N, I) = 0 THEN
          BEGIN
            IF DISPLAY <> 0 THEN WRITEON(I);
            COUNT := COUNT + 1;
          END;
        I := I + DELTA;
        IF COUNT = 1 THEN LIMIT := N / I;
      END;
   DIVISORS := COUNT;
END;
 
COMMENT MAIN PROGRAM BEGINS HERE;
INTEGER I, NDIV, TRUE, FALSE, HIGHDIV, HIGHNUM;
TRUE := -1;
FALSE := 0;
 
WRITE("PROPER DIVISORS OF FIRST TEN NUMBERS:");
FOR I := 1 STEP 1 UNTIL 10 DO
  BEGIN
     WRITE(I, " : ");
     NDIV := DIVISORS(I, TRUE);
  END;
 
WRITE("SEARCHING FOR NUMBER UP TO 10000 WITH MOST DIVISORS ...");
HIGHDIV := 1;
HIGHNUM := 1;
FOR I := 1 STEP 1 UNTIL 10000 DO
  BEGIN
     NDIV := DIVISORS(I, FALSE);
     IF NDIV > HIGHDIV THEN 
       BEGIN
         HIGHDIV := NDIV;
         HIGHNUM := I;
       END;
  END;
WRITE("THE NUMBER IS:", HIGHNUM);
WRITE("IT HAS", HIGHDIV, " DIVISORS");
 
END
 