THE PUZZLET PAGE


How_Does_It_Work


FUNCTION:  IsDigiNum()A DigiNum is an integer divisible by both the sum and product of its own digits., such as 36, 8112, etc.

When an integer is passed to IsDigiNum(), the function checks that its argument can be divided by both the product and sum of its own digits. If it can, TRUE (-1) is returned, otherwise FALSE (0).

It's up to you what kind of integer you pass to the function, as long as the function declaration has already taken this data-typing into account. See the table below for the parameter limits.

Type

Exponent of 2
Value

int 230 1073741824
uint 231 2147483648
int64 261 2305843009213693952
uint64 262 4611686018427387904

Declaring the function must be done like this:

declare IsDigiNum(d: int) (or whatever integer type you want).

Here's the code:


sub IsDigiNum(d)
' if argument d is a DigiNum, returns
' TRUE (-1), else returns FALSE (0)
def flag: int
flag = 0
if NoZeroes(d): 'does it contain zeroes?
    prodDigs = ProductDigits(d)
    'is it divisible by product of own digits?
    if d % prodDigs = 0
        sumDigs = SumDigits(d)
        'is it divisible by sum of own digits?
        if d % sumDigs = 0
            flag = -1
        endif
    endif
endif
return flag

Overview:  Variable d holds the number to be checked.  Before checking that d can be divided by the product of its own digits, we have to be sure none of the digits is a zero, since the product would then be zero; trying to divide d by zero would cause an error.

So function NoZeroes() is called at this time. Obviously, you need to include this function in your code if you want to use IsDigiNum().  If you want to get a copy of the code (and see a detailed description of how it works), click here now.

If there are no zeroes in d, we can move on to obtain the product of its digits.  Function ProductDigits is called to perform this task. 
You need to include this function in your code if you want to use IsDigiNum().  If you want to get a copy of the code (and see a detailed description of how it works), click here now.

If it turns out that d is divisible by the product of its own digits, we need to make one final check - can it be divided by the sum of its own digits?  To obtain the sum, function SumDigits() is called. 
You need to include this function in your code if you want to use IsDigiNum().  If you want to get a copy of the code (and see a detailed description of how it works), click here now.

If this final hurdle is successfully leaped, d must be a DigiNum.  A flag is set to TRUE (-1).  If d isn't a DigiNum, flag is left is its initialised state, FALSE (0).  The job is now finished, and the flag's status is returned to the calling routine.


To make it simpler to follow a detailed code description, I've numbered the lines as shown below.  Note that I omitted comment-only lines.


1
2
3
4
5
6
7
8
9
10
11

flag = 0
if NoZeroes(d): 'does it contain zeroes?
    prodDigs = ProductDigits(d)
    if d % prodDigs = 0
        sumDigs = SumDigits(d)
        if d % sumDigs = 0
            flag = -1
        endif
    endif
endif
return flag

Now for a line-by-line exegesis of the code.

1

flag = 0.  Variable flag will be used to indicate a TRUE (-1) or FALSE (0) answer to this function's main question: "Is argument d a DigiNum?"  Line 1 assumes a negative answer arbitrarily by initialising flag to zero.

2

if NoZeroes(d).  We'll be checking shortly to see if d can be divided by the product of its own digits.  If one of those digits is a zero, the product will be a zero, and this would cause an error when trying to divide by zero.  To prevent this, we need to know up-front if there's a zero anywhere in d's own digits.

Line 2 makes this check by calling external function NoZeroes().  So it's asking the question "Are there any zeroes in d?"  Note:  see the overview above for a link to the NoZeroes() code.

3

prodDigs = ProductDigits(d).  Line 3 is only executed if the answer to the question in line 2 is "yes."  Now that we know there are no zeroes in d, it's ok to go ahead and find the product of its digits. 

This is achieved by calling the external function ProductDigits().  So line 3 is saying "Pass d as parameter to function ProductDigits() and save the result in variable prodDigs." 
Note:  see the overview above for a link to the ProductDigits() code.

4

if d % prodDigs = 0. We're ready now to see if d can be divided exactly by the product of its own digits.  Integer arithmetic is used here.  The percentage sign means "take only the remainder of a division, discarding everything else."

Line 4 is asking the question "If we divide d by the value stored in prodDigs (that is, the product of its own digits), is there no remainder?"

5

sumDigs = SumDigits(d).  Line 5 will only be executed if the answer to the question in line 4 is "yes."  There's no point in going on if it's "no."  But having established that d can be divided exactly by the product of its own digits, we need to make one final check: can d be divided exactly by the sum of its own digits?

This is achieved by calling the external function SumDigits().  So line 3 is saying "Pass d as parameter to function SumDigits() and save the result in variable sumDigs."  Note:  see the overview above for a link to the SumDigits() code.

6

if d % sumDigs = 0.  Now that we have the sum of d's digits available we can check to see if it divides exactly into d.  Once again, integer arithmetic is used, already introduced above in the description to line 4.

Line 6 is asking the question "If we divide d by the value stored in sumDigs (that is, the sum of its own digits), is there no remainder?"

7

flag = -1.  If the answer to the question in line 6 is "yes," line 7 is executed.  The "yes" means d must be a DigiNum, so the status of flag  is altered to TRUE (-1). If it's "no," the status of flag is left in its initial state, FALSE (0).

8

endif.  Housekeeping - closes inner IF-ENDIF clause opened in line 6.

9

endifHousekeeping - closes IF-ENDIF clause opened in line 4.

10

endifHousekeeping - closes outer IF-ENDIF clause opened in line 2.

11

return flag.  The function has completed its task.  All that remains is to return the status of flag to the calling routine, and line 11 does exactly that.


MAIN MENU
HOW DOES IT WORK?

Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 4th, 2010.