THE PUZZLET PAGE


How_Does_It_Work


FUNCTION:  SumSquares().  This is rather a specialised function, but is included in the library as it gets used in different programs occasionally.

When an integer is passed to SumSquares(), the function separates each digit, takes the square of each digit, adds the results together, and returns the sum to the calling routine.

Example: 
print SumSquares(253) will print out 38.  This is due to the fact that 22 + 52 + 32 = 38.

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 SumSquares(s: int) (or whatever integer type you want).

Here's the code:


sub SumSquares(s)
' Accepts integer s, returns sum
' of squares of digits of s.
' By Dave Ellis.
def sum: int
sum = 0
do
    sum = sum + (s % 10)^2: 'square LSD
    s = s/10: 'chop off LSD
until s = 0: 'all digits chopped off!
return sum


Overview:  Integer arithmetic is used to separate out the LSD and square it. The result is stored in sum.

The LSD is now chopped off and the process repeated on the new LSD.  This goes on until every digit in the argument has been squared and added to sum

To make it simpler to follow a detailed code description, I've numbered the lines as shown below.


1
2
3
4
5
6
7

def sum: int
sum = 0
do
    sum = sum + (s % 10)^2: 'square LSD
    s = s/10: 'chop off LSD
until s = 0: 'all digits chopped off!
return sum

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

1

def sum: int.  The sum of the squares of the function's argument will be totalised into variable sum and returned to the calling routine.  Line 1 declares the variable in readiness.

2

sum = 0.  Initialises variable sum to zero prior to the start of the code.

3

do.  Opens the main DO loop.

4

sum = sum + (s % 10)^2.  Line 4 is more easily understood by working from the inside of the brackets outwards.  Variable s holds the function's argument, the integer whose digits will be squared and summed.  The expression s % 10  is integer arithmetic.  It means "Divide the contents of variable s by 10 and discard everything but the remainder."  For instance, if holds 234, then the expression s % 10 will result in 4.

The result in this case will be squared, added to whatever value is already stored in variable sum, and the total saved in sum, over-writing what was originally stored there.

Note that the contents of variable s haven't actually been changed at this time.  To do that the code should say something like "s = s% 10."

5

s = s/10.  This line also uses integer arithmetic.  It effectively says Divide the contents of variable s by 10, discard any remainder, and save the result in s, over-writing whatever was already stored there.  For example, if s held 234 before this operation, it would hold 23 afterwards.  In effect, it has discarded the LSD (least significant digit).  Note that this action has permanently changed the contents of s.

6

until s = 0.  The end of the do-until loop opened in line 3 has been reached.  The code must now decide whether to execute the loop again or quit.  The decision will be based on the contents of variable s.  If the last execution of line 5 reduced it to zero, it means all digits have been processed and there's no more to do.  So line 6 is saying "If the contents of variable s equal zero, quit the do-until loop, ot herwise execute it again from line 3."

7

return sum.  When the do-until loop is finally finished, the code executes the final line.  At this point, all the squares of the digits of argument s have been totalised into variable sum, so all that remains is to return that value to the calling routine.

The table below shows the function in action.  The integer 39274 is supplied as the function's argument.  You can see how it is processed line-by-line, finally returning the value 159 as the sum of the squares of its digits.


s
s%10
(s%10)2
sum
s/10
entry
39274


0


39274
4
16
16
3927

3927
7
49
65
392

392
2
4
69
39

39
9
81
150
8

3
3
9
159
0
exit
0


159


This code was originally written with a different algorithm, using strings. This version runs much faster.

MAIN MENU
HOW DOES IT WORK?

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