Question: digit sum iteration

View 2735_add.mw on MapleNet or Download 2735_add.mw
View file details

Hi

what i want is a procedure to do the following:

given an integer input, add the digits together. if the no. of digits resulting is >1 repeat until a single digit remains.

I can do the first step, but i need an if..then statement to complete the operation in one step. e.g. if length(sum)>1 then continue looping.

> addList := proc (a)::integer; local i, sum; description "add a list of digits"; sum := 0; for i to length(a) do

sum := sum+convert(a, base, 10)[i] end do end proc;
 

> addList(99999099999999999999999986665432133399999999999787544556998888888888888888888888888888888888888888899999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999666666666666666666666666666699888888888777777777777888777777777777777777788888888888887777777888888888888888888888888999999777777777777777777777777777777777777777777777777777777777777999);
                                    3088
> addList(%);
                                     19
> addList(%);
                                     10
> addList(%);
                                      1
>

(see attached add.mw file)
 

rgds

Please Wait...