THE PUZZLET PAGE

PUZZLET 056 - THE SOLUTION


' Puzzlet #056
' Find the integer below 5,000 which
' contains the largest number of factors.
' A factor is any smaller integer which
' divides exactly into the integer being
' factorised, except the integer itself.
' By Dave Ellis.


declare CountFactors(n: int)
def num, maxNum: int
def numFactors, maxFactors: int
openconsole
print
"Searching ...": print
print
"Checking  Number  Factors": print

maxNum = 0
maxFactors = 0
for num = 1 to 4999
    locate 5, 3
    print using ("####", num)
    numFactors = CountFactors(num)
    if numFactors > maxFactors
        maxNum = num
        maxFactors = numFactors
        locate 5, 12
        print using ("####", maxNum)
        locate 5, 22
        print using ("##", maxFactors)
    endif
next
num

print: print maxNum, "has ",
print maxFactors, "factors.": print
print: print "Press any key to exit ... ",
do: until inkey$ <> ""
closeconsole
end


sub CountFactors(n)
' Returns the number of factors of n.
' Does not include n itself.
def count, i, stopLoop: int
count = 1
stopLoop = int(n/2)
for i = 2 to stopLoop
    if n % i = 0: 'must be a factor
        count = count + 1
    endif
next i
return count

PROGRAM NOTES


Variable num holds the value of the integer currently being investigated.
Variable maxNum holds the value of the integer which yielded the largest number of factors so far.
Variable numFactors holds the number of factors of num.
Variable maxFactors holds the number of factors of maxNum.

The main FOR loop runs num through its specified range, 1 to 4999. The code isn't very fast, so a printout of what's going on is relayed to the screen.  Note the use of the 
locate command to ensure the printout appears in the same place every time.

After letting the user know which value of num is being investigated, it's passed as parameter to the function CountFactors(), which simply counts the factors in num  and returns them to be stored in variable Numfactors.

If the value in NumFactors is greater than that stored in maxFactors (which was originally set to zero), then any value already in
maxFactors is replaced with that in NumFactors. Likewise, the value in maxNum is overwritten with that in num.  

These two are always kept in step so that the program knows which number held the greatest number of factors in those searched so far. Every time a greater number of factors is found, it's printed out to the screen to keep the user informed of how the search is going.

How does CountFactors() work? It keeps a tally of how many factors have been found in its argument (local variable n) so far by storing it in variable count.  This is preset to 1 and the search commenced at 2, since 1 is always a factor of any number.  The search must stop at half the value of n. This stop value is stored in variable stopLoop, derived from
stopLoop = int(n/2).

Any number which is a factor will divide into n without remainder. The expression
if n % i = 0 takes care of that by the use of integer arithmetic. Every time this expression returns TRUE, it means another factor has been found, and the value of count is incremented by 1.  When stopLoop is reached, whatever value is stored in count is returned to the calling routine.


When you run the code, it generates the screen shown inset.  

This is telling us that the highest number checked was 4999, and that 2520, with a total of 47 factors, is the solution.

Searching ...

Checking  Number  Factors

    4999          2520          47

2520 has 47 factors.

Press any key to exit ...


MAIN MENU
DOWNLOAD CODE
ARCHIVES


ite design/maintenance: Dave Ellis E-mail me!
Last Updated: January 12th, 2010.