Help with "Cyclic Addition" function

Hello....

I am working on a program in Turbo Pascal which in one section

requires a

function to do what I call -- for lack of a better term -- "cyclic

addition".

The input and output for the function is a long integer. Within the

function

each element of the of the number input into the function is added to

each other

to create a new number which in turn is fed into the loop until a number

between

1 and 9 is achieved.

Example: 1971 =

1 + 9 + 7 + 1 =

18

18 =

1 + 8 =

9

At this point the output of this function becomes the input for a case

statement

which selects the next function to branch to according to the input it

receives.

I have created such a function called "ReduceNumber" in this

example but

I am not completely happy with it. In my opinion, it relies too heavily

on brute

force also I predict that it will fail if passed a very large number. I

am

hoping that someone here can suggest an alternative approach which will

allow a

string of any length for input and also allow me to write tighter code.

Thank you all in advance

David Solly

Function ReduceNumber (Number : LongInt) : LongInt;

{

REDNO2.INC

Program: Reduce Number (Function)

Purpose: Creates quasi-random numbers for games of fortune and

chance.

History: Written by David Solly

19 September 1996

Methodology:

Add each element to the next within a number.

Example: 1971 =

1 + 9 + 7 + 1 =

18

18 =

1 + 8 =

9

Quote:

}

Var

LN1, Rmd, Acc, Temp, k : LongInt;

i, E: Integer;

Function Pow(B, E : Double) : Double;

{ Local power function }

Begin

Pow := Exp(E * Ln(B))

End; { Function Pow }

Begin

{ Initiate the variables }

LN1 := Number; { The local copy of the number to reduce }

Acc := 0; { Clear Acc. Prob. redundant but safe }

E := 0; { Number of decimal places counted }

Rmd := 0; { Clear Rmd. Prob. redundant but safe }

{

Calculate the number of decimal places by repeatedly dividing

the number by 10

}

Repeat

LN1 := LN1 div 10;

Inc(E); { Count number of decimal places }

Until LN1 <= 9;

{

Reduce by dividing by diminising powers of 10 and

adding the result to the variable Acc.

}

For i := E downto 0 do

Begin

k := Trunc(Pow(10, i)); { k = 10^i

Quote:

}

Temp := Number div k; { Divide the number

Quote:

}

Rmd := Number mod k; { Find what remains

Quote:

}

Acc := Acc + Temp; { Cumulative addition of the above results

Quote:

}

Number := Rmd; { Feed Rmd back into the loop

Quote:

}

End;

ReduceNumber := Acc;

End; { Function ReduceNumber }