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


 
OPENW 1
CLEARW 1
'
' Array f% is used to hold the divisors
DIM f%(SQR(20000)) ! cannot redim arrays, so set size to largest needed
'
' 1. Show proper divisors of 1 to 10, inclusive
'
FOR i%=1 TO 10
  num%=@proper_divisors(i%)
  PRINT "Divisors for ";i%;":";
  FOR j%=1 TO num%
    PRINT " ";f%(j%);
  NEXT j%
  PRINT
NEXT i%
'
' 2. Find (smallest) number <= 20000 with largest number of proper divisors
'
result%=1 ! largest so far
number%=0 ! its number of divisors
FOR i%=1 TO 20000
  num%=@proper_divisors(i%)
  IF num%>number%
    result%=i%
    number%=num%
  ENDIF
NEXT i%
PRINT "Largest number of divisors is ";number%;" for ";result%
'
~INP(2)
CLOSEW 1
'
' find the proper divisors of n%, placing results in f%
' and return the number found
'
FUNCTION proper_divisors(n%)
  LOCAL i%,root%,count%
  '
  ARRAYFILL f%(),0
  count%=1 ! index of next slot in f% to fill
  '
  IF n%>1
    f%(count%)=1
    count%=count%+1
    root%=SQR(n%)
    FOR i%=2 TO root%
      IF n% MOD i%=0
        f%(count%)=i%
        count%=count%+1
        IF i%*i%<>n% ! root% is an integer, so check if i% is actual squa- lists:seq(1,10)].                                      
X: 1, N: []
X: 2, N: [1]
X: 3, N: [1]
X: 4, N: [1,2]
X: 5, N: [1]
X: 6, N: [1,2,3]
X: 7, N: [1]
X: 8, N: [1,2,4]
X: 9, N: [1,3]
X: 10, N: [1,2,5]
[ok,ok,ok,ok,ok,ok,ok,ok,ok,ok]
 
2> properdivs:longest(20000).
With 79, Number 15120 has the most divisors
re root of n%
          f%(count%)=n%/i%
          count%=count%+1
        ENDIF
      ENDIF
    NEXT i%
  ENDIF
  '
  RETURN count%-1
ENDFUNC
 