Objectives: In this tutorial, we discuss the digital sum of a positive integer and a Maple program that calculates the digital sum. After working through these materials, the student will be able to calculate a digital sum, to pick a digit from a number using Maple, to use the commands irem and iquo, and to use nested while .. do loops.


Definition of a Digital Sum. Given a positive integer, say 321, we form the sum of the digits, 3+2+1 = 6. 6 is called the digital sum of 321. If the sum of the digits is greater than 9 as in the case of 789 with 7+8+9 = 26 then we again add the digits of the result, 2+6 = 8. 8 is called the digital sum of 789. In general, we keep adding the sum of the digits of the results until we get a single digit for an answer.

Maple program for calculating the digital sum of a positive integer n:

> digitalSum:=proc(n)

> local c,d,r,sum;

> c:=n;

> while(c>9) do

> sum:=0;

> d:=c;

> while(d>9) do

> r:=irem(d,10);

> sum:=sum+r;

> d:=iquo(d,10);

> od;

> c:=sum+d;

> print(c);

> od;

> end:

Run simulation of program.

Comments on the program:

  1. This program defines a function which we have called digitalSum. The first line of the program, digitalSum:=proc(n), defines the name of the program and indicates that it is a procedure which can be thought of as another name for a function. The n is the name of the variable that represents an element of the domain of digitalSum.
  2. The second line, local c,d,r,sum;, tells the program that we are defining new variables called c, d, r and sum. These are local variables which means that the values of c, d, r and sum that are assigned by the program are only valid in this program.
  3. The third line of the program assigns the value of n to c.
  4. The fourth line, while(c>9) do, is a variation of the for .. from .. while .. do;; since there is no variable i that is increased by 1 in each iteration of this command, the for .. from .. of the command is deleted. Note that there are two while .. do statements in this program. Since the od; statement does not appear before the second while .. do statement starts in the seventh line, the first while .. do statement is not completed before the second one in the seventh starts. This is an example of what many refer to as two nested while .. do statements. The command while(c>9) do tells Maple that as long as c is greater than 9, execute what follows the do statement up to the second od; statement. As will be noted below the value of c will change. When c becomes less than 9, Maple drops out of the loop to the line following the second od; statement. Mathematically, it can be shown that c must eventually become less than 9.
  5. The fifth and sixth lines assign the value of 0 to the variable sum and the value of c to d, respectively.
  6. The seventh line, while(d>9) do, is the start of the second while .. do statement. The command while(d>9) do tells Maple that as long as d is greater than 9, execute what follows the do statement up to the first od; statement. As will be noted below the value of d will change. When d becomes less than 9, Maple drops out of the loop to the line following the first od; statement. Mathematically, it can be shown that d must eventually become less than 9.
  7. The eight line, r:=irem(d,10);, tells Maple to calculate the remainder when d is divided by 10 and store the remainder in the variable r. This remainder is the last digit of d.
  8. The ninth line, sum:=sum+r;, adds the last digit of d that was stored in r to the value in sum. The answer is stored back in sum. Eventually, the variable sum will contain the sum of all the digits of d excluding the first digit of d.
  9. The tenth line, d:=iquo(d,10);, tells Maple to calculate the quotient when d is divided by 10 and store the quotient in the variable d. This quotient is the result of dropping the last digit from d.
  10. The eleventh line, od;, tells Maple that this is the end of the second while .. do statement.
  11. The twelfth line, c:=sum+d;, takes the value of sum and the value of d, adds them together and stores the answer in c. Recall from line 8 above that sum contains all the digits of the original d except for the first. When Maple drops out of the second while .. do statement, the current value for d is the first digit of the original d. Hence, the value of c is the sum of all the digits of d.
  12. The thirteenth line, print(c);, prints this current value of c.
  13. The fourteenth line, od;, tells Maple that this is the end of the first while .. do statement.
  14. The fifteenth line, end;, indicates to Maple that this is the end of the program.

To test our function, we type

> digitalSum(1234567);

[Maple Math]
[Maple Math]
[Maple Math]

> digitalSum(500!);

[Maple Math]
[Maple Math]
[Maple Math]


Exercises:

  1. Write a program that takes a positive integer n and reverses all of its digits. For example, if we start with the number 123 then the output would be 321.
  2. A positve integer n is a palindrone if n and the number obtained from n by reversing all of its digits are the same. As an example, consider 12321. Write a program that takes a positive integer n and determines whether it is a palindrone.